gapminder_filtered <- gapminder |>filter(year >2000)base_plot <-ggplot(data = gapminder_filtered,mapping =aes(x = gdpPercap, y = lifeExp,color = continent, size = pop )) +geom_point() +# Use dollars, and get rid of the cents part (i.e. $300 instead of $300.00)scale_x_log10(labels =label_dollar(scale_cut =cut_short_scale())) +# Format with commasscale_size_continuous(labels =label_comma()) +# Use dark 3scale_color_discrete_qualitative(palette ="Dark 3") +labs(x ="GDP per capita", y ="Life expectancy",color ="Continent", size ="Population",title ="Here's a cool title",subtitle ="And here's a neat subtitle",caption ="Source: The Gapminder Project" ) +facet_wrap(facets =vars(year)) +theme_grey()base_plot
Change the theme
base_plot +theme_minimal()
Create a custom theme
# change base font and sizetheme_pretty <-function(base_family ="Atkinson Hyperlegible", base_size =14, ...) {theme_minimal(base_family = base_family, base_size = base_size, ...) +theme(# Remove minor grid linespanel.grid.minor =element_blank(),# Bold, bigger titleplot.title =element_text(face ="bold", size =rel(1.7)),# Plain, slightly bigger subtitle that is greyplot.subtitle =element_text(face ="plain", size =rel(1.3), color ="grey70"),# Italic, smaller, grey caption that is left-alignedplot.caption =element_text(face ="italic", size =rel(0.7),color ="grey70", hjust =0 ),# Bold legend titleslegend.title =element_text(face ="bold"),# Bold, slightly larger facet titles that are left-aligned for the sake of repetitionstrip.text =element_text(face ="bold", size =rel(1.1), hjust =0),# Bold axis titlesaxis.title =element_text(face ="bold"),# Add some space above the x-axis title and make it left-alignedaxis.title.x =element_text(margin =margin(t =10), hjust =0),# Add some space to the right of the y-axis title and make it top-alignedaxis.title.y =element_text(margin =margin(r =10), hjust =1),# Add a light grey background to the facet titles, with no bordersstrip.background =element_rect(fill ="grey90", color =NA),# Add a thin grey border around all the plots to tie in the facet titlespanel.border =element_rect(color ="grey90", fill =NA) )}
library(palmerpenguins)ggplot(data =drop_na(penguins, sex),mapping =aes(x = bill_length_mm, y = body_mass_g, color =str_to_title(sex))) +geom_point(size =3, alpha =0.5) +scale_color_discrete_qualitative(palette ="Dark 3") +scale_y_continuous(labels =label_comma()) +facet_wrap(facets =vars(species)) +labs(x ="Bill length (mm)", y ="Body mass (g)", color ="Sex",title ="Gentoo penguins are the largest",subtitle ="But females are typically smaller than males",caption ="Here's a caption" ) +theme_pretty()
A note on custom fonts
Custom fonts in {ggplot2}
Rendering text and custom fonts has been historically difficult in R (and many other programming languages)
Requires unifying the graphics device, operating system, text rendering, and R
Several approaches exist with {ggplot2} to simplify this process
{ragg}
{ragg} is a package that provides a consistent way to render fonts across different devices
Install fonts directly on system and use {ragg} as the graphics device
Many great uses for this workflow! But it requires the ability to directly install fonts on the device…
ggsave() automatically uses {ragg} as of {ggplot2} 3.3.4
In Quarto, set the dev chunk option
knitr:opts_chunk:dev: ragg_png
Application exercise
ae-08
Instructions
Go to the course GitHub org and find your ae-08 (repo name will be suffixed with your GitHub name).
Clone the repo in RStudio, run renv::restore() to install the required packages, open the Quarto document in the repo, and follow along and complete the exercises.
Render, commit, and push your edits by the AE deadline – end of the day
Wrap up
Wrap up
{ggplot2} uses theming to control the visual appearance of charts
Use custom themes to apply consistent styles across multiple plots