class: titleSlide, hide_logo # Data Visualization ## Designing effective plots <br> <center><img src="data:image/png;base64,#logo.png" width="200px"/></center> --- class: left, hide_logo, hide-count ## Setup .pull-left[ **Option 1** * Download and unzip today's materials into `glhlth562/materials` * Find and open the `dataviz2_template.Rmd` file in `materials/dataviz2`. ] .pull-right[ **Option 2** * Pull updates from [github](https://github.com/ericpgreen/glhlth562) (assumed you cloned this repo previously) * Find and open the `dataviz2_template.Rmd` file in `materials/dataviz2`. ] --- class: newTopicSub, hide_logo # Labels, themes, annotations, scales --- class: left ### Let's return to our FT example <img src="data:image/png;base64,#img/ft2.png" width="70%" style="display: block; margin: auto;" /> --- class: left ### Load the data if it's not already loaded ```r covid <- read_csv("https://raw.githubusercontent.com/ericpgreen/glhlth562/main/materials/dataviz1/covid.csv") glimpse(covid) ``` ``` ## Rows: 624 ## Columns: 6 ## $ X1 <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1… ## $ Province_State <chr> "Arizona", "Arizona", "Arizona", "Arizona", "Arizona… ## $ date <date> 2020-09-01, 2020-09-02, 2020-09-03, 2020-09-04, 202… ## $ deaths_roll7 <dbl> 27.57143, 28.57143, 27.28571, 27.14286, 25.28571, 26… ## $ deaths_roll7_100k <dbl> 0.3787952, 0.3925339, 0.3748698, 0.3729072, 0.347392… ## $ label <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, … ``` --- class: left ### Here's where we left off .panelset[ .panel[.panel-name[Code] ```r # see chunk 'resume' p <- covid %>% # data ggplot(aes(x = date, y = deaths_roll7_100k, # mapping color = Province_State)) + geom_line() + # geom scale_color_manual(values = c( # theme "#eb5e8d", # pink "#0f5599" # blue )) p ``` ] .panel[.panel-name[Plot] <img src="data:image/png;base64,#dataviz2_deck_files/figure-html/resume_plot-1.png" width="70%" /> ] ] --- class: left ### Adjust the scales .panelset[ .panel[.panel-name[Code] ```r # see chunk 'scales' p <- p + scale_y_continuous(breaks = seq(____, ____, _____)) + scale_x_date(date_breaks = "_______", #date_labels = "%b", # https://stackoverflow.com/a/50116294/841405 labels = function(x) if_else(month(x) == 9 | month(x) == 1, paste(month(x, label = TRUE), "\n", year(x)), paste(month(x, label = TRUE)))) p ``` ] .panel[.panel-name[Answer] ```r # see chunk 'scales' p <- p + scale_y_continuous(breaks = seq(0, 2.4, .2)) + scale_x_date(date_breaks = "1 month", #date_labels = "%b", # https://stackoverflow.com/a/50116294/841405 labels = function(x) if_else(month(x) == 9 | month(x) == 1, paste(month(x, label = TRUE), "\n", year(x)), paste(month(x, label = TRUE)))) p ``` ] .panel[.panel-name[Plot] <img src="data:image/png;base64,#dataviz2_deck_files/figure-html/scales_plot-1.png" width="70%" /> ] ] --- class: left ### Annotate .panelset[ .panel[.panel-name[Code] ```r # see chunk 'annotate' p <- p + geom_vline(xintercept = as.Date("2020-12-20"), linetype="solid", color="black") + annotate("text", x = as.Date("2020-12-20"), y = ___, label = "US vaccine launch", hjust=1.1) + geom_segment(aes(x=as.Date("2021-03-11"), xend=as.Date("2021-03-11"), y=___, yend=___), linetype="dashed", color="#0f5599") + annotate("text", x = as.Date("2021-03-11"), y = ___, label = "NC 10% fully vaccinated", color="#0f5599", hjust=-0.1) + geom_segment(aes(x=as.Date("2021-03-08"), xend=as.Date("2021-03-08"), y=0, yend=1.4), linetype="dashed", color="#eb5e8d") + annotate("text", x = as.Date("2021-03-08"), y = 1.4, label = "___", color="#eb5e8d", hjust=-0.1) p ``` ] .panel[.panel-name[Plot] <img src="data:image/png;base64,#dataviz2_deck_files/figure-html/annotate_plot-1.png" width="70%" /> ] ] --- class: left ### Labels .panelset[ .panel[.panel-name[Code] ```r # see chunk 'label' p <- p + labs(title = "New deaths attributed to COVID-19 in North Carolina and Arizona", subtitle = "Seven-day rolling average of new deaths (per 100k)", y = NULL, x = NULL, caption = "Source: Financial Times analysis of data from the Johns Hopkins CSSE.") p ``` ] .panel[.panel-name[Plot] <img src="data:image/png;base64,#dataviz2_deck_files/figure-html/label_plot-1.png" width="70%" /> ] ] --- class: left ### Themes .panelset[ .panel[.panel-name[Code] ```r # see chunk 'theme' p <- p + theme_minimal() + theme(panel.grid.minor = element_blank(), plot.title.position = "plot", plot.title = element_text(face="bold"), legend.position = "none") p ``` ] .panel[.panel-name[Plot] <img src="data:image/png;base64,#dataviz2_deck_files/figure-html/theme_plot-1.png" width="70%" /> ] ] --- class: left # Credits Deck by Eric Green ([@ericpgreen](https://twitter.com/ericpgreen)), licensed under Creative Commons Attribution [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/) * {[`xaringan`](https://github.com/yihui/xaringan)} for slides with help from {[`xaringanExtra`](https://github.com/gadenbuie/xaringanExtra)} * [JHU CSSE COVID-19 data](https://github.com/CSSEGISandData/COVID-19)