class: center, middle, inverse, title-slide # annotate ### Gina Reynolds, April 26, 2020 --- --- name: annotate # Exclamations and Interjections: Annotate using "geoms" not tied to data --- class: split-40 count: false .left-panel-annotate-auto[ ```r *ggplot(data = gapminder_2002) ``` ] .right-panel-annotate-auto[ <img src="annotate_files/figure-html/annotate_auto_1_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-annotate-auto[ ```r ggplot(data = gapminder_2002) + * aes(x = gdpPercap) ``` ] .right-panel-annotate-auto[ <img src="annotate_files/figure-html/annotate_auto_2_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-annotate-auto[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + * aes(y = lifeExp) ``` ] .right-panel-annotate-auto[ <img src="annotate_files/figure-html/annotate_auto_3_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-annotate-auto[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + * geom_point() ``` ] .right-panel-annotate-auto[ <img src="annotate_files/figure-html/annotate_auto_4_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-annotate-auto[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + * annotate(geom = "point", * x = 10000, * y = 60, * color = "red") ``` ] .right-panel-annotate-auto[ <img src="annotate_files/figure-html/annotate_auto_5_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-annotate-auto[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + annotate(geom = "point", x = 10000, y = 60, color = "red") + * annotate(geom = "text", * x = c(3000, 10000, 40000), * y = 80, * label = "Hello", * color = "blue") ``` ] .right-panel-annotate-auto[ <img src="annotate_files/figure-html/annotate_auto_6_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-annotate-auto[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + annotate(geom = "point", x = 10000, y = 60, color = "red") + annotate(geom = "text", x = c(3000, 10000, 40000), y = 80, label = "Hello", color = "blue") + * annotate(geom = "curve", * x = 3000, * y = 79, * xend = 10000 - 500, * yend = 60 + .3, * color = "green", * arrow = arrow(angle = 20)) ``` ] .right-panel-annotate-auto[ <img src="annotate_files/figure-html/annotate_auto_7_output-1.png" width="100%" /> ] <style> .left-panel-annotate-auto { color: #777; width: 38.6138613861386%; height: 92%; float: left; font-size: 80% } .right-panel-annotate-auto { width: 59.4059405940594%; float: right; padding-left: 1%; font-size: 80% } .middle-panel-annotate-auto { width: 0%; float: left; padding-left: 1%; font-size: 80% } </style> --- ## Convenience annotation geoms Some convenience functions have also been written for annotation using the geom_*() syntax. - geom_abline() - geom_hline() - geom_vline() --- class: split-40 count: false .left-panel-annotation_geoms-auto[ ```r *ggplot(data = gapminder_2002) ``` ] .right-panel-annotation_geoms-auto[ <img src="annotate_files/figure-html/annotation_geoms_auto_1_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-annotation_geoms-auto[ ```r ggplot(data = gapminder_2002) + * aes(x = gdpPercap) ``` ] .right-panel-annotation_geoms-auto[ <img src="annotate_files/figure-html/annotation_geoms_auto_2_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-annotation_geoms-auto[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + * aes(y = lifeExp) ``` ] .right-panel-annotation_geoms-auto[ <img src="annotate_files/figure-html/annotation_geoms_auto_3_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-annotation_geoms-auto[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + * geom_point() ``` ] .right-panel-annotation_geoms-auto[ <img src="annotate_files/figure-html/annotation_geoms_auto_4_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-annotation_geoms-auto[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + * geom_abline(slope = .01, intercept = 0) ``` ] .right-panel-annotation_geoms-auto[ <img src="annotate_files/figure-html/annotation_geoms_auto_5_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-annotation_geoms-auto[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + geom_abline(slope = .01, intercept = 0) + * geom_hline(yintercept = 70, * linetype = "dotted") ``` ] .right-panel-annotation_geoms-auto[ <img src="annotate_files/figure-html/annotation_geoms_auto_6_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-annotation_geoms-auto[ ```r ggplot(data = gapminder_2002) + aes(x = gdpPercap) + aes(y = lifeExp) + geom_point() + geom_abline(slope = .01, intercept = 0) + geom_hline(yintercept = 70, linetype = "dotted") + * geom_vline(xintercept = c(1000, 10000), * linetype = "dashed") ``` ] .right-panel-annotation_geoms-auto[ <img src="annotate_files/figure-html/annotation_geoms_auto_7_output-1.png" width="100%" /> ] <style> .left-panel-annotation_geoms-auto { color: #777; width: 38.6138613861386%; height: 92%; float: left; font-size: 80% } .right-panel-annotation_geoms-auto { width: 59.4059405940594%; float: right; padding-left: 1%; font-size: 80% } .middle-panel-annotation_geoms-auto { width: 0%; float: left; padding-left: 1%; font-size: 80% } </style> --- --- class: split-40 count: false .left-panel-ref_lines-rotate[ ```r library(gapminder) gapminder %>% filter(continent == "Oceania") %>% ggplot() + aes(x = year) + aes(y = pop/1000000) + aes(linetype = country) + coord_cartesian(xlim = c(1952,2007), ylim = c(0, 25)) + geom_line(color = "steelblue") + geom_hline(yintercept = 10, linetype = "dotted") + annotate(geom = "text", x = 1970, y = 22, label = "Often these\ngeometric annotations\nare also accompanied\nby text annotation", hjust = 1, size = 5, angle = 20, lineheight = .8, fontface = "italic", color = "plum4") ``` ] .right-panel-ref_lines-rotate[ <img src="annotate_files/figure-html/ref_lines_rotate_1_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-ref_lines-rotate[ ```r library(gapminder) gapminder %>% filter(continent == "Oceania") %>% ggplot() + aes(x = year) + aes(y = pop/1000000) + aes(linetype = country) + coord_cartesian(xlim = c(1952,2007), ylim = c(0, 25)) + geom_line(color = "steelblue") + * geom_vline(xintercept = 1980, linetype = "dotted") + annotate(geom = "text", x = 1970, y = 22, label = "Often these\ngeometric annotations\nare also accompanied\nby text annotation", hjust = 1, size = 5, angle = 20, lineheight = .8, fontface = "italic", color = "plum4") ``` ] .right-panel-ref_lines-rotate[ <img src="annotate_files/figure-html/ref_lines_rotate_2_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-ref_lines-rotate[ ```r library(gapminder) gapminder %>% filter(continent == "Oceania") %>% ggplot() + aes(x = year) + aes(y = pop/1000000) + aes(linetype = country) + coord_cartesian(xlim = c(1952,2007), ylim = c(0, 25)) + geom_line(color = "steelblue") + * annotate(geom = "text", x = 1982, y = 10, label = "1952", angle = 90, size = 6) + annotate(geom = "text", x = 1970, y = 22, label = "Often these\ngeometric annotations\nare also accompanied\nby text annotation", hjust = 1, size = 5, angle = 20, lineheight = .8, fontface = "italic", color = "plum4") ``` ] .right-panel-ref_lines-rotate[ <img src="annotate_files/figure-html/ref_lines_rotate_3_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-ref_lines-rotate[ ```r library(gapminder) gapminder %>% filter(continent == "Oceania") %>% ggplot() + aes(x = year) + aes(y = pop/1000000) + aes(linetype = country) + coord_cartesian(xlim = c(1952,2007), ylim = c(0, 25)) + geom_line(color = "steelblue") + * geom_abline(slope = 1, intercept = -1950, linetype = "dashed") + annotate(geom = "text", x = 1970, y = 22, label = "Often these\ngeometric annotations\nare also accompanied\nby text annotation", hjust = 1, size = 5, angle = 20, lineheight = .8, fontface = "italic", color = "plum4") ``` ] .right-panel-ref_lines-rotate[ <img src="annotate_files/figure-html/ref_lines_rotate_4_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-ref_lines-rotate[ ```r library(gapminder) gapminder %>% filter(continent == "Oceania") %>% ggplot() + aes(x = year) + aes(y = pop/1000000) + aes(linetype = country) + coord_cartesian(xlim = c(1952,2007), ylim = c(0, 25)) + geom_line(color = "steelblue") + * geom_vline(xintercept = 1980, linetype = "dotted") + annotate(geom = "text", x = 1970, y = 22, label = "Often these\ngeometric annotations\nare also accompanied\nby text annotation", hjust = 1, size = 5, angle = 20, lineheight = .8, fontface = "italic", color = "plum4") ``` ] .right-panel-ref_lines-rotate[ <img src="annotate_files/figure-html/ref_lines_rotate_5_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-ref_lines-rotate[ ```r library(gapminder) gapminder %>% filter(continent == "Oceania") %>% ggplot() + aes(x = year) + aes(y = pop/1000000) + aes(linetype = country) + coord_cartesian(xlim = c(1952,2007), ylim = c(0, 25)) + geom_line(color = "steelblue") + * annotate(geom = "rect", xmin = -Inf, xmax = Inf, ymin = 10, ymax = 15, alpha = .2) + annotate(geom = "text", x = 1970, y = 22, label = "Often these\ngeometric annotations\nare also accompanied\nby text annotation", hjust = 1, size = 5, angle = 20, lineheight = .8, fontface = "italic", color = "plum4") ``` ] .right-panel-ref_lines-rotate[ <img src="annotate_files/figure-html/ref_lines_rotate_6_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-ref_lines-rotate[ ```r library(gapminder) gapminder %>% filter(continent == "Oceania") %>% ggplot() + aes(x = year) + aes(y = pop/1000000) + aes(linetype = country) + coord_cartesian(xlim = c(1952,2007), ylim = c(0, 25)) + geom_line(color = "steelblue") + * annotate(geom = "rect", xmin = 1980, xmax = 1970, ymin = -Inf, ymax = Inf, alpha = .2) + annotate(geom = "text", x = 1970, y = 22, label = "Often these\ngeometric annotations\nare also accompanied\nby text annotation", hjust = 1, size = 5, angle = 20, lineheight = .8, fontface = "italic", color = "plum4") ``` ] .right-panel-ref_lines-rotate[ <img src="annotate_files/figure-html/ref_lines_rotate_7_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-ref_lines-rotate[ ```r library(gapminder) gapminder %>% filter(continent == "Oceania") %>% ggplot() + aes(x = year) + aes(y = pop/1000000) + aes(linetype = country) + coord_cartesian(xlim = c(1952,2007), ylim = c(0, 25)) + geom_line(color = "steelblue") + * geom_point(data = . %>% group_by(country) %>% slice(n()), aes(label = country)) + annotate(geom = "text", x = 1970, y = 22, label = "Often these\ngeometric annotations\nare also accompanied\nby text annotation", hjust = 1, size = 5, angle = 20, lineheight = .8, fontface = "italic", color = "plum4") ``` ] .right-panel-ref_lines-rotate[ <img src="annotate_files/figure-html/ref_lines_rotate_8_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-ref_lines-rotate[ ```r library(gapminder) gapminder %>% filter(continent == "Oceania") %>% ggplot() + aes(x = year) + aes(y = pop/1000000) + aes(linetype = country) + coord_cartesian(xlim = c(1952,2007), ylim = c(0, 25)) + geom_line(color = "steelblue") + * geom_text(data = . %>% group_by(country) %>% slice(n()), aes(label = country), nudge_x = -3, nudge_y = 1) + annotate(geom = "text", x = 1970, y = 22, label = "Often these\ngeometric annotations\nare also accompanied\nby text annotation", hjust = 1, size = 5, angle = 20, lineheight = .8, fontface = "italic", color = "plum4") ``` ] .right-panel-ref_lines-rotate[ <img src="annotate_files/figure-html/ref_lines_rotate_9_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-ref_lines-rotate[ ```r library(gapminder) gapminder %>% filter(continent == "Oceania") %>% ggplot() + aes(x = year) + aes(y = pop/1000000) + aes(linetype = country) + coord_cartesian(xlim = c(1952,2007), ylim = c(0, 25)) + geom_line(color = "steelblue") + * annotate(geom = "curve", curvature = 1, x = 1970, xend = 2000, y = 23, yend = 20) + annotate(geom = "text", x = 1970, y = 22, label = "Often these\ngeometric annotations\nare also accompanied\nby text annotation", hjust = 1, size = 5, angle = 20, lineheight = .8, fontface = "italic", color = "plum4") ``` ] .right-panel-ref_lines-rotate[ <img src="annotate_files/figure-html/ref_lines_rotate_10_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-ref_lines-rotate[ ```r library(gapminder) gapminder %>% filter(continent == "Oceania") %>% ggplot() + aes(x = year) + aes(y = pop/1000000) + aes(linetype = country) + coord_cartesian(xlim = c(1952,2007), ylim = c(0, 25)) + geom_line(color = "steelblue") + * annotate(geom = "curve", curvature = .7, x = 1970, xend = 2000, y = 23, yend = 20) + annotate(geom = "text", x = 1970, y = 22, label = "Often these\ngeometric annotations\nare also accompanied\nby text annotation", hjust = 1, size = 5, angle = 20, lineheight = .8, fontface = "italic", color = "plum4") ``` ] .right-panel-ref_lines-rotate[ <img src="annotate_files/figure-html/ref_lines_rotate_11_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-ref_lines-rotate[ ```r library(gapminder) gapminder %>% filter(continent == "Oceania") %>% ggplot() + aes(x = year) + aes(y = pop/1000000) + aes(linetype = country) + coord_cartesian(xlim = c(1952,2007), ylim = c(0, 25)) + geom_line(color = "steelblue") + * annotate(geom = "curve", curvature = .3, x = 1970, xend = 2000, y = 23, yend = 20) + annotate(geom = "text", x = 1970, y = 22, label = "Often these\ngeometric annotations\nare also accompanied\nby text annotation", hjust = 1, size = 5, angle = 20, lineheight = .8, fontface = "italic", color = "plum4") ``` ] .right-panel-ref_lines-rotate[ <img src="annotate_files/figure-html/ref_lines_rotate_12_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-ref_lines-rotate[ ```r library(gapminder) gapminder %>% filter(continent == "Oceania") %>% ggplot() + aes(x = year) + aes(y = pop/1000000) + aes(linetype = country) + coord_cartesian(xlim = c(1952,2007), ylim = c(0, 25)) + geom_line(color = "steelblue") + * annotate(geom = "curve", curvature = .1, x = 1970, xend = 2000, y = 23, yend = 20) + annotate(geom = "text", x = 1970, y = 22, label = "Often these\ngeometric annotations\nare also accompanied\nby text annotation", hjust = 1, size = 5, angle = 20, lineheight = .8, fontface = "italic", color = "plum4") ``` ] .right-panel-ref_lines-rotate[ <img src="annotate_files/figure-html/ref_lines_rotate_13_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-ref_lines-rotate[ ```r library(gapminder) gapminder %>% filter(continent == "Oceania") %>% ggplot() + aes(x = year) + aes(y = pop/1000000) + aes(linetype = country) + coord_cartesian(xlim = c(1952,2007), ylim = c(0, 25)) + geom_line(color = "steelblue") + * annotate(geom = "curve", curvature = .0, x = 1970, xend = 2000, y = 23, yend = 20) + annotate(geom = "text", x = 1970, y = 22, label = "Often these\ngeometric annotations\nare also accompanied\nby text annotation", hjust = 1, size = 5, angle = 20, lineheight = .8, fontface = "italic", color = "plum4") ``` ] .right-panel-ref_lines-rotate[ <img src="annotate_files/figure-html/ref_lines_rotate_14_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-ref_lines-rotate[ ```r library(gapminder) gapminder %>% filter(continent == "Oceania") %>% ggplot() + aes(x = year) + aes(y = pop/1000000) + aes(linetype = country) + coord_cartesian(xlim = c(1952,2007), ylim = c(0, 25)) + geom_line(color = "steelblue") + * annotate(geom = "curve", curvature = -.3, x = 1970, xend = 2000, y = 23, yend = 20) + annotate(geom = "text", x = 1970, y = 22, label = "Often these\ngeometric annotations\nare also accompanied\nby text annotation", hjust = 1, size = 5, angle = 20, lineheight = .8, fontface = "italic", color = "plum4") ``` ] .right-panel-ref_lines-rotate[ <img src="annotate_files/figure-html/ref_lines_rotate_15_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-ref_lines-rotate[ ```r library(gapminder) gapminder %>% filter(continent == "Oceania") %>% ggplot() + aes(x = year) + aes(y = pop/1000000) + aes(linetype = country) + coord_cartesian(xlim = c(1952,2007), ylim = c(0, 25)) + geom_line(color = "steelblue") + * annotate(geom = "curve", curvature =-1.5, x = 1970, xend = 2000, y = 23, yend = 20) + annotate(geom = "text", x = 1970, y = 22, label = "Often these\ngeometric annotations\nare also accompanied\nby text annotation", hjust = 1, size = 5, angle = 20, lineheight = .8, fontface = "italic", color = "plum4") ``` ] .right-panel-ref_lines-rotate[ <img src="annotate_files/figure-html/ref_lines_rotate_16_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-ref_lines-rotate[ ```r library(gapminder) gapminder %>% filter(continent == "Oceania") %>% ggplot() + aes(x = year) + aes(y = pop/1000000) + aes(linetype = country) + coord_cartesian(xlim = c(1952,2007), ylim = c(0, 25)) + geom_line(color = "steelblue") + * annotate(geom = "curve", arrow = arrow(), curvature =-1.5, x = 1970, xend = 2000, y = 23, yend = 20) + annotate(geom = "text", x = 1970, y = 22, label = "Often these\ngeometric annotations\nare also accompanied\nby text annotation", hjust = 1, size = 5, angle = 20, lineheight = .8, fontface = "italic", color = "plum4") ``` ] .right-panel-ref_lines-rotate[ <img src="annotate_files/figure-html/ref_lines_rotate_17_output-1.png" width="100%" /> ] <style> .left-panel-ref_lines-rotate { color: #777; width: 59%; height: 92%; float: left; font-size: 80% } .right-panel-ref_lines-rotate { width: 40%; float: right; padding-left: 1%; font-size: 80% } .middle-panel-ref_lines-rotate { width: NA%; float: left; padding-left: 1%; font-size: 80% } </style> --- ## Using geoms for annotation -- Geoms can also be useful for annotation. --- class: split-40 count: false .left-panel-labeling_points-rotate[ ```r library(ggrepel) # for the very last option gapminder %>% filter(continent == "Asia") %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap) + scale_x_log10() + aes(y = lifeExp) + coord_cartesian(xlim = c(500, 40000), ylim = c(40,85)) + geom_point() + aes(label = country) + geom_text() ``` ] .right-panel-labeling_points-rotate[ <img src="annotate_files/figure-html/labeling_points_rotate_1_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-labeling_points-rotate[ ```r library(ggrepel) # for the very last option gapminder %>% filter(continent == "Asia") %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap) + scale_x_log10() + aes(y = lifeExp) + coord_cartesian(xlim = c(500, 40000), ylim = c(40,85)) + geom_point() + aes(label = country) + * geom_text(nudge_y = 1.25) ``` ] .right-panel-labeling_points-rotate[ <img src="annotate_files/figure-html/labeling_points_rotate_2_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-labeling_points-rotate[ ```r library(ggrepel) # for the very last option gapminder %>% filter(continent == "Asia") %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap) + scale_x_log10() + aes(y = lifeExp) + coord_cartesian(xlim = c(500, 40000), ylim = c(40,85)) + geom_point() + aes(label = country) + * geom_label(nudge_y = 1.25) ``` ] .right-panel-labeling_points-rotate[ <img src="annotate_files/figure-html/labeling_points_rotate_3_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-labeling_points-rotate[ ```r library(ggrepel) # for the very last option gapminder %>% filter(continent == "Asia") %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap) + scale_x_log10() + aes(y = lifeExp) + coord_cartesian(xlim = c(500, 40000), ylim = c(40,85)) + geom_point() + aes(label = country) + * geom_text(check_overlap = T, nudge_y = 1.25) ``` ] .right-panel-labeling_points-rotate[ <img src="annotate_files/figure-html/labeling_points_rotate_4_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-labeling_points-rotate[ ```r library(ggrepel) # for the very last option gapminder %>% filter(continent == "Asia") %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap) + scale_x_log10() + aes(y = lifeExp) + coord_cartesian(xlim = c(500, 40000), ylim = c(40,85)) + geom_point() + aes(label = country) + * ggrepel::geom_text_repel() ``` ] .right-panel-labeling_points-rotate[ <img src="annotate_files/figure-html/labeling_points_rotate_5_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-labeling_points-rotate[ ```r library(ggrepel) # for the very last option gapminder %>% filter(continent == "Asia") %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap) + scale_x_log10() + aes(y = lifeExp) + coord_cartesian(xlim = c(500, 40000), ylim = c(40,85)) + geom_point() + aes(label = country) + * shadowtext::geom_shadowtext(aes(color = lifeExp > 70)) ``` ] .right-panel-labeling_points-rotate[ <img src="annotate_files/figure-html/labeling_points_rotate_6_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-labeling_points-rotate[ ```r library(ggrepel) # for the very last option gapminder %>% filter(continent == "Asia") %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap) + scale_x_log10() + aes(y = lifeExp) + coord_cartesian(xlim = c(500, 40000), ylim = c(40,85)) + geom_point() + aes(label = country) + * geom_text(data = . %>% filter(lifeExp < 60)) ``` ] .right-panel-labeling_points-rotate[ <img src="annotate_files/figure-html/labeling_points_rotate_7_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-labeling_points-rotate[ ```r library(ggrepel) # for the very last option gapminder %>% filter(continent == "Asia") %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap) + scale_x_log10() + aes(y = lifeExp) + coord_cartesian(xlim = c(500, 40000), ylim = c(40,85)) + geom_point() + aes(label = country) + * geom_point(data = . %>% filter(lifeExp < 60), color = "salmon") ``` ] .right-panel-labeling_points-rotate[ <img src="annotate_files/figure-html/labeling_points_rotate_8_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-labeling_points-rotate[ ```r library(ggrepel) # for the very last option gapminder %>% filter(continent == "Asia") %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap) + scale_x_log10() + aes(y = lifeExp) + coord_cartesian(xlim = c(500, 40000), ylim = c(40,85)) + geom_point() + aes(label = country) + * geom_text(data = . %>% filter(country %in% c("Vietnam", "Thailand")), color = "seagreen") ``` ] .right-panel-labeling_points-rotate[ <img src="annotate_files/figure-html/labeling_points_rotate_9_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-labeling_points-rotate[ ```r library(ggrepel) # for the very last option gapminder %>% filter(continent == "Asia") %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap) + scale_x_log10() + aes(y = lifeExp) + coord_cartesian(xlim = c(500, 40000), ylim = c(40,85)) + geom_point() + aes(label = country) + * annotate(geom = "text", x = 5000, y = 58, label = "Iraq") ``` ] .right-panel-labeling_points-rotate[ <img src="annotate_files/figure-html/labeling_points_rotate_10_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-labeling_points-rotate[ ```r library(ggrepel) # for the very last option gapminder %>% filter(continent == "Asia") %>% filter(year == 2002) %>% ggplot() + aes(x = gdpPercap) + scale_x_log10() + aes(y = lifeExp) + coord_cartesian(xlim = c(500, 40000), ylim = c(40,85)) + geom_point() + aes(label = country) + * ggpmisc::stat_dens2d_filter(geom = "text_repel", keep.fraction = 0.5) ``` ] .right-panel-labeling_points-rotate[ <img src="annotate_files/figure-html/labeling_points_rotate_11_output-1.png" width="100%" /> ] <style> .left-panel-labeling_points-rotate { color: #777; width: 59%; height: 92%; float: left; font-size: 80% } .right-panel-labeling_points-rotate { width: 40%; float: right; padding-left: 1%; font-size: 80% } .middle-panel-labeling_points-rotate { width: NA%; float: left; padding-left: 1%; font-size: 80% } </style> --- --- # Strategies for annotating time series --- class: inverse, center, middle # Legend, ordered legend, or direct label --- class: split-40 count: false .left-panel-plots-auto[ ```r *dat ``` ] .right-panel-plots-auto[ ``` # A tibble: 9 x 5 year fruit price fruit_ABCD fruit_sane <int> <chr> <dbl> <fct> <fct> 1 2016 Oranges 4.25 Oranges Oranges 2 2016 Lemons 4 Lemons Lemons 3 2016 Grapes 3.25 Grapes Grapes 4 2017 Oranges 4 Oranges Oranges 5 2017 Lemons 4.75 Lemons Lemons 6 2017 Grapes 3.75 Grapes Grapes 7 2018 Oranges 5.55 Oranges Oranges 8 2018 Lemons NA Lemons Lemons 9 2018 Grapes 4.85 Grapes Grapes ``` ] --- class: split-40 count: false .left-panel-plots-auto[ ```r dat %>% * ggplot() ``` ] .right-panel-plots-auto[ <img src="annotate_files/figure-html/plots_auto_2_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-plots-auto[ ```r dat %>% ggplot() + * aes(x = year, y = price) ``` ] .right-panel-plots-auto[ <img src="annotate_files/figure-html/plots_auto_3_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-plots-auto[ ```r dat %>% ggplot() + aes(x = year, y = price) + * geom_line(size = 1.5) ``` ] .right-panel-plots-auto[ <img src="annotate_files/figure-html/plots_auto_4_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-plots-auto[ ```r dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + * aes(color = fruit) ``` ] .right-panel-plots-auto[ <img src="annotate_files/figure-html/plots_auto_5_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-plots-auto[ ```r dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(color = fruit) + * theme(legend.justification = c(1, 0.85)) ``` ] .right-panel-plots-auto[ <img src="annotate_files/figure-html/plots_auto_6_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-plots-auto[ ```r dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(color = fruit) + theme(legend.justification = c(1, 0.85)) + * theme(legend.position = "none") ``` ] .right-panel-plots-auto[ <img src="annotate_files/figure-html/plots_auto_7_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-plots-auto[ ```r dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(color = fruit) + theme(legend.justification = c(1, 0.85)) + theme(legend.position = "none") + * geom_text(data = . %>% * group_by(fruit) %>% * drop_na() %>% * filter(year == max(year)), * aes(label = fruit), * nudge_x = .1, nudge_y = .12, * color = "black", size = 7) ``` ] .right-panel-plots-auto[ <img src="annotate_files/figure-html/plots_auto_8_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-plots-auto[ ```r dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(color = fruit) + theme(legend.justification = c(1, 0.85)) + theme(legend.position = "none") + geom_text(data = . %>% group_by(fruit) %>% drop_na() %>% filter(year == max(year)), aes(label = fruit), nudge_x = .1, nudge_y = .12, color = "black", size = 7) + * geom_point(data = dat %>% * group_by(fruit) %>% * drop_na() %>% * filter(year == max(year)), * aes(fill = fruit), * shape = 21, size = 6, color = "white") ``` ] .right-panel-plots-auto[ <img src="annotate_files/figure-html/plots_auto_9_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-plots-auto[ ```r dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(color = fruit) + theme(legend.justification = c(1, 0.85)) + theme(legend.position = "none") + geom_text(data = . %>% group_by(fruit) %>% drop_na() %>% filter(year == max(year)), aes(label = fruit), nudge_x = .1, nudge_y = .12, color = "black", size = 7) + geom_point(data = dat %>% group_by(fruit) %>% drop_na() %>% filter(year == max(year)), aes(fill = fruit), shape = 21, size = 6, color = "white") + * scale_x_continuous(breaks = 2016:2018, * expand = c(0.1,0), * limits = c(2016,2018.2)) ``` ] .right-panel-plots-auto[ <img src="annotate_files/figure-html/plots_auto_10_output-1.png" width="100%" /> ] <style> .left-panel-plots-auto { color: #777; width: 59%; height: 92%; float: left; font-size: 80% } .right-panel-plots-auto { width: 40%; float: right; padding-left: 1%; font-size: 80% } .middle-panel-plots-auto { width: NA%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: split-40 count: false .left-panel-fct_reorder-rotate[ ```r dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(color = fruit) ``` ] .right-panel-fct_reorder-rotate[ <img src="annotate_files/figure-html/fct_reorder_rotate_1_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-fct_reorder-rotate[ ```r dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + * aes(color = fct_reorder2(fruit, year, price)) ``` ] .right-panel-fct_reorder-rotate[ <img src="annotate_files/figure-html/fct_reorder_rotate_2_output-1.png" width="100%" /> ] <style> .left-panel-fct_reorder-rotate { color: #777; width: 38.6138613861386%; height: 92%; float: left; font-size: 80% } .right-panel-fct_reorder-rotate { width: 59.4059405940594%; float: right; padding-left: 1%; font-size: 80% } .middle-panel-fct_reorder-rotate { width: 0%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: inverse, center, middle # Facet instead of annotation --- class: split-40 count: false .left-panel-via_faceting-auto[ ```r *dat ``` ] .right-panel-via_faceting-auto[ ``` # A tibble: 9 x 5 year fruit price fruit_ABCD fruit_sane <int> <chr> <dbl> <fct> <fct> 1 2016 Oranges 4.25 Oranges Oranges 2 2016 Lemons 4 Lemons Lemons 3 2016 Grapes 3.25 Grapes Grapes 4 2017 Oranges 4 Oranges Oranges 5 2017 Lemons 4.75 Lemons Lemons 6 2017 Grapes 3.75 Grapes Grapes 7 2018 Oranges 5.55 Oranges Oranges 8 2018 Lemons NA Lemons Lemons 9 2018 Grapes 4.85 Grapes Grapes ``` ] --- class: split-40 count: false .left-panel-via_faceting-auto[ ```r dat %>% * ggplot() ``` ] .right-panel-via_faceting-auto[ <img src="annotate_files/figure-html/via_faceting_auto_2_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-via_faceting-auto[ ```r dat %>% ggplot() + * aes(x = year) ``` ] .right-panel-via_faceting-auto[ <img src="annotate_files/figure-html/via_faceting_auto_3_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-via_faceting-auto[ ```r dat %>% ggplot() + aes(x = year) + * aes(y = price) ``` ] .right-panel-via_faceting-auto[ <img src="annotate_files/figure-html/via_faceting_auto_4_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-via_faceting-auto[ ```r dat %>% ggplot() + aes(x = year) + aes(y = price) + * facet_wrap(~fruit) ``` ] .right-panel-via_faceting-auto[ <img src="annotate_files/figure-html/via_faceting_auto_5_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-via_faceting-auto[ ```r dat %>% ggplot() + aes(x = year) + aes(y = price) + facet_wrap(~fruit) + * geom_line(data = . %>% * mutate(fruit_grey = fruit) %>% * select(-fruit), * aes(group = fruit_grey), * color = "grey") ``` ] .right-panel-via_faceting-auto[ <img src="annotate_files/figure-html/via_faceting_auto_6_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-via_faceting-auto[ ```r dat %>% ggplot() + aes(x = year) + aes(y = price) + facet_wrap(~fruit) + geom_line(data = . %>% mutate(fruit_grey = fruit) %>% select(-fruit), aes(group = fruit_grey), color = "grey") + * geom_line(size = 1.5, * color = "steelblue") ``` ] .right-panel-via_faceting-auto[ <img src="annotate_files/figure-html/via_faceting_auto_7_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-via_faceting-auto[ ```r dat %>% ggplot() + aes(x = year) + aes(y = price) + facet_wrap(~fruit) + geom_line(data = . %>% mutate(fruit_grey = fruit) %>% select(-fruit), aes(group = fruit_grey), color = "grey") + geom_line(size = 1.5, color = "steelblue") + * theme(strip.text = * element_text(color = "steelblue", * size = 20, * face = "bold")) ``` ] .right-panel-via_faceting-auto[ <img src="annotate_files/figure-html/via_faceting_auto_8_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-via_faceting-auto[ ```r dat %>% ggplot() + aes(x = year) + aes(y = price) + facet_wrap(~fruit) + geom_line(data = . %>% mutate(fruit_grey = fruit) %>% select(-fruit), aes(group = fruit_grey), color = "grey") + geom_line(size = 1.5, color = "steelblue") + theme(strip.text = element_text(color = "steelblue", size = 20, face = "bold")) + * theme(panel.spacing.x = * unit(2, "lines")) ``` ] .right-panel-via_faceting-auto[ <img src="annotate_files/figure-html/via_faceting_auto_9_output-1.png" width="100%" /> ] --- class: split-40 count: false .left-panel-via_faceting-auto[ ```r dat %>% ggplot() + aes(x = year) + aes(y = price) + facet_wrap(~fruit) + geom_line(data = . %>% mutate(fruit_grey = fruit) %>% select(-fruit), aes(group = fruit_grey), color = "grey") + geom_line(size = 1.5, color = "steelblue") + theme(strip.text = element_text(color = "steelblue", size = 20, face = "bold")) + theme(panel.spacing.x = unit(2, "lines")) + # overwriting above faceting specification * facet_wrap(~fct_reorder2(fruit, year, price)) ``` ] .right-panel-via_faceting-auto[ <img src="annotate_files/figure-html/via_faceting_auto_10_output-1.png" width="100%" /> ] <style> .left-panel-via_faceting-auto { color: #777; width: 59%; height: 92%; float: left; font-size: 80% } .right-panel-via_faceting-auto { width: 40%; float: right; padding-left: 1%; font-size: 80% } .middle-panel-via_faceting-auto { width: NA%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: inverse, center, middle # Interested in the toy data prep for fruit example? -- ## Uses pivot_longer() -- ### This is basically [Jenny Bryan's set up](https://gist.github.com/jennybc/847c6b43c4e35cec2e5bb30a3f38af73) --- class: split-40 count: false .left-panel-data_prep-auto[ ```r *library(tidyverse) ``` ] .right-panel-data_prep-auto[ ] --- class: split-40 count: false .left-panel-data_prep-auto[ ```r library(tidyverse) *tibble( * year = 2016:2018, * Oranges = c(4.25, 4, 5.55), * Lemons = c(4, 4.75, NA), * Grapes = c(3.25, 3.75, 4.85) *) ``` ] .right-panel-data_prep-auto[ ``` # A tibble: 3 x 4 year Oranges Lemons Grapes <int> <dbl> <dbl> <dbl> 1 2016 4.25 4 3.25 2 2017 4 4.75 3.75 3 2018 5.55 NA 4.85 ``` ] --- class: split-40 count: false .left-panel-data_prep-auto[ ```r library(tidyverse) tibble( year = 2016:2018, Oranges = c(4.25, 4, 5.55), Lemons = c(4, 4.75, NA), Grapes = c(3.25, 3.75, 4.85) ) -> *dat_wide ``` ] .right-panel-data_prep-auto[ ] --- class: split-40 count: false .left-panel-data_prep-auto[ ```r library(tidyverse) tibble( year = 2016:2018, Oranges = c(4.25, 4, 5.55), Lemons = c(4, 4.75, NA), Grapes = c(3.25, 3.75, 4.85) ) -> dat_wide *dat_wide ``` ] .right-panel-data_prep-auto[ ``` # A tibble: 3 x 4 year Oranges Lemons Grapes <int> <dbl> <dbl> <dbl> 1 2016 4.25 4 3.25 2 2017 4 4.75 3.75 3 2018 5.55 NA 4.85 ``` ] --- class: split-40 count: false .left-panel-data_prep-auto[ ```r library(tidyverse) tibble( year = 2016:2018, Oranges = c(4.25, 4, 5.55), Lemons = c(4, 4.75, NA), Grapes = c(3.25, 3.75, 4.85) ) -> dat_wide dat_wide %>% * pivot_longer( * cols = c(Oranges, Lemons, Grapes), * names_to = "fruit", * values_to = "price") ``` ] .right-panel-data_prep-auto[ ``` # A tibble: 9 x 3 year fruit price <int> <chr> <dbl> 1 2016 Oranges 4.25 2 2016 Lemons 4 3 2016 Grapes 3.25 4 2017 Oranges 4 5 2017 Lemons 4.75 6 2017 Grapes 3.75 7 2018 Oranges 5.55 8 2018 Lemons NA 9 2018 Grapes 4.85 ``` ] --- class: split-40 count: false .left-panel-data_prep-auto[ ```r library(tidyverse) tibble( year = 2016:2018, Oranges = c(4.25, 4, 5.55), Lemons = c(4, 4.75, NA), Grapes = c(3.25, 3.75, 4.85) ) -> dat_wide dat_wide %>% pivot_longer( cols = c(Oranges, Lemons, Grapes), names_to = "fruit", values_to = "price") %>% * mutate( * fruit_ABCD = factor(fruit), * fruit_sane = fct_reorder2(fruit, year, price) * ) ``` ] .right-panel-data_prep-auto[ ``` # A tibble: 9 x 5 year fruit price fruit_ABCD fruit_sane <int> <chr> <dbl> <fct> <fct> 1 2016 Oranges 4.25 Oranges Oranges 2 2016 Lemons 4 Lemons Lemons 3 2016 Grapes 3.25 Grapes Grapes 4 2017 Oranges 4 Oranges Oranges 5 2017 Lemons 4.75 Lemons Lemons 6 2017 Grapes 3.75 Grapes Grapes 7 2018 Oranges 5.55 Oranges Oranges 8 2018 Lemons NA Lemons Lemons 9 2018 Grapes 4.85 Grapes Grapes ``` ] --- class: split-40 count: false .left-panel-data_prep-auto[ ```r library(tidyverse) tibble( year = 2016:2018, Oranges = c(4.25, 4, 5.55), Lemons = c(4, 4.75, NA), Grapes = c(3.25, 3.75, 4.85) ) -> dat_wide dat_wide %>% pivot_longer( cols = c(Oranges, Lemons, Grapes), names_to = "fruit", values_to = "price") %>% mutate( fruit_ABCD = factor(fruit), fruit_sane = fct_reorder2(fruit, year, price) ) -> *dat ``` ] .right-panel-data_prep-auto[ ] <style> .left-panel-data_prep-auto { color: #777; width: 38.6138613861386%; height: 92%; float: left; font-size: 80% } .right-panel-data_prep-auto { width: 59.4059405940594%; float: right; padding-left: 1%; font-size: 80% } .middle-panel-data_prep-auto { width: 0%; float: left; padding-left: 1%; font-size: 80% } </style> --- <style type="text/css"> .remark-code{line-height: 1.5; font-size: 60%} </style>