8.4 Annotations

8.3 Annotations - ggplot2 annotation options - geom_text and geom_label

  • geom_rect()

  • geom_line(), geom_path(), geom_segment(), arrow()

  • geom_vline(), geom_hline(), geom_abline()

  • annotate() which can be used in combination with arrow()

base + annotate(
    geom = "text", x = 42, y = 20, label = "The Adelie species is on all 3 islands", size = 5, color = "darkcyan")

Arrows Code

base +
  annotate(
    geom = "curve", x = 53, y = 20, xend = 49, yend = 18.5, 
      curvature = .3, size = 1, arrow = arrow(length = unit(3, "mm"))
  ) +
   annotate(geom = "text", x = 53.1, y = 20, 
    label = "Average Chinstrap", hjust = "left", size = 4, color = "darkcyan") +
  annotate(
   geom = "curve", x = 35, y = 20, xend = 38, yend = 18.5, 
    curvature = .3, size = 1, arrow = arrow(length = unit(3, "mm"))
  ) +
  annotate(geom = "text", x = 32, y = 20.3, 
           label = "Average Adelie", hjust = "left", size = 4, color = "darkcyan") +
  annotate(
   geom = "curve", x = 53, y = 15, xend = 48, yend = 15, 
    curvature = .3, size = 1, arrow = arrow(length = unit(3, "mm"))
  ) +
  annotate(geom = "text", x = 53, y = 15.3, 
           label = "Average Gentoo", hjust = "left", size = 4, color = "darkcyan") 

Arrows Plot

base +
  annotate(
    geom = "curve", x = 53, y = 20, xend = 49, yend = 18.5, curvature = .3, size = 1, arrow = arrow(length = unit(3, "mm"))
  ) +
   annotate(geom = "text", x = 53.1, y = 20, label = "Average Chinstrap", hjust = "left", size = 4, color = "darkcyan") +
  annotate(
   geom = "curve", x = 35, y = 20, xend = 38, yend = 18.5, curvature = .3, size = 1, arrow = arrow(length = unit(3, "mm"))
  ) +
  annotate(geom = "text", x = 32, y = 20.3, label = "Average Adelie", hjust = "left", size = 4, color = "darkcyan") +
  annotate(
   geom = "curve", x = 53, y = 15, xend = 48, yend = 15, curvature = .3, size = 1, arrow = arrow(length = unit(3, "mm"))
  ) +
  annotate(geom = "text", x = 53, y = 15.3, label = "Average Gentoo", hjust = "left", size = 4, color = "darkcyan") +
    theme(legend.position = "none")

 astronauts %>% 
  filter(nationality %in% c("U.S.","Australia", "U.K.", "U.S.S.R/Russia", "Japan")) %>% 
  ggplot(aes(x = nationality, y = hours_mission, color = hours_mission)) +
  coord_flip() +
    geom_point(size = 4, alpha = 0.15) +
    geom_boxplot(color = "gray60", outlier.alpha = 0) +
    stat_summary(fun = mean, geom = "point", size = 5, color = "dodgerblue") +
   annotate(
   geom = "curve", x = 3.8, y = 2500, xend = 4, yend = 650,
   curvature = .3, arrow = arrow(length = unit(2, "mm"))
) +
 annotate(
    "text", x = 3.7, y = 2500,
    label = "The U.S. Mean Hours Mission", size = 2.7) +
   annotate(
   geom = "curve", x = 4.7, y = 4200, xend = 5, yend = 2800,
   curvature = .3, arrow = arrow(length = unit(2, "mm"))
) +
 annotate(
    "text", x = 4.5, y = 3700,
    label = "The interquartile range, between 25% and 75% of values", size = 2.8) +
    annotate(
   geom = "curve", x = 1, y = 3800, xend = 1, yend = 900,
   curvature = .3, arrow = arrow(length = unit(2, "mm"))
) +
 annotate(
    "text", x = .8, y = 3000,
    label = "Australian Astronaut Andrew S. W. Thomas 
    completed missions in 1983, 1998, 2001, 2005 and is now retired", size = 2.8) +
  scale_color_viridis_c() +
  scale_y_continuous(limits = c(0, 5000)) +
  labs(title = "Length of Astronaut Missions in hours",
       subtitle = "A Study was conducted on the effects of space on various individuals",
       caption = "Source: TidyTuesday 2020 week 29 \n inspired by plots in The Evolution of a ggplot (ep1) by Cedric Scherer") +
    theme_fivethirtyeight() +
  theme(legend.position = "none") +
  theme(plot.title = element_text(hjust = .5)) +
    theme(plot.subtitle = element_text(hjust = .5))