DANL 310 Lecture 05

Byeong-Hak Choe

February 10, 2022

Make a Plot

Loading the R packages

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5     ✓ purrr   0.3.4
## ✓ tibble  3.1.6     ✓ dplyr   1.0.8
## ✓ tidyr   1.1.4     ✓ stringr 1.4.0
## ✓ readr   2.1.2     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(skimr)   # a better summary of data frame
library(scales)  # scales for ggplot
## 
## Attaching package: 'scales'
## The following object is masked from 'package:purrr':
## 
##     discard
## The following object is masked from 'package:readr':
## 
##     col_factor

Setting the theme for ggplot

theme_set(theme_minimal()) # setting the minimal theme for ggplot
  • Use theme() if you want to tweak the display of an existing theme.

  • The following lists the ggplot themes:

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

gapminder data

  • The gapminder package include the gapminder data frame.
library(gapminder)
gapminder
## # A tibble: 1,704 × 6
##    country     continent  year lifeExp      pop gdpPercap
##    <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
##  1 Afghanistan Asia       1952    28.8  8425333      779.
##  2 Afghanistan Asia       1957    30.3  9240934      821.
##  3 Afghanistan Asia       1962    32.0 10267083      853.
##  4 Afghanistan Asia       1967    34.0 11537966      836.
##  5 Afghanistan Asia       1972    36.1 13079460      740.
##  6 Afghanistan Asia       1977    38.4 14880372      786.
##  7 Afghanistan Asia       1982    39.9 12881816      978.
##  8 Afghanistan Asia       1987    40.8 13867957      852.
##  9 Afghanistan Asia       1992    41.7 16317921      649.
## 10 Afghanistan Asia       1997    41.8 22227415      635.
## # … with 1,694 more rows

Descriptive statistics

skim(gapminder)

Mappings link data to things you see

Mappings link data to things you see

Mappings link data to things you see

Build your plots layer by layer

p + geom_smooth()

Build your plots layer by layer

p + geom_point() + geom_smooth() 

Build your plots layer by layer

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

Build your plots layer by layer

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

Build your plots layer by layer

p + geom_point() +
    geom_smooth(method = "gam") +
    scale_x_log10(labels = scales::dollar)

Mapping aesthetics vs setting them

p <-  ggplot(data = gapminder,
             mapping = aes(x = gdpPercap, y = lifeExp,
                           color = continent))
p

Mapping aesthetics vs setting them

p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap, y = lifeExp,
                          color = "purple"))
p + geom_point() +
    geom_smooth(method = "loess") +
    scale_x_log10()

Mapping aesthetics vs setting them

Mapping aesthetics vs setting them

p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y = lifeExp))
p + geom_point(color = "purple") +
    geom_smooth(method = "loess") +
    scale_x_log10()

Mapping aesthetics vs setting them

Mapping aesthetics vs setting them

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

Mapping aesthetics vs setting them

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.")

Mapping aesthetics vs setting them

Mapping aesthetics vs setting them

p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y = lifeExp,
                          color = continent))
p + geom_point() +
    geom_smooth(method = "loess") +
    scale_x_log10()

Mapping aesthetics vs setting them

Mapping aesthetics vs setting them

p <- ggplot(data = gapminder,
            mapping = aes(x = gdpPercap,
                          y = lifeExp,
                          color = continent,
                          fill = continent))
p + geom_point() +
    geom_smooth(method = "loess") +
    scale_x_log10()

Mapping aesthetics vs setting them

Aesthetics can be mapped per geom

p <- ggplot(data = gapminder, 
            mapping = aes(x = gdpPercap, y = lifeExp))

p + geom_point(mapping = aes(color = continent)) +
    geom_smooth(method = "loess") +
    scale_x_log10()

Aesthetics can be mapped per geom

Aesthetics can be mapped per geom

p + geom_point(mapping = aes(color = log(pop))) +
      scale_x_continuous(trans = scales::log_trans())  # natural log scale

Save your work

knitr::opts_chunk$set(fig.width=8, fig.height=5) 

Save your work

p + geom_point()

Save your work

ggsave(filename = "my_figure.png")

Save your work

ggsave(filename = "my_figure.pdf")

Save your work

p_out <- p + geom_point() +
    geom_smooth(method = "loess") +
    scale_x_log10()
ggsave("my_figure.pdf", plot = p_out)

Save your work

library(here)
ggsave(here("figures", "lifexp_vs_gdp_gradient.pdf"), plot = p_out)
  • here() starts at the directory where the project is created.

  • Create the “figures” folder in the directory where the project is created.

Save your work

ggsave("lifexp_vs_gdp_gradient.pdf",
       plot = p_out, height = 8, width = 10, units = "in")

Show the right number

Grouped data and the “group” aesthetic

p <- ggplot(data = gapminder,
            mapping = aes(x = year,
                          y = gdpPercap))
p + geom_line() 

Grouped data and the “group” aesthetic

p + geom_line(aes(group=country)) 

Facet to make small multiples

p + geom_line(aes(group = country)) + facet_wrap(~ continent)

Facet to make small multiples

p + geom_line(color="gray70", aes(group = country)) +
    geom_smooth(size = 1.1, method = "loess", se = FALSE) +
    scale_y_log10(labels=scales::dollar) +
    facet_wrap(~ continent, ncol = 5) +
    labs(x = "Year", y = "GDP per capita",
         title = "GDP per capita on Five Continents")

Facet to make small multiples

Facet to make small multiples

p + geom_line(color="gray70", aes(group = country)) +
    geom_smooth(size = 1.1, method = "loess", se = FALSE) +
    scale_y_log10(labels=scales::dollar) +
    facet_wrap(~ continent, ncol = 5) +
    labs(x = "Year", y = "GDP per capita",
         title = "GDP per capita on Five Continents") + 
    theme(axis.text.x = element_text(angle = 45),
          axis.title.x = element_text(margin = margin(t = 25)))

Facet to make small multiples