class: left, top background-image: url(https://images.unsplash.com/photo-1454944338482-a69bb95894af?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1504&q=80) background-size: 170% # .right.large[Labeling time series <br>in {ggplot2}] <br> <br> <br> <br> <!-- ### An exploration <br>with {flipbookr}<br>and {xaringan} --> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> <br> #### Edited: Gina Reynolds<br>Photo Credit: Lauren Mancke --- Viz and Code Inspirations: - [Jenny Bryan](https://twitter.com/JennyBryan/status/1244499728831672320) - [John Burn-Murdoch](https://gist.githubusercontent.com/johnburnmurdoch/34bd7470dca92e470fd5f12a488923ce/raw/c7a66c8a718299ecfdb9b7c922daca5d33eb8aa0/coronavirus_cases_trajectories.R) - [Allison Horst](https://twitter.com/allison_horst/status/1248765119334498305) - [Hiroaki Yutani](https://yutani.rbind.io/post/gghighlight-0-2-0/) - [Malcolm Barrett](https://designing-ggplots.netlify.com/#1) - [Taming Themes in ggplot2](https://evamaerey.github.io/little_flipbooks_library/taming_themes_in_ggplot/taming_ggplot_themes.html) --- class: inverse, center, middle # Legend, ordered legend, or direct label --- class: split-40 count: false .column[.content[ ```r *dat ``` ]] .column[.content[ ``` # 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 .column[.content[ ```r dat %>% * ggplot() ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/plots_auto_2_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r dat %>% ggplot() + * aes(x = year, y = price) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/plots_auto_3_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r dat %>% ggplot() + aes(x = year, y = price) + * geom_line(size = 1.5) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/plots_auto_4_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + * aes(colour = fruit) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/plots_auto_5_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(colour = fruit) + * theme_minimal(base_size = 25) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/plots_auto_6_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(colour = fruit) + theme_minimal(base_size = 25) + * scale_x_continuous(breaks = 2016:2018) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/plots_auto_7_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(colour = fruit) + theme_minimal(base_size = 25) + scale_x_continuous(breaks = 2016:2018) + * labs(title = "Price of produce: Edible?") ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/plots_auto_8_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(colour = fruit) + theme_minimal(base_size = 25) + scale_x_continuous(breaks = 2016:2018) + labs(title = "Price of produce: Edible?") + # overwrite with a more appropriate factor ordering * aes(colour = fct_reorder2(fruit, year, price)) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/plots_auto_9_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(colour = fruit) + theme_minimal(base_size = 25) + scale_x_continuous(breaks = 2016:2018) + labs(title = "Price of produce: Edible?") + # overwrite with a more appropriate factor ordering aes(colour = fct_reorder2(fruit, year, price)) + # put the key near the data * theme(legend.justification = c(1, 0.85)) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/plots_auto_10_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(colour = fruit) + theme_minimal(base_size = 25) + scale_x_continuous(breaks = 2016:2018) + labs(title = "Price of produce: Edible?") + # overwrite with a more appropriate factor ordering aes(colour = fct_reorder2(fruit, year, price)) + # put the key near the data theme(legend.justification = c(1, 0.85)) + * labs(colour = NULL) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/plots_auto_11_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(colour = fruit) + theme_minimal(base_size = 25) + scale_x_continuous(breaks = 2016:2018) + labs(title = "Price of produce: Edible?") + # overwrite with a more appropriate factor ordering aes(colour = fct_reorder2(fruit, year, price)) + # put the key near the data theme(legend.justification = c(1, 0.85)) + labs(colour = NULL) + * labs(title = "Price of produce: Fancy") ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/plots_auto_12_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(colour = fruit) + theme_minimal(base_size = 25) + scale_x_continuous(breaks = 2016:2018) + labs(title = "Price of produce: Edible?") + # overwrite with a more appropriate factor ordering aes(colour = fct_reorder2(fruit, year, price)) + # put the key near the data theme(legend.justification = c(1, 0.85)) + labs(colour = NULL) + labs(title = "Price of produce: Fancy") + # but you might directly label the series * theme(legend.position = "none") ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/plots_auto_13_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(colour = fruit) + theme_minimal(base_size = 25) + scale_x_continuous(breaks = 2016:2018) + labs(title = "Price of produce: Edible?") + # overwrite with a more appropriate factor ordering aes(colour = fct_reorder2(fruit, year, price)) + # put the key near the data theme(legend.justification = c(1, 0.85)) + labs(colour = NULL) + labs(title = "Price of produce: Fancy") + # but you might directly label the series theme(legend.position = "none") + * aes(color = fruit) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/plots_auto_14_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(colour = fruit) + theme_minimal(base_size = 25) + scale_x_continuous(breaks = 2016:2018) + labs(title = "Price of produce: Edible?") + # overwrite with a more appropriate factor ordering aes(colour = fct_reorder2(fruit, year, price)) + # put the key near the data theme(legend.justification = c(1, 0.85)) + labs(colour = NULL) + labs(title = "Price of produce: Fancy") + # but you might directly label the series theme(legend.position = "none") + aes(color = fruit) + * 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) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/plots_auto_15_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(colour = fruit) + theme_minimal(base_size = 25) + scale_x_continuous(breaks = 2016:2018) + labs(title = "Price of produce: Edible?") + # overwrite with a more appropriate factor ordering aes(colour = fct_reorder2(fruit, year, price)) + # put the key near the data theme(legend.justification = c(1, 0.85)) + labs(colour = NULL) + labs(title = "Price of produce: Fancy") + # but you might directly label the series theme(legend.position = "none") + aes(color = fruit) + 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) + # give them a cute little end point * geom_point(data = dat %>% * group_by(fruit) %>% * drop_na() %>% * filter(year == max(year)), * aes(fill = fruit), * shape = 21, * size = 6, * color = "white") ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/plots_auto_16_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(colour = fruit) + theme_minimal(base_size = 25) + scale_x_continuous(breaks = 2016:2018) + labs(title = "Price of produce: Edible?") + # overwrite with a more appropriate factor ordering aes(colour = fct_reorder2(fruit, year, price)) + # put the key near the data theme(legend.justification = c(1, 0.85)) + labs(colour = NULL) + labs(title = "Price of produce: Fancy") + # but you might directly label the series theme(legend.position = "none") + aes(color = fruit) + 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) + # give them a cute little end point geom_point(data = dat %>% group_by(fruit) %>% drop_na() %>% filter(year == max(year)), aes(fill = fruit), shape = 21, size = 6, color = "white") + # but probably wider x axis * scale_x_continuous(breaks = 2016:2018, * expand = c(0.1,0), * limits = c(2016,2018.2)) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/plots_auto_17_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(colour = fruit) + theme_minimal(base_size = 25) + scale_x_continuous(breaks = 2016:2018) + labs(title = "Price of produce: Edible?") + # overwrite with a more appropriate factor ordering aes(colour = fct_reorder2(fruit, year, price)) + # put the key near the data theme(legend.justification = c(1, 0.85)) + labs(colour = NULL) + labs(title = "Price of produce: Fancy") + # but you might directly label the series theme(legend.position = "none") + aes(color = fruit) + 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) + # give them a cute little end point geom_point(data = dat %>% group_by(fruit) %>% drop_na() %>% filter(year == max(year)), aes(fill = fruit), shape = 21, size = 6, color = "white") + # but probably wider x axis scale_x_continuous(breaks = 2016:2018, expand = c(0.1,0), limits = c(2016,2018.2)) + * scale_y_continuous(labels = scales::dollar_format(), * expand = c(0,.15)) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/plots_auto_18_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(colour = fruit) + theme_minimal(base_size = 25) + scale_x_continuous(breaks = 2016:2018) + labs(title = "Price of produce: Edible?") + # overwrite with a more appropriate factor ordering aes(colour = fct_reorder2(fruit, year, price)) + # put the key near the data theme(legend.justification = c(1, 0.85)) + labs(colour = NULL) + labs(title = "Price of produce: Fancy") + # but you might directly label the series theme(legend.position = "none") + aes(color = fruit) + 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) + # give them a cute little end point geom_point(data = dat %>% group_by(fruit) %>% drop_na() %>% filter(year == max(year)), aes(fill = fruit), shape = 21, size = 6, color = "white") + # but probably wider x axis scale_x_continuous(breaks = 2016:2018, expand = c(0.1,0), limits = c(2016,2018.2)) + scale_y_continuous(labels = scales::dollar_format(), expand = c(0,.15)) + * labs(x = NULL, y = NULL) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/plots_auto_19_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(colour = fruit) + theme_minimal(base_size = 25) + scale_x_continuous(breaks = 2016:2018) + labs(title = "Price of produce: Edible?") + # overwrite with a more appropriate factor ordering aes(colour = fct_reorder2(fruit, year, price)) + # put the key near the data theme(legend.justification = c(1, 0.85)) + labs(colour = NULL) + labs(title = "Price of produce: Fancy") + # but you might directly label the series theme(legend.position = "none") + aes(color = fruit) + 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) + # give them a cute little end point geom_point(data = dat %>% group_by(fruit) %>% drop_na() %>% filter(year == max(year)), aes(fill = fruit), shape = 21, size = 6, color = "white") + # but probably wider x axis scale_x_continuous(breaks = 2016:2018, expand = c(0.1,0), limits = c(2016,2018.2)) + scale_y_continuous(labels = scales::dollar_format(), expand = c(0,.15)) + labs(x = NULL, y = NULL) + * theme(panel.grid.minor.x = element_blank()) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/plots_auto_20_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(colour = fruit) + theme_minimal(base_size = 25) + scale_x_continuous(breaks = 2016:2018) + labs(title = "Price of produce: Edible?") + # overwrite with a more appropriate factor ordering aes(colour = fct_reorder2(fruit, year, price)) + # put the key near the data theme(legend.justification = c(1, 0.85)) + labs(colour = NULL) + labs(title = "Price of produce: Fancy") + # but you might directly label the series theme(legend.position = "none") + aes(color = fruit) + 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) + # give them a cute little end point geom_point(data = dat %>% group_by(fruit) %>% drop_na() %>% filter(year == max(year)), aes(fill = fruit), shape = 21, size = 6, color = "white") + # but probably wider x axis scale_x_continuous(breaks = 2016:2018, expand = c(0.1,0), limits = c(2016,2018.2)) + scale_y_continuous(labels = scales::dollar_format(), expand = c(0,.15)) + labs(x = NULL, y = NULL) + theme(panel.grid.minor.x = element_blank()) + * theme(panel.grid.major.x = element_blank()) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/plots_auto_21_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(colour = fruit) + theme_minimal(base_size = 25) + scale_x_continuous(breaks = 2016:2018) + labs(title = "Price of produce: Edible?") + # overwrite with a more appropriate factor ordering aes(colour = fct_reorder2(fruit, year, price)) + # put the key near the data theme(legend.justification = c(1, 0.85)) + labs(colour = NULL) + labs(title = "Price of produce: Fancy") + # but you might directly label the series theme(legend.position = "none") + aes(color = fruit) + 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) + # give them a cute little end point geom_point(data = dat %>% group_by(fruit) %>% drop_na() %>% filter(year == max(year)), aes(fill = fruit), shape = 21, size = 6, color = "white") + # but probably wider x axis scale_x_continuous(breaks = 2016:2018, expand = c(0.1,0), limits = c(2016,2018.2)) + scale_y_continuous(labels = scales::dollar_format(), expand = c(0,.15)) + labs(x = NULL, y = NULL) + theme(panel.grid.minor.x = element_blank()) + theme(panel.grid.major.x = element_blank()) + * theme(plot.title.position = "plot") ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/plots_auto_22_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(colour = fruit) + theme_minimal(base_size = 25) + scale_x_continuous(breaks = 2016:2018) + labs(title = "Price of produce: Edible?") + # overwrite with a more appropriate factor ordering aes(colour = fct_reorder2(fruit, year, price)) + # put the key near the data theme(legend.justification = c(1, 0.85)) + labs(colour = NULL) + labs(title = "Price of produce: Fancy") + # but you might directly label the series theme(legend.position = "none") + aes(color = fruit) + 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) + # give them a cute little end point geom_point(data = dat %>% group_by(fruit) %>% drop_na() %>% filter(year == max(year)), aes(fill = fruit), shape = 21, size = 6, color = "white") + # but probably wider x axis scale_x_continuous(breaks = 2016:2018, expand = c(0.1,0), limits = c(2016,2018.2)) + scale_y_continuous(labels = scales::dollar_format(), expand = c(0,.15)) + labs(x = NULL, y = NULL) + theme(panel.grid.minor.x = element_blank()) + theme(panel.grid.major.x = element_blank()) + theme(plot.title.position = "plot") + * theme(plot.background = element_rect(fill = "grey98")) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/plots_auto_23_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(colour = fruit) + theme_minimal(base_size = 25) + scale_x_continuous(breaks = 2016:2018) + labs(title = "Price of produce: Edible?") + # overwrite with a more appropriate factor ordering aes(colour = fct_reorder2(fruit, year, price)) + # put the key near the data theme(legend.justification = c(1, 0.85)) + labs(colour = NULL) + labs(title = "Price of produce: Fancy") + # but you might directly label the series theme(legend.position = "none") + aes(color = fruit) + 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) + # give them a cute little end point geom_point(data = dat %>% group_by(fruit) %>% drop_na() %>% filter(year == max(year)), aes(fill = fruit), shape = 21, size = 6, color = "white") + # but probably wider x axis scale_x_continuous(breaks = 2016:2018, expand = c(0.1,0), limits = c(2016,2018.2)) + scale_y_continuous(labels = scales::dollar_format(), expand = c(0,.15)) + labs(x = NULL, y = NULL) + theme(panel.grid.minor.x = element_blank()) + theme(panel.grid.major.x = element_blank()) + theme(plot.title.position = "plot") + theme(plot.background = element_rect(fill = "grey98")) + * theme(axis.line.x = element_line(color = "grey20")) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/plots_auto_24_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(colour = fruit) + theme_minimal(base_size = 25) + scale_x_continuous(breaks = 2016:2018) + labs(title = "Price of produce: Edible?") + # overwrite with a more appropriate factor ordering aes(colour = fct_reorder2(fruit, year, price)) + # put the key near the data theme(legend.justification = c(1, 0.85)) + labs(colour = NULL) + labs(title = "Price of produce: Fancy") + # but you might directly label the series theme(legend.position = "none") + aes(color = fruit) + 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) + # give them a cute little end point geom_point(data = dat %>% group_by(fruit) %>% drop_na() %>% filter(year == max(year)), aes(fill = fruit), shape = 21, size = 6, color = "white") + # but probably wider x axis scale_x_continuous(breaks = 2016:2018, expand = c(0.1,0), limits = c(2016,2018.2)) + scale_y_continuous(labels = scales::dollar_format(), expand = c(0,.15)) + labs(x = NULL, y = NULL) + theme(panel.grid.minor.x = element_blank()) + theme(panel.grid.major.x = element_blank()) + theme(plot.title.position = "plot") + theme(plot.background = element_rect(fill = "grey98")) + theme(axis.line.x = element_line(color = "grey20")) + * theme(axis.ticks.x = element_line(color = "grey20")) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/plots_auto_25_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(colour = fruit) + theme_minimal(base_size = 25) + scale_x_continuous(breaks = 2016:2018) + labs(title = "Price of produce: Edible?") + # overwrite with a more appropriate factor ordering aes(colour = fct_reorder2(fruit, year, price)) + # put the key near the data theme(legend.justification = c(1, 0.85)) + labs(colour = NULL) + labs(title = "Price of produce: Fancy") + # but you might directly label the series theme(legend.position = "none") + aes(color = fruit) + 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) + # give them a cute little end point geom_point(data = dat %>% group_by(fruit) %>% drop_na() %>% filter(year == max(year)), aes(fill = fruit), shape = 21, size = 6, color = "white") + # but probably wider x axis scale_x_continuous(breaks = 2016:2018, expand = c(0.1,0), limits = c(2016,2018.2)) + scale_y_continuous(labels = scales::dollar_format(), expand = c(0,.15)) + labs(x = NULL, y = NULL) + theme(panel.grid.minor.x = element_blank()) + theme(panel.grid.major.x = element_blank()) + theme(plot.title.position = "plot") + theme(plot.background = element_rect(fill = "grey98")) + theme(axis.line.x = element_line(color = "grey20")) + theme(axis.ticks.x = element_line(color = "grey20")) + * labs(title = "Price of produce: Extra Fancy") ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/plots_auto_26_output-1.png" width="576" /> ]] --- class: inverse, center, middle # Faceting --- -- class: split-40 count: false .column[.content[ ```r *library(gghighlight) ``` ]] .column[.content[ ]] --- class: split-40 count: false .column[.content[ ```r library(gghighlight) *dat ``` ]] .column[.content[ ``` # 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 .column[.content[ ```r library(gghighlight) dat %>% * ggplot() ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/faceting_auto_3_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r library(gghighlight) dat %>% ggplot() + * aes(x = year) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/faceting_auto_4_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r library(gghighlight) dat %>% ggplot() + aes(x = year) + * aes(y = price) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/faceting_auto_5_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r library(gghighlight) dat %>% ggplot() + aes(x = year) + aes(y = price) + * facet_wrap(~fruit) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/faceting_auto_6_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r library(gghighlight) 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") ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/faceting_auto_7_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r library(gghighlight) 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") ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/faceting_auto_8_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r library(gghighlight) 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_minimal(base_size = 18) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/faceting_auto_9_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r library(gghighlight) 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_minimal(base_size = 18) + * scale_x_continuous(breaks = 2016:2018) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/faceting_auto_10_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r library(gghighlight) 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_minimal(base_size = 18) + scale_x_continuous(breaks = 2016:2018) + * theme(legend.position = "none") ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/faceting_auto_11_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r library(gghighlight) 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_minimal(base_size = 18) + scale_x_continuous(breaks = 2016:2018) + theme(legend.position = "none") + * theme(strip.text = * element_text(color = "steelblue", * size = 20, * face = "bold")) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/faceting_auto_12_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r library(gghighlight) 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_minimal(base_size = 18) + scale_x_continuous(breaks = 2016:2018) + theme(legend.position = "none") + theme(strip.text = element_text(color = "steelblue", size = 20, face = "bold")) + * theme(panel.spacing.x = * unit(2, "lines")) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/faceting_auto_13_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r library(gghighlight) 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_minimal(base_size = 18) + scale_x_continuous(breaks = 2016:2018) + theme(legend.position = "none") + theme(strip.text = element_text(color = "steelblue", size = 20, face = "bold")) + theme(panel.spacing.x = unit(2, "lines")) + # beyond alphabetical for faceting too * facet_wrap(~fct_reorder2(fruit, year, price)) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/faceting_auto_14_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r library(gghighlight) 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_minimal(base_size = 18) + scale_x_continuous(breaks = 2016:2018) + theme(legend.position = "none") + theme(strip.text = element_text(color = "steelblue", size = 20, face = "bold")) + theme(panel.spacing.x = unit(2, "lines")) + # beyond alphabetical for faceting too facet_wrap(~fct_reorder2(fruit, year, price)) + * labs(x = NULL, y = NULL) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/faceting_auto_15_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r library(gghighlight) 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_minimal(base_size = 18) + scale_x_continuous(breaks = 2016:2018) + theme(legend.position = "none") + theme(strip.text = element_text(color = "steelblue", size = 20, face = "bold")) + theme(panel.spacing.x = unit(2, "lines")) + # beyond alphabetical for faceting too facet_wrap(~fct_reorder2(fruit, year, price)) + labs(x = NULL, y = NULL) + * scale_y_continuous(labels = scales::dollar_format()) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/faceting_auto_16_output-1.png" width="576" /> ]] --- class: inverse, center, middle # gghighlight --- class: split-40 count: false .column[.content[ ```r *library(gghighlight) ``` ]] .column[.content[ ]] --- class: split-40 count: false .column[.content[ ```r library(gghighlight) *dat ``` ]] .column[.content[ ``` # 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 .column[.content[ ```r library(gghighlight) dat %>% * ggplot() ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/gghighlight_auto_3_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r library(gghighlight) dat %>% ggplot() + * aes(x = year, y = price) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/gghighlight_auto_4_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r library(gghighlight) dat %>% ggplot() + aes(x = year, y = price) + * geom_line(size = 1.5) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/gghighlight_auto_5_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r library(gghighlight) dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + * aes(colour = fruit) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/gghighlight_auto_6_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r library(gghighlight) dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(colour = fruit) + * theme_minimal(base_size = 25) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/gghighlight_auto_7_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r library(gghighlight) dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(colour = fruit) + theme_minimal(base_size = 25) + * scale_x_continuous(breaks = 2016:2018) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/gghighlight_auto_8_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r library(gghighlight) dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(colour = fruit) + theme_minimal(base_size = 25) + scale_x_continuous(breaks = 2016:2018) -> *g ``` ]] .column[.content[ ]] --- class: split-40 count: false .column[.content[ ```r library(gghighlight) dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(colour = fruit) + theme_minimal(base_size = 25) + scale_x_continuous(breaks = 2016:2018) -> g *g ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/gghighlight_auto_10_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r library(gghighlight) dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(colour = fruit) + theme_minimal(base_size = 25) + scale_x_continuous(breaks = 2016:2018) -> g g + * gghighlight(max(price, na.rm = T) > 5, * keep_scales = TRUE) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/gghighlight_auto_11_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r library(gghighlight) dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(colour = fruit) + theme_minimal(base_size = 25) + scale_x_continuous(breaks = 2016:2018) -> g g + gghighlight(max(price, na.rm = T) > 5, keep_scales = TRUE) -> *g2 ``` ]] .column[.content[ ]] --- class: split-40 count: false .column[.content[ ```r library(gghighlight) dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(colour = fruit) + theme_minimal(base_size = 25) + scale_x_continuous(breaks = 2016:2018) -> g g + gghighlight(max(price, na.rm = T) > 5, keep_scales = TRUE) -> g2 *g ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/gghighlight_auto_13_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r library(gghighlight) dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(colour = fruit) + theme_minimal(base_size = 25) + scale_x_continuous(breaks = 2016:2018) -> g g + gghighlight(max(price, na.rm = T) > 5, keep_scales = TRUE) -> g2 g + * gghighlight(max(price, na.rm = T) > 3, * keep_scales = TRUE) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/gghighlight_auto_14_output-1.png" width="576" /> ]] --- class: split-40 count: false .column[.content[ ```r library(gghighlight) dat %>% ggplot() + aes(x = year, y = price) + geom_line(size = 1.5) + aes(colour = fruit) + theme_minimal(base_size = 25) + scale_x_continuous(breaks = 2016:2018) -> g g + gghighlight(max(price, na.rm = T) > 5, keep_scales = TRUE) -> g2 g + gghighlight(max(price, na.rm = T) > 3, keep_scales = TRUE) ``` ]] .column[.content[ <img src="labeling_time_series_files/figure-html/gghighlight_auto_15_output-1.png" width="576" /> ]] --- class: inverse, center, middle # Interested in the toy data prep? -- ## Uses the newish pivot_longer()! -- ### This is basically [Jenny Bryan's set up](https://gist.github.com/jennybc/847c6b43c4e35cec2e5bb30a3f38af73) --- class: split-40 count: false .column[.content[ ```r *library(tidyverse) ``` ]] .column[.content[ ]] --- class: split-40 count: false .column[.content[ ```r library(tidyverse) *library(patchwork) ``` ]] .column[.content[ ]] --- class: split-40 count: false .column[.content[ ```r library(tidyverse) library(patchwork) *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) *) ``` ]] .column[.content[ ``` # 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 .column[.content[ ```r library(tidyverse) library(patchwork) 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 ``` ]] .column[.content[ ]] --- class: split-40 count: false .column[.content[ ```r library(tidyverse) library(patchwork) 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 ``` ]] .column[.content[ ``` # 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 .column[.content[ ```r library(tidyverse) library(patchwork) 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") ``` ]] .column[.content[ ``` # 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 .column[.content[ ```r library(tidyverse) library(patchwork) 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) * ) ``` ]] .column[.content[ ``` # 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 .column[.content[ ```r library(tidyverse) library(patchwork) 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 ``` ]] .column[.content[ ]] --- <style type="text/css"> .remark-code{line-height: 1.5; font-size: 45%} </style>