Lecture 4

Make a plot with ggplot

Byeong-Hak Choe

SUNY Geneseo

February 3, 2025

Make a plot with ggplot

Make a plot

ggplot Themes

  • Use theme() if you want to tweak the display of an existing theme.

  • The following lists basic ggplot themes:

theme theme
theme_grey() theme_gray()
theme_bw() theme_linedraw()
theme_light() theme_dark()
theme_minimal() theme_classic()
theme_void() theme_test()

Make a plot

ggplot Themes

  • ggthemes package provides the following themes:
    • theme_economist(), theme_wsj(), theme_fivethirtyeight(), theme_gdocs(), theme_map()
  • hrbrthemes package provides the following themes:
    • theme_ipsum(), theme_ipsum_rc(), theme_ft_rc(), and more.

Make a plot

gapminder data

  • The gapminder package include the gapminder data frame.
library(gapminder)
gapminder
skim(gapminder)

Make a plot

  • First, we tell the core ggplot function what our data is and what our aesthetic mapping is:
p <- ggplot(data = gapminder)
p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y = lifeExp))
  • We can then add the geometric object (geom_*()) to the ggplot().
p + geom_point() 

Make a plot

  1. Tell the ggplot() function what our tidy data is.
  • ggplot(data = ...)
  1. Tell ggplot() what relationships we want to see.
  • The ggplot(mapping = aes(...))
  • p <- ggplot(data = ... , aes(...))
  1. Tell ggplot() how we want to see the relationships in our data.
  • Choose a geom_*().
  1. Layer on geoms as needed, by adding them to the p object one at a time.
  2. Use additional functions to adjust scales, labels, tick marks, titles, etc.

Make a plot

Build your plots layer by layer

p + geom_point() + geom_smooth() 
p + geom_point() + geom_smooth(method = "lm") 
p + geom_point() +
    geom_smooth(method = "lm")
p + geom_point() +
    geom_smooth(method = "gam")

Make a plot

Mapping aesthetics vs setting them

  • In the following, we map color to continent in the aes() function:
p1 <-  ggplot(data = gapminder,
             mapping = aes(x = gdpPercap, y = lifeExp,
                           color = continent))
  • We then add geoms.
p1 + geom_point() +
     geom_smooth(method = "lm") 

Make a plot

Mapping aesthetics vs setting them

  • In the following, we map color to "purple" outside of the aes() function:
p2 <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap, y = lifeExp))
  • We then add geoms, manually set aethetics, and then adjust scales.
p2 + geom_point(color = "purple") +
     geom_smooth(method = "lm")

Make a plot

Mapping aesthetics vs setting them

  • Here are another exmaples that set aesthetic mapping manually outside aes() function and inside geom_*() function.
p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y = lifeExp))

p + geom_point(color = "purple") +
    geom_smooth(method = "loess") 

p + geom_point(alpha = 0.3) +
    geom_smooth(color = "orange", se = F, size = 8, method = "lm") 

Make a plot

Mapping aesthetics vs setting them

  • Let’s add scale_*() and labs() functions to ggplot()
p + geom_point(alpha = 0.3) +
    geom_smooth(method = "gam") +
    scale_x_log10(labels = scales::dollar) +
    labs(x = "GDP Per Capita", y = "Life Expectancy in Years",
         title = "Economic Growth and Life Expectancy",
         subtitle = "Data points are country-years",
         caption = "Source: Gapminder.")
  • Q. Is it really the best way to display this country-year data?
    • What are we gaining and losing by ignoring the temporal and country-level structure of the data? How could we do better?

Make a plot

Aesthetics can be mapped per geom

  • We can have different aesthetic mappings across different geoms:
p <- ggplot(data = gapminder, 
            mapping = aes(x = gdpPercap, y = lifeExp))

p + geom_point(mapping = aes(color = continent)) +
    geom_smooth(method = "loess")  +
    scale_x_continuous(trans = scales::log_trans())  # natural log

Make a plot

Save your work

  • We can use ggsave() to save ggplot output as a .png or .pdf file.
ggsave(filename = "my_figure.png")
  • We can put our recent plot into an object called p_out and then tell ggave() that we want to save that object.
p_out <- p + geom_point() +
    geom_smooth(method = "loess") +
    scale_x_log10()
ggsave("my_figure.pdf", plot = p_out)

Make a plot

Save your work

  • We can set the size of ggplot figue:
ggsave("lifexp_vs_gdp_gradient.pdf",
       plot = p_out, height = 8, width = 10, units = "in")