class: center, middle, inverse, title-slide .title[ # Data Communication ] .author[ ### MACSS 30500
University of Chicago ] --- # Agenda * Review layers + EDA * Communicating with Data * Labels * Annotations * Scales * Themes * Layout --- class: inverse, middle # Communicating with data --- ### Setup For this, we're going to work with these packages -- remember to install them if you haven't yet! ``` r library(tidyverse) library(scales) ``` ``` ## ## Attaching package: 'scales' ``` ``` ## The following object is masked from 'package:purrr': ## ## discard ``` ``` ## The following object is masked from 'package:readr': ## ## col_factor ``` ``` r library(ggrepel) library(patchwork) ``` --- ## Labels There are many ways to label your axes -- two primary ways: * `labs()`: can specify x and y [labs example](https://r4ds.hadley.nz/communication.html#labels) * `theme()`: can customize with more detail [theme example](https://ggplot2-book.org/themes.html) * note: confusingly, this isn't the same as the color theme! -- We will mostly use labs in our examples (for now ... theme is particularly helpful for rotating axis text!) --- ### Labels: examples Here are our baseline options: ``` r labs( x = "", y = "", title = waiver(), subtitle = waiver(), caption = waiver(), tag = waiver(), alt = waiver(), alt_insight = waiver() ) ``` --- ## Example: `taylor` package: Add labels to this plot's axes: .panelset[ .panel[.panel-name[Code] ``` r library(taylor) data(eras_tour_surprise) ggplot(eras_tour_surprise, aes(x = dress)) + geom_bar(stat = "count", fill = c("deepskyblue", "blue", "deeppink", "darkgreen", "tomato1", "steelblue1", "red3", "hotpink2", "chocolate3", "slateblue4", "gold2" )) ``` ] .panel[.panel-name[Plot]  (add labs(x = "Suprise Song Dress")) ] ] --- ## Example: `taylor` package: Add labels to this plot's axes: .panelset[ .panel[.panel-name[Code] ``` r ggplot(eras_tour_surprise, aes(x = forcats::fct_infreq(dress))) + geom_bar(stat = "count", fill = c("red3","gold2","darkgreen", "chocolate3" , "deeppink", "blue","slateblue4", "steelblue1", "deepskyblue", "tomato4", "hotpink2")) + labs(title = "Taylor Swift Surprise Song Dress Colors", subtitle = "Eras Tour", caption = 'source: taylor package', x = "Dress", y = "Number of times worn for performance") ``` ] .panel[.panel-name[Plot]  (advanced: map color names to dresses to avoid sort issues!) ] ] --- ### Info/guidance on titles * Book suggests only informative titles: re what is occurring * I disagree * Context and role: what is your goal and who is your audience --- ## Annotatations Sometimes you want to add additional elements to your plots! * `geom_text()` * `geom_label_repel()` * `geom_label()` * lines: `geom_hline()` and `geom_vline()` * shapes: `geom_rect()` and `geom_segment()` * additional layers: e.g. a second dataset with `geom_point()` --- ## Annotatations .panelset[ .panel[.panel-name[Code] ``` r trend_text <- "Larger engine sizes tend to have lower fuel economy." |> str_wrap(width = 30) ggplot(mpg, aes(x = displ, y = hwy)) + geom_point() + annotate( geom = "label", x = 3.5, y = 38, label = trend_text, hjust = "left", color = "red" ) + annotate( geom = "segment", x = 3, y = 35, xend = 5, yend = 25, color = "red", arrow = arrow(type = "closed") ) ``` ] .panel[.panel-name[Plot]  ] ] --- ## Annotations example: .panelset[ .panel[.panel-name[Code] ``` r library(taylor) data(taylor_albums) taylor_albums %>% mutate(album_name = factor(album_name, c(album_levels, "Other"), labels = c(gsub("POETS DEPARTMENT", "POETS\nDEPARTMENT", album_levels), "Other"))) %>% ggplot(aes(x = album_name, y = metacritic_score, fill = album_name)) + geom_bar(stat = "identity", color = "black") + geom_text(aes(label = metacritic_score), vjust = -0.5, color = "gray15") + theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1)) + scale_fill_albums() + theme(legend.position = "none") ``` ] .panel[.panel-name[Plot]  ] ] --- ## Scales This is how your axes appear -- from their limitations to their breaks/ticks! Examples below are for continuous scales, but the same logic applies to discrete! * `scale_x_continuous()` * `scale_y_continuous()` * `scale_color_*()` * `label_*()` * e.g. `label_percent()` * `label_dollar()` * [Color Brewer](https://ggplot2.tidyverse.org/reference/scale_brewer.html) (great for pre-set colors) * e.g. `scale_color_brewer()` --- ### Scales: examples .panelset[ .panel[.panel-name[Code] ``` r outliers <- mpg %>% filter(hwy > 34 | displ > 6 | hwy < 15) ggplot(mpg, aes(x = displ, y = hwy, color = drv)) + geom_point(alpha = 0.5) + geom_label_repel(data = outliers, aes(x = displ, y = hwy, label = model ))+ geom_smooth(se = FALSE) + geom_text( data = label_info, aes(x = displ, y = hwy, label = drive_type), fontface = "bold", size = 5, nudge_y = 2 ) + scale_y_continuous(breaks = seq(15, 40, by = 5)) + theme(legend.position = "none") ``` ] .panel[.panel-name[Plot]  ] ] --- ### Scales: examples .panelset[ .panel[.panel-name[Code] ``` r taylor_album_songs %>% mutate(album_name = factor(album_name, c(album_levels, "Other"), labels = c(gsub("POETS DEPARTMENT", "POETS\nDEPARTMENT", album_levels), "Other"))) %>% ggplot(aes(x = key_name, fill = album_name)) + geom_bar(color = "black", position = "fill") + scale_y_continuous(name = "Percentage", labels = label_percent()) + theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1)) + scale_fill_albums() + theme(legend.position = "right") + labs(title = "Keys used in Taylor Swift's Albums", caption = "source: taylor package") ``` ] .panel[.panel-name[Plot]  ] ] --- ## Themes PREPARE FOR FUN!!!! -- This is how you can make your best plot! The possibilities are endless!! (Source: [ThemePark package](https://github.com/MatthewBJane/ThemePark)) .panelset[ .panel[.panel-name[Code] ``` r library(ThemePark) ggplot(barbie_box_office |> dplyr::mutate(daily=daily/1000000), aes(x = day, y = daily)) + geom_smooth(method = 'loess', color = barbie_theme_colors["dark"], fill = barbie_theme_colors["light"]) + geom_point(color = barbie_theme_colors["medium"]) + labs(title = "Barbie Scatter Plot", subtitle = "Daily box office takings", caption = "Source: www.boxofficemojo.com", x = "Days since release", y = "Box office takings (million USD)") + theme_barbie() ``` ] .panel[.panel-name[Plot]  ] ] --- ## Other themes * Built-in options (e.g. `theme_bw()`) * Outside-but-still-popular options (`ggthemes` package) * Google 'thing you like' + 'ggplot' and chances are there is a theme for you! --- ## You can really add any theme to anything! ``` r ggplot(mpg, aes(x = displ, y = hwy, color = class)) + geom_point() + labs(title = "Displ vs Hwy", x = "Highway mpg", y = "Displacement") + theme(legend.position = "none") + theme_barbie() ``` <!-- --> --- ## Layout .panelset[ .panel[.panel-name[Code] ``` r p1 <- ggplot(mpg, aes(x = displ, y = hwy, color = class)) + geom_point(show.legend = FALSE) + labs(title = "Displ vs Hwy", x = "Highway mpg", y = "Displacement") + scale_color_brewer(palette = "Dark2") p2 <- ggplot(mpg, aes(x = drv, y = hwy)) + geom_boxplot(color = "slateblue", fill = "slateblue4", alpha = 0.4) + labs(title = "Drivetrain vs Hwy mpg", y = "Highway mpg", x = "Drivetrain") p3 <- ggplot(mpg, aes(x = cty, y = hwy, color = class)) + geom_point(show.legend = FALSE) + theme(legend.position = "none") + scale_color_brewer(palette = "Dark2") + labs(title = "City vs Highway MPG", x = "City mpg", y = "Highway mpg") p4 <- ggplot(mpg, aes(x = class, y = cty, fill = class)) + geom_bar(stat = "summary", fun = "mean") + theme(legend.position = "bottom") + scale_fill_brewer(palette = "Dark2") (guide_area() / (p1 | p3) / (p2 | p4)) + plot_annotation( title = "City and highway mileage for cars with different drive trains", caption = "Source: https://fueleconomy.gov.") + plot_layout( guides = "collect", heights = c(1, 3, 2, 4) ) & theme(legend.position = "top") ``` ] .panel[.panel-name[Plot]  ] ] --- class: center, inverse, middle # PRACTICE! [go to today's repo](https://classroom.github.com/a/Abgb4rln) practice of concepts and prep for A4! --- # Recap: * Labels * Annotations * Scales * Themes * Layout