# load packagesif(!require(pacman))install.packages("pacman")pacman::p_load(countdown, tidyverse, colorspace, fs, palmerpenguins, scales, openintro, gghighlight, glue)# set theme for ggplot2ggplot2::theme_set(ggplot2::theme_minimal(base_size =14))# set width of code outputoptions(width =65)# set figure parameters for knitrknitr::opts_chunk$set(fig.width =7, # 7" widthfig.asp =0.618, # the golden ratiofig.retina =3, # dpi multiplier for displaying HTML output on retinafig.align ="center", # center align figuresdpi =300# higher dpi, sharper image)
Themes
Complete themes
p <-ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g)) +geom_point()p +theme_gray() +labs(title ="Gray")p +theme_void() +labs(title ="Void")p +theme_dark() +labs(title ="Dark")
p +labs(title ="Palmer penguins") +theme(plot.title =element_text(color ="red", face ="bold", family ="Comic Sans MS"),plot.background =element_rect(color ="red", fill ="mistyrose") )
Axes
Axis breaks
How can the following figure be improved with custom breaks in axes, if at all?
Context matters
pac_plot +scale_x_continuous(breaks =seq(from =2000, to =2022, by =2))
sf <- sf |>mutate(year =year(date),day_of_year =yday(date) )sf <- sf |>mutate(year =year(date),day_of_year =yday(date) )sf <- sf |>mutate(year =year(date),day_of_year =yday(date) )
ggplot(sf, aes(x = day_of_year, y = aqi_value, group = year)) +geom_line()
Plot AQI over years
ggplot(sf, aes(x = day_of_year, y = aqi_value, group = year, color = year)) +geom_line()
Plot AQI over years
ggplot(sf, aes(x = day_of_year, y = aqi_value, group = year, color =factor(year))) +geom_line()
Highlight 2016
ggplot(sf, aes(x = day_of_year, y = aqi_value, group = year)) +geom_line(color ="gray") +geom_line(data = sf |>filter(year ==2016), color ="red") +labs(title ="AQI levels in SF in 2016",subtitle ="Versus all years 2016 - 2022",x ="Day of year", y ="AQI value" )ggplot(sf, aes(x = day_of_year, y = aqi_value, group = year)) +geom_line(color ="gray") +geom_line(data = sf |>filter(year ==2016), color ="red") +labs(title ="AQI levels in SF in 2016",subtitle ="Versus all years 2016 - 2022",x ="Day of year", y ="AQI value" )
Highlight 2017
ggplot(sf, aes(x = day_of_year, y = aqi_value, group = year)) +geom_line(color ="gray") +geom_line(data = sf |>filter(year ==2017), color ="red") +labs(title ="AQI levels in SF in 2017",subtitle ="Versus all years 2016 - 2022",x ="Day of year", y ="AQI value" )
Highlight 2018
ggplot(sf, aes(x = day_of_year, y = aqi_value, group = year)) +geom_line(color ="gray") +geom_line(data = sf |>filter(year ==2018), color ="red") +labs(title ="AQI levels in SF in 2018",subtitle ="Versus all years 2016 - 2022",x ="Day of year", y ="AQI value" )
Highlight any year
year_to_highlight <-2018ggplot(sf, aes(x = day_of_year, y = aqi_value, group = year)) +geom_line(color ="gray") +geom_line(data = sf |>filter(year == year_to_highlight), color ="red") +labs(title =glue("AQI levels in SF in {year_to_highlight}"),subtitle ="Versus all years 2016 - 2022",x ="Day of year", y ="AQI value" )year_to_highlight <-2018ggplot(sf, aes(x = day_of_year, y = aqi_value, group = year)) +geom_line(color ="gray") +geom_line(data = sf |>filter(year == year_to_highlight), color ="red") +labs(title =glue("AQI levels in SF in {year_to_highlight}"),subtitle ="Versus all years 2016 - 2022",x ="Day of year", y ="AQI value" )year_to_highlight <-2018ggplot(sf, aes(x = day_of_year, y = aqi_value, group = year)) +geom_line(color ="gray") +geom_line(data = sf |>filter(year == year_to_highlight), color ="red") +labs(title =glue("AQI levels in SF in {year_to_highlight}"),subtitle ="Versus all years 2016 - 2022",x ="Day of year", y ="AQI value" )year_to_highlight <-2018ggplot(sf, aes(x = day_of_year, y = aqi_value, group = year)) +geom_line(color ="gray") +geom_line(data = sf |>filter(year == year_to_highlight), color ="red") +labs(title =glue("AQI levels in SF in {year_to_highlight}"),subtitle ="Versus all years 2016 - 2022",x ="Day of year", y ="AQI value" )
Highlight with gghighlight
Code
year_to_highlight <-2018sf |>ungroup() |>ggplot(aes(x = day_of_year, y = aqi_value, group = year)) +geom_line(color ="red") +gghighlight(year ==2018, use_direct_label =FALSE) +labs(title =glue("AQI levels in SF in {year_to_highlight}"),subtitle ="Versus all years 2016 - 2022",x ="Day of year", y ="AQI value" ) +theme_minimal(base_size =12) +theme(legend.position ="none") # Hide the legend