10 Lessons in Data Visualization

CSSCR Short Workshop, Fall 2025

Brian Leung

Consultant, Center for Social Science Computation and Research

Ph.D. Candidate, Department of Political Science

University of Washington

Preview

  • Developed during my TA-ship for Chris Adolph’s CS&SS 569

  • Based on ggplot2 and tidyverse

  • Code and materials available on my personal website

  • Not going to cover every method or data

  • But should be generalizable enough to better your vis.

Preview

  • Key idea: data vis. is about storytelling

  • How to tell a good story?

    • Get rid of extraneous and distracting details

    • Provide enough context to situate the story

    • Accentuate interesting or surprising patterns

  • Ask yourself: What is the main takeaway with a graph for readers?

10 lessons

  • Get rid of things
    1. Start with minimalist aesthetics
    2. Get rid of excessive ink or space
    3. Replace bad labels
  • Accentuate patterns
    1. Choose nice colors
    2. Use small multiples
    3. Diagonalize (sort) whenever possible
  • Provide context
    1. Highlight observations
    2. Juxtapose observations
    3. Present uncertainty
  • Last but not least
    1. Save graphics with code

Set up

Gapminder data

## Install pacakges
# install.packages(c("tidyverse", "gapminder", "scales", "RColorBrewer", "ggrepel", "ggforce", "modelsummary"))

## Load and inspect data from gapminder:
library(tidyverse)
library(gapminder)

gapminder %>%
  print(n = 100)
# 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.
 11 Afghanistan Asia       2002    42.1 25268405      727.
 12 Afghanistan Asia       2007    43.8 31889923      975.
 13 Albania     Europe     1952    55.2  1282697     1601.
 14 Albania     Europe     1957    59.3  1476505     1942.
 15 Albania     Europe     1962    64.8  1728137     2313.
 16 Albania     Europe     1967    66.2  1984060     2760.
 17 Albania     Europe     1972    67.7  2263554     3313.
 18 Albania     Europe     1977    68.9  2509048     3533.
 19 Albania     Europe     1982    70.4  2780097     3631.
 20 Albania     Europe     1987    72    3075321     3739.
 21 Albania     Europe     1992    71.6  3326498     2497.
 22 Albania     Europe     1997    73.0  3428038     3193.
 23 Albania     Europe     2002    75.7  3508512     4604.
 24 Albania     Europe     2007    76.4  3600523     5937.
 25 Algeria     Africa     1952    43.1  9279525     2449.
 26 Algeria     Africa     1957    45.7 10270856     3014.
 27 Algeria     Africa     1962    48.3 11000948     2551.
 28 Algeria     Africa     1967    51.4 12760499     3247.
 29 Algeria     Africa     1972    54.5 14760787     4183.
 30 Algeria     Africa     1977    58.0 17152804     4910.
 31 Algeria     Africa     1982    61.4 20033753     5745.
 32 Algeria     Africa     1987    65.8 23254956     5681.
 33 Algeria     Africa     1992    67.7 26298373     5023.
 34 Algeria     Africa     1997    69.2 29072015     4797.
 35 Algeria     Africa     2002    71.0 31287142     5288.
 36 Algeria     Africa     2007    72.3 33333216     6223.
 37 Angola      Africa     1952    30.0  4232095     3521.
 38 Angola      Africa     1957    32.0  4561361     3828.
 39 Angola      Africa     1962    34    4826015     4269.
 40 Angola      Africa     1967    36.0  5247469     5523.
 41 Angola      Africa     1972    37.9  5894858     5473.
 42 Angola      Africa     1977    39.5  6162675     3009.
 43 Angola      Africa     1982    39.9  7016384     2757.
 44 Angola      Africa     1987    39.9  7874230     2430.
 45 Angola      Africa     1992    40.6  8735988     2628.
 46 Angola      Africa     1997    41.0  9875024     2277.
 47 Angola      Africa     2002    41.0 10866106     2773.
 48 Angola      Africa     2007    42.7 12420476     4797.
 49 Argentina   Americas   1952    62.5 17876956     5911.
 50 Argentina   Americas   1957    64.4 19610538     6857.
 51 Argentina   Americas   1962    65.1 21283783     7133.
 52 Argentina   Americas   1967    65.6 22934225     8053.
 53 Argentina   Americas   1972    67.1 24779799     9443.
 54 Argentina   Americas   1977    68.5 26983828    10079.
 55 Argentina   Americas   1982    69.9 29341374     8998.
 56 Argentina   Americas   1987    70.8 31620918     9140.
 57 Argentina   Americas   1992    71.9 33958947     9308.
 58 Argentina   Americas   1997    73.3 36203463    10967.
 59 Argentina   Americas   2002    74.3 38331121     8798.
 60 Argentina   Americas   2007    75.3 40301927    12779.
 61 Australia   Oceania    1952    69.1  8691212    10040.
 62 Australia   Oceania    1957    70.3  9712569    10950.
 63 Australia   Oceania    1962    70.9 10794968    12217.
 64 Australia   Oceania    1967    71.1 11872264    14526.
 65 Australia   Oceania    1972    71.9 13177000    16789.
 66 Australia   Oceania    1977    73.5 14074100    18334.
 67 Australia   Oceania    1982    74.7 15184200    19477.
 68 Australia   Oceania    1987    76.3 16257249    21889.
 69 Australia   Oceania    1992    77.6 17481977    23425.
 70 Australia   Oceania    1997    78.8 18565243    26998.
 71 Australia   Oceania    2002    80.4 19546792    30688.
 72 Australia   Oceania    2007    81.2 20434176    34435.
 73 Austria     Europe     1952    66.8  6927772     6137.
 74 Austria     Europe     1957    67.5  6965860     8843.
 75 Austria     Europe     1962    69.5  7129864    10751.
 76 Austria     Europe     1967    70.1  7376998    12835.
 77 Austria     Europe     1972    70.6  7544201    16662.
 78 Austria     Europe     1977    72.2  7568430    19749.
 79 Austria     Europe     1982    73.2  7574613    21597.
 80 Austria     Europe     1987    74.9  7578903    23688.
 81 Austria     Europe     1992    76.0  7914969    27042.
 82 Austria     Europe     1997    77.5  8069876    29096.
 83 Austria     Europe     2002    79.0  8148312    32418.
 84 Austria     Europe     2007    79.8  8199783    36126.
 85 Bahrain     Asia       1952    50.9   120447     9867.
 86 Bahrain     Asia       1957    53.8   138655    11636.
 87 Bahrain     Asia       1962    56.9   171863    12753.
 88 Bahrain     Asia       1967    59.9   202182    14805.
 89 Bahrain     Asia       1972    63.3   230800    18269.
 90 Bahrain     Asia       1977    65.6   297410    19340.
 91 Bahrain     Asia       1982    69.1   377967    19211.
 92 Bahrain     Asia       1987    70.8   454612    18524.
 93 Bahrain     Asia       1992    72.6   529491    19036.
 94 Bahrain     Asia       1997    73.9   598561    20292.
 95 Bahrain     Asia       2002    74.8   656397    23404.
 96 Bahrain     Asia       2007    75.6   708573    29796.
 97 Bangladesh  Asia       1952    37.5 46886859      684.
 98 Bangladesh  Asia       1957    39.3 51365468      662.
 99 Bangladesh  Asia       1962    41.2 56839289      686.
100 Bangladesh  Asia       1967    43.5 62821884      721.
# ℹ 1,604 more rows

Basic ggplot2

## Establish `aes`thetics mappings between
## variables and visual properties:
ggplot(
  data = gapminder,
  )

Basic ggplot2

## Establish `aes`thetics mappings between
## variables and visual properties:
ggplot(
  data = gapminder,
  mapping = aes(
      x = gdpPercap,
    )
  )

Basic ggplot2

## Establish `aes`thetics mappings between
## variables and visual properties:
ggplot(
  data = gapminder,
  mapping = aes(
      x = gdpPercap,
      y = lifeExp
    )
  )

Basic ggplot2

## Plot them in specific `geom_` layers:
ggplot(
  data = gapminder,
  mapping = aes(
     x = gdpPercap,
     y = lifeExp
    )
  ) +
  geom_point()

Basic ggplot2

## You can technically map many variables
## But the curse of dimensionality!
ggplot(
  data = gapminder,
  mapping = aes(
    x = gdpPercap,
    y = lifeExp,
    )
  ) +
  geom_point()

Basic ggplot2

## You can technically map many variables
## But the curse of dimensionality!
ggplot(
  data = gapminder,
  mapping = aes(
    x = gdpPercap,
    y = lifeExp,
    color = continent,
    )
  ) +
  geom_point()

Basic ggplot2

## You can technically map many variables
## But the curse of dimensionality!
ggplot(
  data = gapminder,
  mapping = aes(
    x = gdpPercap,
    y = lifeExp,
    color = continent,
    shape = continent,
    )
  ) +
  geom_point()

Basic ggplot2

## You can technically map many variables
## But the curse of dimensionality!
ggplot(
  data = gapminder,
  mapping = aes(
    x = gdpPercap,
    y = lifeExp,
    color = continent,
    shape = continent,
    size = log(pop),
    )
  ) +
  geom_point()

Basic ggplot2

## You can technically map many variables
## But the curse of dimensionality!
ggplot(
  data = gapminder,
  mapping = aes(
    x = gdpPercap,
    y = lifeExp,
    color = continent,
    shape = continent,
    size = log(pop),
    alpha = year
    )
  ) +
  geom_point()

Basic ggplot2

## Simpler syntax
ggplot(gapminder,
       aes(x = gdpPercap,
           y = lifeExp,
           color = continent)) +
  geom_point()

Lesson 1: Start with minimalist aesthetics

Lesson 1: Start with minimalist aesthetics

ggplot(gapminder,
       aes(x = gdpPercap,
           y = lifeExp,
           color = continent)) +
geom_point()

Lesson 1: Start with minimalist aesthetics

ggplot(gapminder,
       aes(x = gdpPercap,
           y = lifeExp,
           color = continent)) +
geom_point() +
theme(
  panel.background = element_rect(fill = "white"),
)

Lesson 1: Start with minimalist aesthetics

ggplot(gapminder,
       aes(x = gdpPercap,
           y = lifeExp,
           color = continent)) +
geom_point() +
theme(
  panel.background = element_rect(fill = "white"),
  aspect.ratio = 1/1.618,
)

Lesson 1: Start with minimalist aesthetics

ggplot(gapminder,
       aes(x = gdpPercap,
           y = lifeExp,
           color = continent)) +
geom_point() +
theme(
  panel.background = element_rect(fill = "white"),
  aspect.ratio = 1/1.618,
  axis.line.x.bottom = element_line(linewidth = 0.2),
)

Lesson 1: Start with minimalist aesthetics

ggplot(gapminder,
       aes(x = gdpPercap,
           y = lifeExp,
           color = continent)) +
geom_point() +
theme(
  panel.background = element_rect(fill = "white"),
  aspect.ratio = 1/1.618,
  axis.line.x.bottom = element_line(linewidth = 0.2),
  panel.grid.major.y = element_line(color = "gray75", linewidth = 0.1),
)

Lesson 1: Start with minimalist aesthetics

ggplot(gapminder,
       aes(x = gdpPercap,
           y = lifeExp,
           color = continent)) +
geom_point() +
theme(
  panel.background = element_rect(fill = "white"),
  aspect.ratio = 1/1.618,
  axis.line.x.bottom = element_line(linewidth = 0.2),
  panel.grid.major.y = element_line(color = "gray75", linewidth = 0.1),
  panel.grid.major.x = element_line(color = "gray75", linewidth = 0.1)
)

Lesson 1: Start with minimalist aesthetics

## A customized theme
url <- "https://raw.githubusercontent.com/brianleungkp/brianleungkp.github.io/refs/heads/main/datavis/theme_cavis.R"
source(url)

ggplot(gapminder,
       aes(x = gdpPercap,
           y = lifeExp,
           color = continent)) +
  geom_point() +
  theme_cavis

Lesson 1: Start with minimalist aesthetics

## A customized theme
url <- "https://raw.githubusercontent.com/brianleungkp/brianleungkp.github.io/refs/heads/main/datavis/theme_cavis.R"
source(url)

ggplot(gapminder,
       aes(x = gdpPercap,
           y = lifeExp,
           color = continent)) +
  geom_point() +
  theme_cavis_hgrid

Lesson 1: Start with minimalist aesthetics

## A customized theme
url <- "https://raw.githubusercontent.com/brianleungkp/brianleungkp.github.io/refs/heads/main/datavis/theme_cavis.R"
source(url)

ggplot(gapminder,
       aes(x = gdpPercap,
           y = lifeExp,
           color = continent)) +
  geom_point() +
  theme_cavis_vgrid

Lesson 1: Start with minimalist aesthetics

## A customized theme
url <- "https://raw.githubusercontent.com/brianleungkp/brianleungkp.github.io/refs/heads/main/datavis/theme_cavis.R"
source(url)

ggplot(gapminder,
       aes(x = gdpPercap,
           y = lifeExp,
           color = continent)) +
  geom_point() +
  theme_cavis_grids

Lesson 1: Start with minimalist aesthetics

## Or from ggplot2
ggplot(gapminder,
       aes(x = gdpPercap,
           y = lifeExp,
           color = continent))

Lesson 1: Start with minimalist aesthetics

## Or from ggplot2
ggplot(gapminder,
       aes(x = gdpPercap,
           y = lifeExp,
           color = continent)) +
geom_point()

Lesson 1: Start with minimalist aesthetics

## Or from ggplot2
ggplot(gapminder,
       aes(x = gdpPercap,
           y = lifeExp,
           color = continent)) +
geom_point() +
theme_minimal(base_family = "Roboto Condensed")

Lesson 2: Get rid of excessive space or ink

Lesson 2: Get rid of excessive space or ink

ggplot(gapminder,
       aes(x = gdpPercap,
           y = lifeExp,
           color = continent))

Lesson 2: Get rid of excessive space or ink

ggplot(gapminder,
       aes(x = gdpPercap,
           y = lifeExp,
           color = continent)) +
  geom_point()

Lesson 2: Get rid of excessive space or ink

ggplot(gapminder,
       aes(x = gdpPercap,
           y = lifeExp,
           color = continent)) +
  geom_point() +
  theme_cavis_grids

Lesson 2: Get rid of excessive space or ink

ggplot(gapminder,
       aes(x = gdpPercap,
           y = lifeExp,
           color = continent)) +
  geom_point() +
  theme_cavis_grids +
  scale_x_continuous(trans = "log")

Lesson 2: Get rid of excessive space or ink

ggplot(gapminder,
       aes(x = gdpPercap,
           y = lifeExp,
           color = continent)) +
  geom_point() +
  theme_cavis_grids +
  scale_x_continuous(trans = "log") +
  theme(plot.background = element_rect(color = "black", linewidth = 1, linetype = 3))

Lesson 2: Get rid of excessive space or ink

ggplot(gapminder,
       aes(x = gdpPercap,
           y = lifeExp,
           color = continent)) +
  geom_point() +
  theme_cavis_grids +
  scale_x_continuous(trans = "log") +
  theme(legend.position = "top", plot.background = element_rect(color = "black", linewidth = 1, linetype = 3))

Lesson 2: Get rid of excessive space or ink

ggplot(gapminder,
       aes(x = gdpPercap,
           y = lifeExp,
           color = continent)) +
  geom_point() +
  theme_cavis_grids +
  scale_x_continuous(trans = "log") +
  theme(legend.position = "bottom")

Lesson 2: Get rid of excessive space or ink

ggplot(gapminder,
       aes(x = gdpPercap,
           y = lifeExp,
           color = continent)) +
  geom_point() +
  theme_cavis_grids +
  scale_x_continuous(trans = "log") +
  theme(legend.position = "top")

Lesson 2: Get rid of excessive space or ink

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.
# ℹ 1,694 more rows

Lesson 2: Get rid of excessive space or ink

gapminder %>%
  group_by(country, continent)
# A tibble: 1,704 × 6
# Groups:   country, continent [142]
   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.
# ℹ 1,694 more rows

Lesson 2: Get rid of excessive space or ink

gapminder %>%
  group_by(country, continent) %>%
  summarize(lifeExp_mean = mean(lifeExp))
# A tibble: 142 × 3
# Groups:   country [142]
   country     continent lifeExp_mean
   <fct>       <fct>            <dbl>
 1 Afghanistan Asia              37.5
 2 Albania     Europe            68.4
 3 Algeria     Africa            59.0
 4 Angola      Africa            37.9
 5 Argentina   Americas          69.1
 6 Australia   Oceania           74.7
 7 Austria     Europe            73.1
 8 Bahrain     Asia              65.6
 9 Bangladesh  Asia              49.8
10 Belgium     Europe            73.6
# ℹ 132 more rows

Lesson 2: Get rid of excessive space or ink

gapminder %>%
  group_by(country, continent) %>%
  summarize(lifeExp_mean = mean(lifeExp)) %>%
  ungroup()
# A tibble: 142 × 3
   country     continent lifeExp_mean
   <fct>       <fct>            <dbl>
 1 Afghanistan Asia              37.5
 2 Albania     Europe            68.4
 3 Algeria     Africa            59.0
 4 Angola      Africa            37.9
 5 Argentina   Americas          69.1
 6 Australia   Oceania           74.7
 7 Austria     Europe            73.1
 8 Bahrain     Asia              65.6
 9 Bangladesh  Asia              49.8
10 Belgium     Europe            73.6
# ℹ 132 more rows

Lesson 2: Get rid of excessive space or ink

gapminder %>%
  group_by(country, continent) %>%
  summarize(lifeExp_mean = mean(lifeExp)) %>%
  ungroup() %>%
  slice_sample(n = 20)
# A tibble: 20 × 3
   country     continent lifeExp_mean
   <fct>       <fct>            <dbl>
 1 Haiti       Americas          50.2
 2 Albania     Europe            68.4
 3 Mexico      Americas          65.4
 4 Syria       Asia              61.3
 5 Myanmar     Asia              53.3
 6 Liberia     Africa            42.5
 7 Philippines Asia              61.0
 8 Canada      Americas          74.9
 9 Cameroon    Africa            48.1
10 Belgium     Europe            73.6
11 Mauritania  Africa            52.3
12 Nepal       Asia              49.0
13 Sudan       Africa            48.4
14 Korea, Rep. Asia              65.0
15 Costa Rica  Americas          70.2
16 Sri Lanka   Asia              66.5
17 Spain       Europe            74.2
18 Honduras    Americas          57.9
19 China       Asia              61.8
20 Israel      Asia              73.6

Lesson 2: Get rid of excessive space or ink

gapminder %>%
  group_by(country, continent) %>%
  summarize(lifeExp_mean = mean(lifeExp)) %>%
  ungroup() %>%
  slice_sample(n = 20) ->
  lifeExp_mean_sample

Lesson 2: Get rid of excessive space or ink

ggplot(lifeExp_mean_sample,
       aes(y = fct_reorder(country, lifeExp_mean),
           x = lifeExp_mean)) +
  geom_col(
  )

Lesson 2: Get rid of excessive space or ink

ggplot(lifeExp_mean_sample,
       aes(y = fct_reorder(country, lifeExp_mean),
           x = lifeExp_mean)) +
  geom_col(
    aes(fill = continent)
  )

Lesson 2: Get rid of excessive space or ink

ggplot(lifeExp_mean_sample,
       aes(y = fct_reorder(country, lifeExp_mean),
           x = lifeExp_mean,
           color = continent))

Lesson 2: Get rid of excessive space or ink

ggplot(lifeExp_mean_sample,
       aes(y = fct_reorder(country, lifeExp_mean),
           x = lifeExp_mean,
           color = continent)) +
  theme_cavis_hgrid

Lesson 2: Get rid of excessive space or ink

ggplot(lifeExp_mean_sample,
       aes(y = fct_reorder(country, lifeExp_mean),
           x = lifeExp_mean,
           color = continent)) +
  theme_cavis_hgrid +
  theme(aspect.ratio = 1.618,
        legend.position = "top")

Lesson 2: Get rid of excessive space or ink

ggplot(lifeExp_mean_sample,
       aes(y = fct_reorder(country, lifeExp_mean),
           x = lifeExp_mean,
           color = continent)) +
  theme_cavis_hgrid +
  theme(aspect.ratio = 1.618,
        legend.position = "top") +
  geom_point(size = 7)

Lesson 2: Get rid of excessive space or ink

ggplot(lifeExp_mean_sample,
       aes(y = fct_reorder(country, lifeExp_mean),
           x = lifeExp_mean,
           color = continent)) +
  theme_cavis_hgrid +
  theme(aspect.ratio = 1.618,
        legend.position = "top") +
  geom_point(size = 7) +
  geom_text(aes(label = round(lifeExp_mean, 0)),
            color = "white",
            size = 3.5)

Lesson 2: Get rid of excessive space or ink

ggplot(lifeExp_mean_sample,
       aes(y = fct_reorder(country, lifeExp_mean),
           x = lifeExp_mean,
           color = continent)) +
  theme_cavis_hgrid +
  theme(aspect.ratio = 1.618,
        legend.position = "top") +
  geom_point(size = 7) +
  geom_text(aes(label = round(lifeExp_mean, 0)),
            color = "white",
            size = 3.5) +
  labs(y = NULL,
       x = "Avg. life expectancy")

Lesson 2: Get rid of excessive space or ink

ggplot(lifeExp_mean_sample,
       aes(y = fct_reorder(country, lifeExp_mean),
           x = lifeExp_mean,
           color = continent)) +
  theme_cavis_hgrid +
  theme(aspect.ratio = 1.618,
        legend.position = "top") +
  geom_point(size = 7) +
  geom_text(aes(label = round(lifeExp_mean, 0)),
            color = "white",
            size = 3.5) +
  labs(y = NULL,
       x = "Avg. life expectancy") +
  guides(
    color = guide_legend(
      override.aes = list(size = 3)
    )
  )

Lesson 3: Replace bad labels

Lesson 3: Replace bad labels

library(scales)

ggplot(gapminder,
       aes(x = gdpPercap, y = lifeExp,
           color = continent)) +
  geom_point() +
  theme_cavis_grids +
  theme(legend.position = "top")

Lesson 3: Replace bad labels

library(scales)

ggplot(gapminder,
       aes(x = gdpPercap, y = lifeExp,
           color = continent)) +
  geom_point() +
  theme_cavis_grids +
  theme(legend.position = "top") +
  scale_y_continuous(
    breaks = seq(30, 80, 10)
  )

Lesson 3: Replace bad labels

library(scales)

ggplot(gapminder,
       aes(x = gdpPercap, y = lifeExp,
           color = continent)) +
  geom_point() +
  theme_cavis_grids +
  theme(legend.position = "top") +
  scale_y_continuous(
    breaks = seq(30, 80, 10)
  ) +
  scale_x_continuous(
    trans = "log10",
  )

Lesson 3: Replace bad labels

library(scales)

ggplot(gapminder,
       aes(x = gdpPercap, y = lifeExp,
           color = continent)) +
  geom_point() +
  theme_cavis_grids +
  theme(legend.position = "top") +
  scale_y_continuous(
    breaks = seq(30, 80, 10)
  ) +
  scale_x_continuous(
    trans = "log10",
    breaks = breaks_log(n = 6, base = 10),
  )

Lesson 3: Replace bad labels

library(scales)

ggplot(gapminder,
       aes(x = gdpPercap, y = lifeExp,
           color = continent)) +
  geom_point() +
  theme_cavis_grids +
  theme(legend.position = "top") +
  scale_y_continuous(
    breaks = seq(30, 80, 10)
  ) +
  scale_x_continuous(
    trans = "log10",
    breaks = breaks_log(n = 6, base = 10),
    labels = label_comma()
  )

Lesson 3: Replace bad labels

library(scales)

ggplot(gapminder,
       aes(x = gdpPercap, y = lifeExp,
           color = continent)) +
  geom_point() +
  theme_cavis_grids +
  theme(legend.position = "top") +
  scale_y_continuous(
    breaks = seq(30, 80, 10)
  ) +
  scale_x_continuous(
    trans = "log10",
    breaks = breaks_log(n = 6, base = 10),
    labels = label_comma()
  ) +
  labs(
    x = "GDP per capita (logged)",
    y = "Life expectancy",
  )

Lesson 3: Replace bad labels

library(scales)

ggplot(gapminder,
       aes(x = gdpPercap, y = lifeExp,
           color = continent)) +
  geom_point() +
  theme_cavis_grids +
  theme(legend.position = "top") +
  scale_y_continuous(
    breaks = seq(30, 80, 10)
  ) +
  scale_x_continuous(
    trans = "log10",
    breaks = breaks_log(n = 6, base = 10),
    labels = label_comma()
  ) +
  labs(
    x = "GDP per capita (logged)",
    y = "Life expectancy",
    title = "Positive association between GDP per capita and life expectancy",
    subtitle = "Data source: Gapminder"
  )

Lesson 4: Choose nice colors

Lesson 4: Choose nice colors

library(RColorBrewer)

Lesson 4: Choose nice colors

library(RColorBrewer)

## see all palettes
display.brewer.all()

Lesson 4: Choose nice colors

library(RColorBrewer)

## see all palettes
display.brewer.all()

## choose specific colors
set1 <- brewer.pal(n = 5, name = "Set1")

Lesson 4: Choose nice colors

library(RColorBrewer)

## see all palettes
display.brewer.all()

## choose specific colors
set1 <- brewer.pal(n = 5, name = "Set1")
dark2 <- brewer.pal(n = 5, name = "Dark2")

Lesson 4: Choose nice colors

library(RColorBrewer)

## see all palettes
display.brewer.all()

## choose specific colors
set1 <- brewer.pal(n = 5, name = "Set1")
dark2 <- brewer.pal(n = 5, name = "Dark2")

## inspect Hex codes
print(set1); print(dark2)

[1] "#E41A1C" "#377EB8" "#4DAF4A" "#984EA3" "#FF7F00"
[1] "#1B9E77" "#D95F02" "#7570B3" "#E7298A" "#66A61E"

Lesson 4: Choose nice colors

ggplot(gapminder,
       aes(x = gdpPercap, y = lifeExp,
           color = continent)) +
  theme_cavis_grids +
  scale_x_continuous(trans = "log10") +
  theme(legend.position = "top") +
  geom_point(alpha = 0.8) +
  scale_color_manual(values = set1)

Lesson 4: Choose nice colors

ggplot(gapminder,
       aes(x = gdpPercap, y = lifeExp,
           color = continent)) +
  theme_cavis_grids +
  scale_x_continuous(trans = "log10") +
  theme(legend.position = "top") +
  geom_point(alpha = 0.8) +
  scale_color_manual(values = dark2)

Lesson 4: Choose nice colors

ggplot(gapminder,
       aes(x = gdpPercap, y = lifeExp,
           color = continent)) +
  theme_cavis_grids +
  scale_x_continuous(trans = "log10") +
  theme(legend.position = "top") +
  geom_point(alpha = 0.8) +
  scale_color_brewer(type = "qual", palette = 4)

Lesson 4: Choose nice colors

ggplot(gapminder,
       aes(x = gdpPercap, y = lifeExp,
           color = continent)) +
  theme_cavis_grids +
  scale_x_continuous(trans = "log10") +
  theme(legend.position = "top") +
  geom_point(alpha = 0.8) +
  scale_color_brewer(type = "qual", palette = 7)

Lesson 5: Use small multiples

Lesson 5: Use small multiples

## create a decade variable
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.
# ℹ 1,694 more rows

Lesson 5: Use small multiples

## create a decade variable
gapminder %>%
  mutate(decade = year %/% 10)
# A tibble: 1,704 × 7
   country     continent  year lifeExp      pop gdpPercap decade
   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>  <dbl>
 1 Afghanistan Asia       1952    28.8  8425333      779.    195
 2 Afghanistan Asia       1957    30.3  9240934      821.    195
 3 Afghanistan Asia       1962    32.0 10267083      853.    196
 4 Afghanistan Asia       1967    34.0 11537966      836.    196
 5 Afghanistan Asia       1972    36.1 13079460      740.    197
 6 Afghanistan Asia       1977    38.4 14880372      786.    197
 7 Afghanistan Asia       1982    39.9 12881816      978.    198
 8 Afghanistan Asia       1987    40.8 13867957      852.    198
 9 Afghanistan Asia       1992    41.7 16317921      649.    199
10 Afghanistan Asia       1997    41.8 22227415      635.    199
# ℹ 1,694 more rows

Lesson 5: Use small multiples

## create a decade variable
gapminder %>%
  mutate(decade = year %/% 10) %>%
  mutate(decade = decade * 10)
# A tibble: 1,704 × 7
   country     continent  year lifeExp      pop gdpPercap decade
   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>  <dbl>
 1 Afghanistan Asia       1952    28.8  8425333      779.   1950
 2 Afghanistan Asia       1957    30.3  9240934      821.   1950
 3 Afghanistan Asia       1962    32.0 10267083      853.   1960
 4 Afghanistan Asia       1967    34.0 11537966      836.   1960
 5 Afghanistan Asia       1972    36.1 13079460      740.   1970
 6 Afghanistan Asia       1977    38.4 14880372      786.   1970
 7 Afghanistan Asia       1982    39.9 12881816      978.   1980
 8 Afghanistan Asia       1987    40.8 13867957      852.   1980
 9 Afghanistan Asia       1992    41.7 16317921      649.   1990
10 Afghanistan Asia       1997    41.8 22227415      635.   1990
# ℹ 1,694 more rows

Lesson 5: Use small multiples

## create a decade variable
gapminder %>%
  mutate(decade = year %/% 10) %>%
  mutate(decade = decade * 10) ->
  gapminder

Lesson 5: Use small multiples

## create a decade variable
gapminder %>%
  mutate(decade = year %/% 10) %>%
  mutate(decade = decade * 10) ->
  gapminder

## find the means of gdpPerca and lifeExp
gdpPercap_mean <- log(mean(gapminder$gdpPercap))

Lesson 5: Use small multiples

## create a decade variable
gapminder %>%
  mutate(decade = year %/% 10) %>%
  mutate(decade = decade * 10) ->
  gapminder

## find the means of gdpPerca and lifeExp
gdpPercap_mean <- log(mean(gapminder$gdpPercap))
lifeExp_mean <- mean(gapminder$lifeExp)

Lesson 5: Use small multiples

## Method 1: facet_grid
ggplot(gapminder,
       aes(x = log(gdpPercap),
           y = lifeExp))

Lesson 5: Use small multiples

## Method 1: facet_grid
ggplot(gapminder,
       aes(x = log(gdpPercap),
           y = lifeExp)) +
  theme_cavis_grids

Lesson 5: Use small multiples

## Method 1: facet_grid
ggplot(gapminder,
       aes(x = log(gdpPercap),
           y = lifeExp)) +
  theme_cavis_grids +
  facet_grid(continent ~ decade)

Lesson 5: Use small multiples

## Method 1: facet_grid
ggplot(gapminder,
       aes(x = log(gdpPercap),
           y = lifeExp)) +
  theme_cavis_grids +
  facet_grid(continent ~ decade) +
  geom_point(alpha = 0.5)

Lesson 5: Use small multiples

## Method 1: facet_grid
ggplot(gapminder,
       aes(x = log(gdpPercap),
           y = lifeExp)) +
  theme_cavis_grids +
  facet_grid(continent ~ decade) +
  geom_point(alpha = 0.5) +
  geom_vline(xintercept = gdpPercap_mean,
             linetype = 2,
             color = "grey")

Lesson 5: Use small multiples

## Method 1: facet_grid
ggplot(gapminder,
       aes(x = log(gdpPercap),
           y = lifeExp)) +
  theme_cavis_grids +
  facet_grid(continent ~ decade) +
  geom_point(alpha = 0.5) +
  geom_vline(xintercept = gdpPercap_mean,
             linetype = 2,
             color = "grey") +
  geom_hline(yintercept = lifeExp_mean,
             linetype = 2,
             color = "grey")

Lesson 5: Use small multiples

## facet_grid vs. facet_wrap
## Imagine we have some NAs
gapminder
# A tibble: 1,704 × 7
   country     continent  year lifeExp      pop gdpPercap decade
   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>  <dbl>
 1 Afghanistan Asia       1952    28.8  8425333      779.   1950
 2 Afghanistan Asia       1957    30.3  9240934      821.   1950
 3 Afghanistan Asia       1962    32.0 10267083      853.   1960
 4 Afghanistan Asia       1967    34.0 11537966      836.   1960
 5 Afghanistan Asia       1972    36.1 13079460      740.   1970
 6 Afghanistan Asia       1977    38.4 14880372      786.   1970
 7 Afghanistan Asia       1982    39.9 12881816      978.   1980
 8 Afghanistan Asia       1987    40.8 13867957      852.   1980
 9 Afghanistan Asia       1992    41.7 16317921      649.   1990
10 Afghanistan Asia       1997    41.8 22227415      635.   1990
# ℹ 1,694 more rows

Lesson 5: Use small multiples

## facet_grid vs. facet_wrap
## Imagine we have some NAs
gapminder %>%
  group_by(continent, decade)
# A tibble: 1,704 × 7
# Groups:   continent, decade [30]
   country     continent  year lifeExp      pop gdpPercap decade
   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>  <dbl>
 1 Afghanistan Asia       1952    28.8  8425333      779.   1950
 2 Afghanistan Asia       1957    30.3  9240934      821.   1950
 3 Afghanistan Asia       1962    32.0 10267083      853.   1960
 4 Afghanistan Asia       1967    34.0 11537966      836.   1960
 5 Afghanistan Asia       1972    36.1 13079460      740.   1970
 6 Afghanistan Asia       1977    38.4 14880372      786.   1970
 7 Afghanistan Asia       1982    39.9 12881816      978.   1980
 8 Afghanistan Asia       1987    40.8 13867957      852.   1980
 9 Afghanistan Asia       1992    41.7 16317921      649.   1990
10 Afghanistan Asia       1997    41.8 22227415      635.   1990
# ℹ 1,694 more rows

Lesson 5: Use small multiples

## facet_grid vs. facet_wrap
## Imagine we have some NAs
gapminder %>%
  group_by(continent, decade) %>%
  nest()
# A tibble: 30 × 3
# Groups:   continent, decade [30]
   continent decade data             
   <fct>      <dbl> <list>           
 1 Asia        1950 <tibble [66 × 5]>
 2 Asia        1960 <tibble [66 × 5]>
 3 Asia        1970 <tibble [66 × 5]>
 4 Asia        1980 <tibble [66 × 5]>
 5 Asia        1990 <tibble [66 × 5]>
 6 Asia        2000 <tibble [66 × 5]>
 7 Europe      1950 <tibble [60 × 5]>
 8 Europe      1960 <tibble [60 × 5]>
 9 Europe      1970 <tibble [60 × 5]>
10 Europe      1980 <tibble [60 × 5]>
# ℹ 20 more rows

Lesson 5: Use small multiples

## facet_grid vs. facet_wrap
## Imagine we have some NAs
gapminder %>%
  group_by(continent, decade) %>%
  nest() %>%
  ungroup()
# A tibble: 30 × 3
   continent decade data             
   <fct>      <dbl> <list>           
 1 Asia        1950 <tibble [66 × 5]>
 2 Asia        1960 <tibble [66 × 5]>
 3 Asia        1970 <tibble [66 × 5]>
 4 Asia        1980 <tibble [66 × 5]>
 5 Asia        1990 <tibble [66 × 5]>
 6 Asia        2000 <tibble [66 × 5]>
 7 Europe      1950 <tibble [60 × 5]>
 8 Europe      1960 <tibble [60 × 5]>
 9 Europe      1970 <tibble [60 × 5]>
10 Europe      1980 <tibble [60 × 5]>
# ℹ 20 more rows

Lesson 5: Use small multiples

## facet_grid vs. facet_wrap
## Imagine we have some NAs
gapminder %>%
  group_by(continent, decade) %>%
  nest() %>%
  ungroup() %>%
  slice_sample(n = 10)
# A tibble: 10 × 3
   continent decade data              
   <fct>      <dbl> <list>            
 1 Asia        1950 <tibble [66 × 5]> 
 2 Africa      1950 <tibble [104 × 5]>
 3 Americas    1950 <tibble [50 × 5]> 
 4 Americas    2000 <tibble [50 × 5]> 
 5 Europe      2000 <tibble [60 × 5]> 
 6 Americas    1970 <tibble [50 × 5]> 
 7 Europe      1960 <tibble [60 × 5]> 
 8 Asia        1990 <tibble [66 × 5]> 
 9 Africa      1970 <tibble [104 × 5]>
10 Oceania     1980 <tibble [4 × 5]>  

Lesson 5: Use small multiples

## facet_grid vs. facet_wrap
## Imagine we have some NAs
gapminder %>%
  group_by(continent, decade) %>%
  nest() %>%
  ungroup() %>%
  slice_sample(n = 10) %>%
  unnest(cols = data)
# A tibble: 448 × 7
   continent decade country      year lifeExp       pop gdpPercap
   <fct>      <dbl> <fct>       <int>   <dbl>     <int>     <dbl>
 1 Oceania     1970 Australia    1972    71.9  13177000    16789.
 2 Oceania     1970 Australia    1977    73.5  14074100    18334.
 3 Oceania     1970 New Zealand  1972    71.9   2929100    16046.
 4 Oceania     1970 New Zealand  1977    72.2   3164900    16234.
 5 Americas    2000 Argentina    2002    74.3  38331121     8798.
 6 Americas    2000 Argentina    2007    75.3  40301927    12779.
 7 Americas    2000 Bolivia      2002    63.9   8445134     3413.
 8 Americas    2000 Bolivia      2007    65.6   9119152     3822.
 9 Americas    2000 Brazil       2002    71.0 179914212     8131.
10 Americas    2000 Brazil       2007    72.4 190010647     9066.
# ℹ 438 more rows

Lesson 5: Use small multiples

## facet_grid vs. facet_wrap
## Imagine we have some NAs
gapminder %>%
  group_by(continent, decade) %>%
  nest() %>%
  ungroup() %>%
  slice_sample(n = 10) %>%
  unnest(cols = data) ->
  gapminder_sample

Lesson 5: Use small multiples

## facet_grid vs. facet_wrap
ggplot(gapminder_sample,
       aes(x = log(gdpPercap),
           y = lifeExp)) +
  theme_cavis_grids +
  geom_point(alpha = 0.5) +
  facet_grid(continent ~ decade)

Lesson 5: Use small multiples

## facet_grid vs. facet_wrap
ggplot(gapminder_sample,
       aes(x = log(gdpPercap),
           y = lifeExp)) +
  theme_cavis_grids +
  geom_point(alpha = 0.5) +
  facet_wrap(continent ~ decade)

Lesson 5: Use small multiples

## facet_grid vs. facet_wrap
ggplot(gapminder_sample,
       aes(x = log(gdpPercap),
           y = lifeExp)) +
  theme_cavis_grids +
  geom_point(alpha = 0.5) +
  facet_wrap(continent ~ decade, nrow = 2)

Lesson 5: Use small multiples

## facet_wrap is more flexible
## but use it carefully...
ggplot(gapminder_sample,
       aes(x = lifeExp))

Lesson 5: Use small multiples

## facet_wrap is more flexible
## but use it carefully...
ggplot(gapminder_sample,
       aes(x = lifeExp)) +
  geom_histogram()

Lesson 5: Use small multiples

## facet_wrap is more flexible
## but use it carefully...
ggplot(gapminder_sample,
       aes(x = lifeExp)) +
  geom_histogram() +
  facet_wrap(~ country, nrow = 6)

Lesson 6: Diagonalize (sort) whenever possible

Lesson 6: Diagonalize (sort) whenever possible

## 1. categorize lifeExp
## 2. select on Asia & Africa
gapminder
# A tibble: 1,704 × 7
   country     continent  year lifeExp      pop gdpPercap decade
   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>  <dbl>
 1 Afghanistan Asia       1952    28.8  8425333      779.   1950
 2 Afghanistan Asia       1957    30.3  9240934      821.   1950
 3 Afghanistan Asia       1962    32.0 10267083      853.   1960
 4 Afghanistan Asia       1967    34.0 11537966      836.   1960
 5 Afghanistan Asia       1972    36.1 13079460      740.   1970
 6 Afghanistan Asia       1977    38.4 14880372      786.   1970
 7 Afghanistan Asia       1982    39.9 12881816      978.   1980
 8 Afghanistan Asia       1987    40.8 13867957      852.   1980
 9 Afghanistan Asia       1992    41.7 16317921      649.   1990
10 Afghanistan Asia       1997    41.8 22227415      635.   1990
# ℹ 1,694 more rows

Lesson 6: Diagonalize (sort) whenever possible

## 1. categorize lifeExp
## 2. select on Asia & Africa
gapminder %>%
  mutate(lifeExp_cat = cut(lifeExp, 5, dig.lab = 2))
# A tibble: 1,704 × 8
   country     continent  year lifeExp      pop gdpPercap decade lifeExp_cat
   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>  <dbl> <fct>      
 1 Afghanistan Asia       1952    28.8  8425333      779.   1950 (24,35]    
 2 Afghanistan Asia       1957    30.3  9240934      821.   1950 (24,35]    
 3 Afghanistan Asia       1962    32.0 10267083      853.   1960 (24,35]    
 4 Afghanistan Asia       1967    34.0 11537966      836.   1960 (24,35]    
 5 Afghanistan Asia       1972    36.1 13079460      740.   1970 (35,47]    
 6 Afghanistan Asia       1977    38.4 14880372      786.   1970 (35,47]    
 7 Afghanistan Asia       1982    39.9 12881816      978.   1980 (35,47]    
 8 Afghanistan Asia       1987    40.8 13867957      852.   1980 (35,47]    
 9 Afghanistan Asia       1992    41.7 16317921      649.   1990 (35,47]    
10 Afghanistan Asia       1997    41.8 22227415      635.   1990 (35,47]    
# ℹ 1,694 more rows

Lesson 6: Diagonalize (sort) whenever possible

## 1. categorize lifeExp
## 2. select on Asia & Africa
gapminder %>%
  mutate(lifeExp_cat = cut(lifeExp, 5, dig.lab = 2)) %>%
  filter(continent %in% c("Asia", "Africa"))
# A tibble: 1,020 × 8
   country     continent  year lifeExp      pop gdpPercap decade lifeExp_cat
   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>  <dbl> <fct>      
 1 Afghanistan Asia       1952    28.8  8425333      779.   1950 (24,35]    
 2 Afghanistan Asia       1957    30.3  9240934      821.   1950 (24,35]    
 3 Afghanistan Asia       1962    32.0 10267083      853.   1960 (24,35]    
 4 Afghanistan Asia       1967    34.0 11537966      836.   1960 (24,35]    
 5 Afghanistan Asia       1972    36.1 13079460      740.   1970 (35,47]    
 6 Afghanistan Asia       1977    38.4 14880372      786.   1970 (35,47]    
 7 Afghanistan Asia       1982    39.9 12881816      978.   1980 (35,47]    
 8 Afghanistan Asia       1987    40.8 13867957      852.   1980 (35,47]    
 9 Afghanistan Asia       1992    41.7 16317921      649.   1990 (35,47]    
10 Afghanistan Asia       1997    41.8 22227415      635.   1990 (35,47]    
# ℹ 1,010 more rows

Lesson 6: Diagonalize (sort) whenever possible

## 1. categorize lifeExp
## 2. select on Asia & Africa
gapminder %>%
  mutate(lifeExp_cat = cut(lifeExp, 5, dig.lab = 2)) %>%
  filter(continent %in% c("Asia", "Africa")) ->
  gapminder_aa

Lesson 6: Diagonalize (sort) whenever possible

ggplot(gapminder_aa,
       aes(x = year,
           y = country,
           fill = lifeExp_cat))

Lesson 6: Diagonalize (sort) whenever possible

ggplot(gapminder_aa,
       aes(x = year,
           y = country,
           fill = lifeExp_cat)) +
  geom_tile()

Lesson 6: Diagonalize (sort) whenever possible

ggplot(gapminder_aa,
       aes(x = year,
           y = country,
           fill = lifeExp_cat)) +
  geom_tile() +
  scale_fill_brewer(palette = "YlGnBu")

Lesson 6: Diagonalize (sort) whenever possible

ggplot(gapminder_aa,
       aes(x = year,
           y = fct_reorder(country, lifeExp),
           fill = lifeExp_cat)) +
  geom_tile() +
  scale_fill_brewer(palette = "YlGnBu")

Lesson 6: Diagonalize (sort) whenever possible

ggplot(gapminder_aa,
       aes(x = year,
           y = fct_reorder(country, lifeExp),
           fill = lifeExp_cat)) +
  geom_tile() +
  scale_fill_brewer(palette = "YlGnBu") +
  labs(y = NULL, x = NULL,
       fill = "Life Expectancy")

Lesson 6: Diagonalize (sort) whenever possible

ggplot(gapminder_aa,
       aes(x = year,
           y = fct_reorder(country, lifeExp),
           fill = lifeExp_cat)) +
  geom_tile() +
  scale_fill_brewer(palette = "YlGnBu") +
  labs(y = NULL, x = NULL,
       fill = "Life Expectancy") +
  theme_cavis +
  theme(aspect.ratio = 1.618,
        )

Lesson 6: Diagonalize (sort) whenever possible

ggplot(gapminder_aa,
       aes(x = year,
           y = fct_reorder(country, lifeExp),
           fill = lifeExp_cat)) +
  geom_tile() +
  scale_fill_brewer(palette = "YlGnBu") +
  labs(y = NULL, x = NULL,
       fill = "Life Expectancy") +
  theme_cavis +
  theme(aspect.ratio = 1.618,
        text = element_text(size = 8),
        )

Lesson 6: Diagonalize (sort) whenever possible

ggplot(gapminder_aa,
       aes(x = year,
           y = fct_reorder(country, lifeExp),
           fill = lifeExp_cat)) +
  geom_tile() +
  scale_fill_brewer(palette = "YlGnBu") +
  labs(y = NULL, x = NULL,
       fill = "Life Expectancy") +
  theme_cavis +
  theme(aspect.ratio = 1.618,
        text = element_text(size = 8),
        axis.text.y.left = element_text(size = 6),
        )

Lesson 6: Diagonalize (sort) whenever possible

ggplot(gapminder_aa,
       aes(x = year,
           y = fct_reorder(country, lifeExp),
           fill = lifeExp_cat)) +
  geom_tile() +
  scale_fill_brewer(palette = "YlGnBu") +
  labs(y = NULL, x = NULL,
       fill = "Life Expectancy") +
  theme_cavis +
  theme(aspect.ratio = 1.618,
        text = element_text(size = 8),
        axis.text.y.left = element_text(size = 6),
        legend.position = "top",
        )

Lesson 6: Diagonalize (sort) whenever possible

ggplot(gapminder_aa,
       aes(x = year,
           y = fct_reorder(country, lifeExp),
           fill = lifeExp_cat)) +
  geom_tile() +
  scale_fill_brewer(palette = "YlGnBu") +
  labs(y = NULL, x = NULL,
       fill = "Life Expectancy") +
  theme_cavis +
  theme(aspect.ratio = 1.618,
        text = element_text(size = 8),
        axis.text.y.left = element_text(size = 6),
        legend.position = "top",
        legend.text.position = "top",
        )

Lesson 6: Diagonalize (sort) whenever possible

ggplot(gapminder_aa,
       aes(x = year,
           y = fct_reorder(country, lifeExp),
           fill = lifeExp_cat)) +
  geom_tile() +
  scale_fill_brewer(palette = "YlGnBu") +
  labs(y = NULL, x = NULL,
       fill = "Life Expectancy") +
  theme_cavis +
  theme(aspect.ratio = 1.618,
        text = element_text(size = 8),
        axis.text.y.left = element_text(size = 6),
        legend.position = "top",
        legend.text.position = "top",
        legend.key.height = unit(0.15, "cm"),
        )

Lesson 6: Diagonalize (sort) whenever possible

ggplot(gapminder_aa,
       aes(x = year,
           y = fct_reorder(country, lifeExp),
           fill = lifeExp_cat)) +
  geom_tile() +
  scale_fill_brewer(palette = "YlGnBu") +
  labs(y = NULL, x = NULL,
       fill = "Life Expectancy") +
  theme_cavis +
  theme(aspect.ratio = 1.618,
        text = element_text(size = 8),
        axis.text.y.left = element_text(size = 6),
        legend.position = "top",
        legend.text.position = "top",
        legend.key.height = unit(0.15, "cm"),
        legend.key.width = unit(0.75, "cm")
        )

Lesson 6: Diagonalize (sort) whenever possible

ggplot(gapminder_aa,
       aes(x = year,
           y = fct_reorder(country, lifeExp),
           fill = lifeExp_cat)) +
  geom_tile() +
  scale_fill_brewer(palette = "YlGnBu") +
  labs(y = NULL, x = NULL,
       fill = "Life Expectancy") +
  theme_cavis +
  theme(aspect.ratio = 1.618,
        text = element_text(size = 8),
        axis.text.y.left = element_text(size = 6),
        legend.position = "top",
        legend.text.position = "top",
        legend.key.height = unit(0.15, "cm"),
        legend.key.width = unit(0.75, "cm")
        ) +
  coord_fixed(expand = FALSE)

Lesson 7: Highlight interesting observations

Lesson 7: Highlight interesting observations

library(ggrepel)

Lesson 7: Highlight interesting observations

library(ggrepel)

gapminder_aa
# A tibble: 1,020 × 8
   country     continent  year lifeExp      pop gdpPercap decade lifeExp_cat
   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>  <dbl> <fct>      
 1 Afghanistan Asia       1952    28.8  8425333      779.   1950 (24,35]    
 2 Afghanistan Asia       1957    30.3  9240934      821.   1950 (24,35]    
 3 Afghanistan Asia       1962    32.0 10267083      853.   1960 (24,35]    
 4 Afghanistan Asia       1967    34.0 11537966      836.   1960 (24,35]    
 5 Afghanistan Asia       1972    36.1 13079460      740.   1970 (35,47]    
 6 Afghanistan Asia       1977    38.4 14880372      786.   1970 (35,47]    
 7 Afghanistan Asia       1982    39.9 12881816      978.   1980 (35,47]    
 8 Afghanistan Asia       1987    40.8 13867957      852.   1980 (35,47]    
 9 Afghanistan Asia       1992    41.7 16317921      649.   1990 (35,47]    
10 Afghanistan Asia       1997    41.8 22227415      635.   1990 (35,47]    
# ℹ 1,010 more rows

Lesson 7: Highlight interesting observations

library(ggrepel)

gapminder_aa %>%
  filter(year == 2007)
# A tibble: 85 × 8
   country      continent  year lifeExp       pop gdpPercap decade lifeExp_cat
   <fct>        <fct>     <int>   <dbl>     <int>     <dbl>  <dbl> <fct>      
 1 Afghanistan  Asia       2007    43.8  31889923      975.   2000 (35,47]    
 2 Algeria      Africa     2007    72.3  33333216     6223.   2000 (71,83]    
 3 Angola       Africa     2007    42.7  12420476     4797.   2000 (35,47]    
 4 Bahrain      Asia       2007    75.6    708573    29796.   2000 (71,83]    
 5 Bangladesh   Asia       2007    64.1 150448339     1391.   2000 (59,71]    
 6 Benin        Africa     2007    56.7   8078314     1441.   2000 (47,59]    
 7 Botswana     Africa     2007    50.7   1639131    12570.   2000 (47,59]    
 8 Burkina Faso Africa     2007    52.3  14326203     1217.   2000 (47,59]    
 9 Burundi      Africa     2007    49.6   8390505      430.   2000 (47,59]    
10 Cambodia     Asia       2007    59.7  14131858     1714.   2000 (59,71]    
# ℹ 75 more rows

Lesson 7: Highlight interesting observations

library(ggrepel)

gapminder_aa %>%
  filter(year == 2007) ->
  gapminder_aa_07

Lesson 7: Highlight interesting observations

library(ggrepel)

gapminder_aa %>%
  filter(year == 2007) ->
  gapminder_aa_07

ggplot(gapminder_aa_07,
         aes(x = gdpPercap,
             y = lifeExp))

Lesson 7: Highlight interesting observations

library(ggrepel)

gapminder_aa %>%
  filter(year == 2007) ->
  gapminder_aa_07

ggplot(gapminder_aa_07,
         aes(x = gdpPercap,
             y = lifeExp)) +
  theme_cavis_grids

Lesson 7: Highlight interesting observations

library(ggrepel)

gapminder_aa %>%
  filter(year == 2007) ->
  gapminder_aa_07

ggplot(gapminder_aa_07,
         aes(x = gdpPercap,
             y = lifeExp)) +
  theme_cavis_grids +
  geom_point()

Lesson 7: Highlight interesting observations

library(ggrepel)

gapminder_aa %>%
  filter(year == 2007) ->
  gapminder_aa_07

ggplot(gapminder_aa_07,
         aes(x = gdpPercap,
             y = lifeExp)) +
  theme_cavis_grids +
  geom_point() +
  scale_x_continuous(trans = "log10")

Lesson 7: Highlight interesting observations

library(ggrepel)

gapminder_aa %>%
  filter(year == 2007) ->
  gapminder_aa_07

ggplot(gapminder_aa_07,
         aes(x = gdpPercap,
             y = lifeExp)) +
  theme_cavis_grids +
  geom_point() +
  scale_x_continuous(trans = "log10") +
  geom_text(aes(label = country))

Lesson 7: Highlight interesting observations

library(ggrepel)

gapminder_aa %>%
  filter(year == 2007) ->
  gapminder_aa_07

ggplot(gapminder_aa_07,
         aes(x = gdpPercap,
             y = lifeExp)) +
  theme_cavis_grids +
  geom_point() +
  scale_x_continuous(trans = "log10") +
  geom_text_repel(aes(label = country))

Lesson 7: Highlight interesting observations

library(ggrepel)

gapminder_aa %>%
  filter(year == 2007) ->
  gapminder_aa_07

ggplot(gapminder_aa_07,
         aes(x = gdpPercap,
             y = lifeExp)) +
  theme_cavis_grids +
  geom_point() +
  scale_x_continuous(trans = "log10") +
  geom_label_repel(aes(label = country))

Lesson 7: Highlight interesting observations

## any unusual obs
ggplot(gapminder_aa_07,
         aes(x = gdpPercap,
             y = lifeExp)) +
  theme_cavis_grids +
  geom_point() +
  scale_x_continuous(trans = "log10")

Lesson 7: Highlight interesting observations

## any unusual obs
ggplot(gapminder_aa_07,
         aes(x = gdpPercap,
             y = lifeExp)) +
  theme_cavis_grids +
  geom_point() +
  scale_x_continuous(trans = "log10") +
  geom_smooth(
  )

Lesson 7: Highlight interesting observations

## any unusual obs
ggplot(gapminder_aa_07,
         aes(x = gdpPercap,
             y = lifeExp)) +
  theme_cavis_grids +
  geom_point() +
  scale_x_continuous(trans = "log10") +
  geom_smooth(
    method = "lm",
  )

Lesson 7: Highlight interesting observations

## any unusual obs
ggplot(gapminder_aa_07,
         aes(x = gdpPercap,
             y = lifeExp)) +
  theme_cavis_grids +
  geom_point() +
  scale_x_continuous(trans = "log10") +
  geom_smooth(
    method = "lm",
    color = "grey30"
  )

Lesson 7: Highlight interesting observations

library(broom)

Lesson 7: Highlight interesting observations

library(broom)

lm_res <- lm(lifeExp ~ log10(gdpPercap),
             data = gapminder_aa_07)

Lesson 7: Highlight interesting observations

library(broom)

lm_res <- lm(lifeExp ~ log10(gdpPercap),
             data = gapminder_aa_07)

lm_res

Call:
lm(formula = lifeExp ~ log10(gdpPercap), data = gapminder_aa_07)

Coefficients:
     (Intercept)  log10(gdpPercap)  
           8.435            15.176  

Lesson 7: Highlight interesting observations

library(broom)

lm_res <- lm(lifeExp ~ log10(gdpPercap),
             data = gapminder_aa_07)

lm_res %>%
  augment(data = gapminder_aa_07)
# A tibble: 85 × 14
   country   continent  year lifeExp    pop gdpPercap decade lifeExp_cat .fitted
   <fct>     <fct>     <int>   <dbl>  <int>     <dbl>  <dbl> <fct>         <dbl>
 1 Afghanis… Asia       2007    43.8 3.19e7      975.   2000 (35,47]        53.8
 2 Algeria   Africa     2007    72.3 3.33e7     6223.   2000 (71,83]        66.0
 3 Angola    Africa     2007    42.7 1.24e7     4797.   2000 (35,47]        64.3
 4 Bahrain   Asia       2007    75.6 7.09e5    29796.   2000 (71,83]        76.3
 5 Banglade… Asia       2007    64.1 1.50e8     1391.   2000 (59,71]        56.1
 6 Benin     Africa     2007    56.7 8.08e6     1441.   2000 (47,59]        56.4
 7 Botswana  Africa     2007    50.7 1.64e6    12570.   2000 (47,59]        70.6
 8 Burkina … Africa     2007    52.3 1.43e7     1217.   2000 (47,59]        55.3
 9 Burundi   Africa     2007    49.6 8.39e6      430.   2000 (47,59]        48.4
10 Cambodia  Asia       2007    59.7 1.41e7     1714.   2000 (59,71]        57.5
# ℹ 75 more rows
# ℹ 5 more variables: .resid <dbl>, .hat <dbl>, .sigma <dbl>, .cooksd <dbl>,
#   .std.resid <dbl>

Lesson 7: Highlight interesting observations

library(broom)

lm_res <- lm(lifeExp ~ log10(gdpPercap),
             data = gapminder_aa_07)

lm_res %>%
  augment(data = gapminder_aa_07) ->
  gapminder_aa_07_augmented

Lesson 7: Highlight interesting observations

library(broom)

lm_res <- lm(lifeExp ~ log10(gdpPercap),
             data = gapminder_aa_07)

lm_res %>%
  augment(data = gapminder_aa_07) ->
  gapminder_aa_07_augmented

gapminder_aa_07_augmented
# A tibble: 85 × 14
   country   continent  year lifeExp    pop gdpPercap decade lifeExp_cat .fitted
   <fct>     <fct>     <int>   <dbl>  <int>     <dbl>  <dbl> <fct>         <dbl>
 1 Afghanis… Asia       2007    43.8 3.19e7      975.   2000 (35,47]        53.8
 2 Algeria   Africa     2007    72.3 3.33e7     6223.   2000 (71,83]        66.0
 3 Angola    Africa     2007    42.7 1.24e7     4797.   2000 (35,47]        64.3
 4 Bahrain   Asia       2007    75.6 7.09e5    29796.   2000 (71,83]        76.3
 5 Banglade… Asia       2007    64.1 1.50e8     1391.   2000 (59,71]        56.1
 6 Benin     Africa     2007    56.7 8.08e6     1441.   2000 (47,59]        56.4
 7 Botswana  Africa     2007    50.7 1.64e6    12570.   2000 (47,59]        70.6
 8 Burkina … Africa     2007    52.3 1.43e7     1217.   2000 (47,59]        55.3
 9 Burundi   Africa     2007    49.6 8.39e6      430.   2000 (47,59]        48.4
10 Cambodia  Asia       2007    59.7 1.41e7     1714.   2000 (59,71]        57.5
# ℹ 75 more rows
# ℹ 5 more variables: .resid <dbl>, .hat <dbl>, .sigma <dbl>, .cooksd <dbl>,
#   .std.resid <dbl>

Lesson 7: Highlight interesting observations

library(broom)

lm_res <- lm(lifeExp ~ log10(gdpPercap),
             data = gapminder_aa_07)

lm_res %>%
  augment(data = gapminder_aa_07) ->
  gapminder_aa_07_augmented

gapminder_aa_07_augmented %>%
  slice_max(order_by = abs(.resid), n = 5)
# A tibble: 5 × 14
  country    continent  year lifeExp    pop gdpPercap decade lifeExp_cat .fitted
  <fct>      <fct>     <int>   <dbl>  <int>     <dbl>  <dbl> <fct>         <dbl>
1 Swaziland  Africa     2007    39.6 1.13e6     4513.   2000 (35,47]        63.9
2 Angola     Africa     2007    42.7 1.24e7     4797.   2000 (35,47]        64.3
3 Botswana   Africa     2007    50.7 1.64e6    12570.   2000 (47,59]        70.6
4 South Afr… Africa     2007    49.3 4.40e7     9270.   2000 (47,59]        68.6
5 Equatoria… Africa     2007    51.6 5.51e5    12154.   2000 (47,59]        70.4
# ℹ 5 more variables: .resid <dbl>, .hat <dbl>, .sigma <dbl>, .cooksd <dbl>,
#   .std.resid <dbl>

Lesson 7: Highlight interesting observations

library(broom)

lm_res <- lm(lifeExp ~ log10(gdpPercap),
             data = gapminder_aa_07)

lm_res %>%
  augment(data = gapminder_aa_07) ->
  gapminder_aa_07_augmented

gapminder_aa_07_augmented %>%
  slice_max(order_by = abs(.resid), n = 5) ->
  outliers

Lesson 7: Highlight interesting observations

library(ggforce)

ggplot(gapminder_aa_07,
         aes(x = gdpPercap,
             y = lifeExp)) +
  theme_cavis_grids +
  geom_point() +
  scale_x_continuous(trans = "log10") +
  geom_smooth(
    method = "lm",
    color = "grey30"
  )

Lesson 7: Highlight interesting observations

library(ggforce)

ggplot(gapminder_aa_07,
         aes(x = gdpPercap,
             y = lifeExp)) +
  theme_cavis_grids +
  geom_point() +
  scale_x_continuous(trans = "log10") +
  geom_smooth(
    method = "lm",
    color = "grey30"
  ) +
  geom_text_repel(data = outliers,
                  aes(label = country))

Lesson 7: Highlight interesting observations

library(ggforce)

ggplot(gapminder_aa_07,
         aes(x = gdpPercap,
             y = lifeExp)) +
  theme_cavis_grids +
  geom_point() +
  scale_x_continuous(trans = "log10") +
  geom_smooth(
    method = "lm",
    color = "grey30"
  ) +
  geom_text_repel(data = outliers,
                  aes(label = country)) +
  geom_mark_ellipse(data = outliers,
                    linetype = 2)

Lesson 7: Highlight interesting observations

library(ggforce)

ggplot(gapminder_aa_07,
         aes(x = gdpPercap,
             y = lifeExp)) +
  theme_cavis_grids +
  geom_point() +
  scale_x_continuous(trans = "log10") +
  geom_smooth(
    method = "lm",
    color = "grey30"
  ) +
  geom_text_repel(data = outliers,
                  aes(label = country)) +
  geom_mark_ellipse(data = outliers,
                    linetype = 2) +
  coord_cartesian(clip = "off")

Lesson 8: Juxtapose observations

Lesson 8: Juxtapose observations

gapminder
# A tibble: 1,704 × 7
   country     continent  year lifeExp      pop gdpPercap decade
   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>  <dbl>
 1 Afghanistan Asia       1952    28.8  8425333      779.   1950
 2 Afghanistan Asia       1957    30.3  9240934      821.   1950
 3 Afghanistan Asia       1962    32.0 10267083      853.   1960
 4 Afghanistan Asia       1967    34.0 11537966      836.   1960
 5 Afghanistan Asia       1972    36.1 13079460      740.   1970
 6 Afghanistan Asia       1977    38.4 14880372      786.   1970
 7 Afghanistan Asia       1982    39.9 12881816      978.   1980
 8 Afghanistan Asia       1987    40.8 13867957      852.   1980
 9 Afghanistan Asia       1992    41.7 16317921      649.   1990
10 Afghanistan Asia       1997    41.8 22227415      635.   1990
# ℹ 1,694 more rows

Lesson 8: Juxtapose observations

gapminder %>%
  filter(continent %in% c("Asia"))
# A tibble: 396 × 7
   country     continent  year lifeExp      pop gdpPercap decade
   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>  <dbl>
 1 Afghanistan Asia       1952    28.8  8425333      779.   1950
 2 Afghanistan Asia       1957    30.3  9240934      821.   1950
 3 Afghanistan Asia       1962    32.0 10267083      853.   1960
 4 Afghanistan Asia       1967    34.0 11537966      836.   1960
 5 Afghanistan Asia       1972    36.1 13079460      740.   1970
 6 Afghanistan Asia       1977    38.4 14880372      786.   1970
 7 Afghanistan Asia       1982    39.9 12881816      978.   1980
 8 Afghanistan Asia       1987    40.8 13867957      852.   1980
 9 Afghanistan Asia       1992    41.7 16317921      649.   1990
10 Afghanistan Asia       1997    41.8 22227415      635.   1990
# ℹ 386 more rows

Lesson 8: Juxtapose observations

gapminder %>%
  filter(continent %in% c("Asia")) ->
  gapminder_asia

Lesson 8: Juxtapose observations

gapminder %>%
  filter(continent %in% c("Asia")) ->
  gapminder_asia

ggplot(gapminder_asia,
       aes(x = year,
           y = log(gdpPercap),
           color = country))

Lesson 8: Juxtapose observations

gapminder %>%
  filter(continent %in% c("Asia")) ->
  gapminder_asia

ggplot(gapminder_asia,
       aes(x = year,
           y = log(gdpPercap),
           color = country)) +
  theme_cavis_hgrid

Lesson 8: Juxtapose observations

gapminder %>%
  filter(continent %in% c("Asia")) ->
  gapminder_asia

ggplot(gapminder_asia,
       aes(x = year,
           y = log(gdpPercap),
           color = country)) +
  theme_cavis_hgrid +
  geom_line()

Lesson 8: Juxtapose observations

set1 <- RColorBrewer::brewer.pal(n = 5, "Set1")

Lesson 8: Juxtapose observations

set1 <- RColorBrewer::brewer.pal(n = 5, "Set1")
orange <- set1[5]; blue <- set1[2]

Lesson 8: Juxtapose observations

set1 <- RColorBrewer::brewer.pal(n = 5, "Set1")
orange <- set1[5]; blue <- set1[2]

ggplot(gapminder_asia,
       aes(x = year,
           y = log(gdpPercap),
           group = country))

Lesson 8: Juxtapose observations

set1 <- RColorBrewer::brewer.pal(n = 5, "Set1")
orange <- set1[5]; blue <- set1[2]

ggplot(gapminder_asia,
       aes(x = year,
           y = log(gdpPercap),
           group = country)) +
  theme_cavis_hgrid

Lesson 8: Juxtapose observations

set1 <- RColorBrewer::brewer.pal(n = 5, "Set1")
orange <- set1[5]; blue <- set1[2]

ggplot(gapminder_asia,
       aes(x = year,
           y = log(gdpPercap),
           group = country)) +
  theme_cavis_hgrid +
  geom_line(color = "grey90")

Lesson 8: Juxtapose observations

set1 <- RColorBrewer::brewer.pal(n = 5, "Set1")
orange <- set1[5]; blue <- set1[2]

ggplot(gapminder_asia,
       aes(x = year,
           y = log(gdpPercap),
           group = country)) +
  theme_cavis_hgrid +
  geom_line(color = "grey90") +
  geom_line(
    data = filter(gapminder_asia,
                  country == "Korea, Rep."),
    color = blue
  )

Lesson 8: Juxtapose observations

set1 <- RColorBrewer::brewer.pal(n = 5, "Set1")
orange <- set1[5]; blue <- set1[2]

ggplot(gapminder_asia,
       aes(x = year,
           y = log(gdpPercap),
           group = country)) +
  theme_cavis_hgrid +
  geom_line(color = "grey90") +
  geom_line(
    data = filter(gapminder_asia,
                  country == "Korea, Rep."),
    color = blue
  ) +
  geom_line(
    data = filter(gapminder_asia,
                  country == "Korea, Dem. Rep."),
    color = orange
  )

Lesson 8: Juxtapose observations

set1 <- RColorBrewer::brewer.pal(n = 5, "Set1")
orange <- set1[5]; blue <- set1[2]

ggplot(gapminder_asia,
       aes(x = year,
           y = log(gdpPercap),
           group = country)) +
  theme_cavis_hgrid +
  geom_line(color = "grey90") +
  geom_line(
    data = filter(gapminder_asia,
                  country == "Korea, Rep."),
    color = blue
  ) +
  geom_line(
    data = filter(gapminder_asia,
                  country == "Korea, Dem. Rep."),
    color = orange
  ) +
  annotate("text", label = "South Korea",
           x = 2007, y = 10.2,
           color = blue)

Lesson 8: Juxtapose observations

set1 <- RColorBrewer::brewer.pal(n = 5, "Set1")
orange <- set1[5]; blue <- set1[2]

ggplot(gapminder_asia,
       aes(x = year,
           y = log(gdpPercap),
           group = country)) +
  theme_cavis_hgrid +
  geom_line(color = "grey90") +
  geom_line(
    data = filter(gapminder_asia,
                  country == "Korea, Rep."),
    color = blue
  ) +
  geom_line(
    data = filter(gapminder_asia,
                  country == "Korea, Dem. Rep."),
    color = orange
  ) +
  annotate("text", label = "South Korea",
           x = 2007, y = 10.2,
           color = blue) +
  annotate("text", label = "North Korea",
           x = 2007, y = 7.6,
           color = orange)

Lesson 9: Present uncertainty

Lesson 9: Present uncertainty

## Run some regression models
gapminder
# A tibble: 1,704 × 7
   country     continent  year lifeExp      pop gdpPercap decade
   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>  <dbl>
 1 Afghanistan Asia       1952    28.8  8425333      779.   1950
 2 Afghanistan Asia       1957    30.3  9240934      821.   1950
 3 Afghanistan Asia       1962    32.0 10267083      853.   1960
 4 Afghanistan Asia       1967    34.0 11537966      836.   1960
 5 Afghanistan Asia       1972    36.1 13079460      740.   1970
 6 Afghanistan Asia       1977    38.4 14880372      786.   1970
 7 Afghanistan Asia       1982    39.9 12881816      978.   1980
 8 Afghanistan Asia       1987    40.8 13867957      852.   1980
 9 Afghanistan Asia       1992    41.7 16317921      649.   1990
10 Afghanistan Asia       1997    41.8 22227415      635.   1990
# ℹ 1,694 more rows

Lesson 9: Present uncertainty

## Run some regression models
gapminder %>%
  mutate(decade = factor(decade))
# A tibble: 1,704 × 7
   country     continent  year lifeExp      pop gdpPercap decade
   <fct>       <fct>     <int>   <dbl>    <int>     <dbl> <fct> 
 1 Afghanistan Asia       1952    28.8  8425333      779. 1950  
 2 Afghanistan Asia       1957    30.3  9240934      821. 1950  
 3 Afghanistan Asia       1962    32.0 10267083      853. 1960  
 4 Afghanistan Asia       1967    34.0 11537966      836. 1960  
 5 Afghanistan Asia       1972    36.1 13079460      740. 1970  
 6 Afghanistan Asia       1977    38.4 14880372      786. 1970  
 7 Afghanistan Asia       1982    39.9 12881816      978. 1980  
 8 Afghanistan Asia       1987    40.8 13867957      852. 1980  
 9 Afghanistan Asia       1992    41.7 16317921      649. 1990  
10 Afghanistan Asia       1997    41.8 22227415      635. 1990  
# ℹ 1,694 more rows

Lesson 9: Present uncertainty

## Run some regression models
gapminder %>%
  mutate(decade = factor(decade)) %>%
  mutate(continent = fct_relevel(continent, "Asia"))
# A tibble: 1,704 × 7
   country     continent  year lifeExp      pop gdpPercap decade
   <fct>       <fct>     <int>   <dbl>    <int>     <dbl> <fct> 
 1 Afghanistan Asia       1952    28.8  8425333      779. 1950  
 2 Afghanistan Asia       1957    30.3  9240934      821. 1950  
 3 Afghanistan Asia       1962    32.0 10267083      853. 1960  
 4 Afghanistan Asia       1967    34.0 11537966      836. 1960  
 5 Afghanistan Asia       1972    36.1 13079460      740. 1970  
 6 Afghanistan Asia       1977    38.4 14880372      786. 1970  
 7 Afghanistan Asia       1982    39.9 12881816      978. 1980  
 8 Afghanistan Asia       1987    40.8 13867957      852. 1980  
 9 Afghanistan Asia       1992    41.7 16317921      649. 1990  
10 Afghanistan Asia       1997    41.8 22227415      635. 1990  
# ℹ 1,694 more rows

Lesson 9: Present uncertainty

## Run some regression models
gapminder %>%
  mutate(decade = factor(decade)) %>%
  mutate(continent = fct_relevel(continent, "Asia")) ->
  gapminder

Lesson 9: Present uncertainty

## Run some regression models
gapminder %>%
  mutate(decade = factor(decade)) %>%
  mutate(continent = fct_relevel(continent, "Asia")) ->
  gapminder

m1 <- lifeExp ~ log(gdpPercap) + log(pop)

Lesson 9: Present uncertainty

## Run some regression models
gapminder %>%
  mutate(decade = factor(decade)) %>%
  mutate(continent = fct_relevel(continent, "Asia")) ->
  gapminder

m1 <- lifeExp ~ log(gdpPercap) + log(pop)
m2 <- lifeExp ~ log(gdpPercap) + log(pop) + continent

Lesson 9: Present uncertainty

## Run some regression models
gapminder %>%
  mutate(decade = factor(decade)) %>%
  mutate(continent = fct_relevel(continent, "Asia")) ->
  gapminder

m1 <- lifeExp ~ log(gdpPercap) + log(pop)
m2 <- lifeExp ~ log(gdpPercap) + log(pop) + continent
m3 <- lifeExp ~ log(gdpPercap) + log(pop) + continent + decade

Lesson 9: Present uncertainty

## Run some regression models
gapminder %>%
  mutate(decade = factor(decade)) %>%
  mutate(continent = fct_relevel(continent, "Asia")) ->
  gapminder

m1 <- lifeExp ~ log(gdpPercap) + log(pop)
m2 <- lifeExp ~ log(gdpPercap) + log(pop) + continent
m3 <- lifeExp ~ log(gdpPercap) + log(pop) + continent + decade

m1_res <- lm(m1, data = gapminder)

Lesson 9: Present uncertainty

## Run some regression models
gapminder %>%
  mutate(decade = factor(decade)) %>%
  mutate(continent = fct_relevel(continent, "Asia")) ->
  gapminder

m1 <- lifeExp ~ log(gdpPercap) + log(pop)
m2 <- lifeExp ~ log(gdpPercap) + log(pop) + continent
m3 <- lifeExp ~ log(gdpPercap) + log(pop) + continent + decade

m1_res <- lm(m1, data = gapminder)
m2_res <- lm(m2, data = gapminder)

Lesson 9: Present uncertainty

## Run some regression models
gapminder %>%
  mutate(decade = factor(decade)) %>%
  mutate(continent = fct_relevel(continent, "Asia")) ->
  gapminder

m1 <- lifeExp ~ log(gdpPercap) + log(pop)
m2 <- lifeExp ~ log(gdpPercap) + log(pop) + continent
m3 <- lifeExp ~ log(gdpPercap) + log(pop) + continent + decade

m1_res <- lm(m1, data = gapminder)
m2_res <- lm(m2, data = gapminder)
m3_res <- lm(m3, data = gapminder)

Lesson 9: Present uncertainty

library(modelsummary)

modelsummary(list(m1_res, m2_res, m3_res), 
             gof_map = c("nobs", "r.squared"),
             stars = TRUE) 
(1) (2) (3)
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
(Intercept) -28.771*** -7.341** 9.801***
(2.076) (2.488) (2.197)
log(gdpPercap) 8.344*** 6.587*** 5.090***
(0.143) (0.182) (0.163)
log(pop) 1.279*** 0.866*** 0.163+
(0.111) (0.111) (0.097)
continentAfrica -4.674*** -6.801***
(0.494) (0.423)
continentAmericas 1.495** 1.917***
(0.541) (0.454)
continentEurope 3.888*** 5.442***
(0.554) (0.469)
continentOceania 3.671* 5.697***
(1.487) (1.251)
decade1960 2.960***
(0.490)
decade1970 5.431***
(0.499)
decade1980 8.638***
(0.505)
decade1990 10.618***
(0.510)
decade2000 11.384***
(0.523)
Num.Obs. 1704 1704 1704
R2 0.677 0.714 0.799

Lesson 9: Present uncertainty

library(broom)

Lesson 9: Present uncertainty

library(broom)

tidy(m1_res, conf.int = T)
# A tibble: 3 × 7
  term           estimate std.error statistic  p.value conf.low conf.high
  <chr>             <dbl>     <dbl>     <dbl>    <dbl>    <dbl>     <dbl>
1 (Intercept)      -28.8      2.08      -13.9 1.78e-41   -32.8     -24.7 
2 log(gdpPercap)     8.34     0.143      58.2 0            8.06      8.63
3 log(pop)           1.28     0.111      11.5 1.12e-29     1.06      1.50

Lesson 9: Present uncertainty

library(broom)

tidy(m1_res, conf.int = T) %>%
  mutate(model = "M1: baseline")
# A tibble: 3 × 8
  term           estimate std.error statistic  p.value conf.low conf.high model 
  <chr>             <dbl>     <dbl>     <dbl>    <dbl>    <dbl>     <dbl> <chr> 
1 (Intercept)      -28.8      2.08      -13.9 1.78e-41   -32.8     -24.7  M1: b…
2 log(gdpPercap)     8.34     0.143      58.2 0            8.06      8.63 M1: b…
3 log(pop)           1.28     0.111      11.5 1.12e-29     1.06      1.50 M1: b…

Lesson 9: Present uncertainty

library(broom)

tidy(m1_res, conf.int = T) %>%
  mutate(model = "M1: baseline") %>%
  filter(term != "(Intercept)")
# A tibble: 2 × 8
  term           estimate std.error statistic  p.value conf.low conf.high model 
  <chr>             <dbl>     <dbl>     <dbl>    <dbl>    <dbl>     <dbl> <chr> 
1 log(gdpPercap)     8.34     0.143      58.2 0            8.06      8.63 M1: b…
2 log(pop)           1.28     0.111      11.5 1.12e-29     1.06      1.50 M1: b…

Lesson 9: Present uncertainty

library(broom)

tidy(m1_res, conf.int = T) %>%
  mutate(model = "M1: baseline") %>%
  filter(term != "(Intercept)") ->
  m1_res_tidy

Lesson 9: Present uncertainty

library(broom)

tidy(m1_res, conf.int = T) %>%
  mutate(model = "M1: baseline") %>%
  filter(term != "(Intercept)") ->
  m1_res_tidy

tidy(m2_res, conf.int = T)
# A tibble: 7 × 7
  term              estimate std.error statistic   p.value conf.low conf.high
  <chr>                <dbl>     <dbl>     <dbl>     <dbl>    <dbl>     <dbl>
1 (Intercept)         -7.34      2.49      -2.95 3.22e-  3  -12.2       -2.46
2 log(gdpPercap)       6.59      0.182     36.3  6.77e-214    6.23       6.94
3 log(pop)             0.866     0.111      7.83 8.48e- 15    0.649      1.08
4 continentAfrica     -4.67      0.494     -9.45 1.06e- 20   -5.64      -3.70
5 continentAmericas    1.49      0.541      2.76 5.77e-  3    0.434      2.56
6 continentEurope      3.89      0.554      7.02 3.30e- 12    2.80       4.98
7 continentOceania     3.67      1.49       2.47 1.36e-  2    0.755      6.59

Lesson 9: Present uncertainty

library(broom)

tidy(m1_res, conf.int = T) %>%
  mutate(model = "M1: baseline") %>%
  filter(term != "(Intercept)") ->
  m1_res_tidy

tidy(m2_res, conf.int = T) %>%
  mutate(model = "M2: w/ continent FEs")
# A tibble: 7 × 8
  term           estimate std.error statistic   p.value conf.low conf.high model
  <chr>             <dbl>     <dbl>     <dbl>     <dbl>    <dbl>     <dbl> <chr>
1 (Intercept)      -7.34      2.49      -2.95 3.22e-  3  -12.2       -2.46 M2: …
2 log(gdpPercap)    6.59      0.182     36.3  6.77e-214    6.23       6.94 M2: …
3 log(pop)          0.866     0.111      7.83 8.48e- 15    0.649      1.08 M2: …
4 continentAfri…   -4.67      0.494     -9.45 1.06e- 20   -5.64      -3.70 M2: …
5 continentAmer…    1.49      0.541      2.76 5.77e-  3    0.434      2.56 M2: …
6 continentEuro…    3.89      0.554      7.02 3.30e- 12    2.80       4.98 M2: …
7 continentOcea…    3.67      1.49       2.47 1.36e-  2    0.755      6.59 M2: …

Lesson 9: Present uncertainty

library(broom)

tidy(m1_res, conf.int = T) %>%
  mutate(model = "M1: baseline") %>%
  filter(term != "(Intercept)") ->
  m1_res_tidy

tidy(m2_res, conf.int = T) %>%
  mutate(model = "M2: w/ continent FEs") %>%
  filter(term != "(Intercept)")
# A tibble: 6 × 8
  term           estimate std.error statistic   p.value conf.low conf.high model
  <chr>             <dbl>     <dbl>     <dbl>     <dbl>    <dbl>     <dbl> <chr>
1 log(gdpPercap)    6.59      0.182     36.3  6.77e-214    6.23       6.94 M2: …
2 log(pop)          0.866     0.111      7.83 8.48e- 15    0.649      1.08 M2: …
3 continentAfri…   -4.67      0.494     -9.45 1.06e- 20   -5.64      -3.70 M2: …
4 continentAmer…    1.49      0.541      2.76 5.77e-  3    0.434      2.56 M2: …
5 continentEuro…    3.89      0.554      7.02 3.30e- 12    2.80       4.98 M2: …
6 continentOcea…    3.67      1.49       2.47 1.36e-  2    0.755      6.59 M2: …

Lesson 9: Present uncertainty

library(broom)

tidy(m1_res, conf.int = T) %>%
  mutate(model = "M1: baseline") %>%
  filter(term != "(Intercept)") ->
  m1_res_tidy

tidy(m2_res, conf.int = T) %>%
  mutate(model = "M2: w/ continent FEs") %>%
  filter(term != "(Intercept)") ->
  m2_res_tidy

Lesson 9: Present uncertainty

library(broom)

tidy(m1_res, conf.int = T) %>%
  mutate(model = "M1: baseline") %>%
  filter(term != "(Intercept)") ->
  m1_res_tidy

tidy(m2_res, conf.int = T) %>%
  mutate(model = "M2: w/ continent FEs") %>%
  filter(term != "(Intercept)") ->
  m2_res_tidy

tidy(m3_res, conf.int = T)
# A tibble: 12 × 7
   term              estimate std.error statistic   p.value conf.low conf.high
   <chr>                <dbl>     <dbl>     <dbl>     <dbl>    <dbl>     <dbl>
 1 (Intercept)          9.80     2.20        4.46 8.68e-  6   5.49      14.1  
 2 log(gdpPercap)       5.09     0.163      31.2  1.53e-169   4.77       5.41 
 3 log(pop)             0.163    0.0966      1.68 9.24e-  2  -0.0268     0.352
 4 continentAfrica     -6.80     0.423     -16.1  3.06e- 54  -7.63      -5.97 
 5 continentAmericas    1.92     0.454       4.22 2.57e-  5   1.03       2.81 
 6 continentEurope      5.44     0.469      11.6  5.65e- 30   4.52       6.36 
 7 continentOceania     5.70     1.25        4.55 5.65e-  6   3.24       8.15 
 8 decade1960           2.96     0.490       6.04 1.91e-  9   2.00       3.92 
 9 decade1970           5.43     0.499      10.9  1.02e- 26   4.45       6.41 
10 decade1980           8.64     0.505      17.1  1.54e- 60   7.65       9.63 
11 decade1990          10.6      0.510      20.8  8.75e- 86   9.62      11.6  
12 decade2000          11.4      0.523      21.8  6.19e- 93  10.4       12.4  

Lesson 9: Present uncertainty

library(broom)

tidy(m1_res, conf.int = T) %>%
  mutate(model = "M1: baseline") %>%
  filter(term != "(Intercept)") ->
  m1_res_tidy

tidy(m2_res, conf.int = T) %>%
  mutate(model = "M2: w/ continent FEs") %>%
  filter(term != "(Intercept)") ->
  m2_res_tidy

tidy(m3_res, conf.int = T) %>%
  mutate(model = "M3: w/ continent + decade FEs")
# A tibble: 12 × 8
   term          estimate std.error statistic   p.value conf.low conf.high model
   <chr>            <dbl>     <dbl>     <dbl>     <dbl>    <dbl>     <dbl> <chr>
 1 (Intercept)      9.80     2.20        4.46 8.68e-  6   5.49      14.1   M3: …
 2 log(gdpPerca…    5.09     0.163      31.2  1.53e-169   4.77       5.41  M3: …
 3 log(pop)         0.163    0.0966      1.68 9.24e-  2  -0.0268     0.352 M3: …
 4 continentAfr…   -6.80     0.423     -16.1  3.06e- 54  -7.63      -5.97  M3: …
 5 continentAme…    1.92     0.454       4.22 2.57e-  5   1.03       2.81  M3: …
 6 continentEur…    5.44     0.469      11.6  5.65e- 30   4.52       6.36  M3: …
 7 continentOce…    5.70     1.25        4.55 5.65e-  6   3.24       8.15  M3: …
 8 decade1960       2.96     0.490       6.04 1.91e-  9   2.00       3.92  M3: …
 9 decade1970       5.43     0.499      10.9  1.02e- 26   4.45       6.41  M3: …
10 decade1980       8.64     0.505      17.1  1.54e- 60   7.65       9.63  M3: …
11 decade1990      10.6      0.510      20.8  8.75e- 86   9.62      11.6   M3: …
12 decade2000      11.4      0.523      21.8  6.19e- 93  10.4       12.4   M3: …

Lesson 9: Present uncertainty

library(broom)

tidy(m1_res, conf.int = T) %>%
  mutate(model = "M1: baseline") %>%
  filter(term != "(Intercept)") ->
  m1_res_tidy

tidy(m2_res, conf.int = T) %>%
  mutate(model = "M2: w/ continent FEs") %>%
  filter(term != "(Intercept)") ->
  m2_res_tidy

tidy(m3_res, conf.int = T) %>%
  mutate(model = "M3: w/ continent + decade FEs") %>%
  filter(term != "(Intercept)")
# A tibble: 11 × 8
   term          estimate std.error statistic   p.value conf.low conf.high model
   <chr>            <dbl>     <dbl>     <dbl>     <dbl>    <dbl>     <dbl> <chr>
 1 log(gdpPerca…    5.09     0.163      31.2  1.53e-169   4.77       5.41  M3: …
 2 log(pop)         0.163    0.0966      1.68 9.24e-  2  -0.0268     0.352 M3: …
 3 continentAfr…   -6.80     0.423     -16.1  3.06e- 54  -7.63      -5.97  M3: …
 4 continentAme…    1.92     0.454       4.22 2.57e-  5   1.03       2.81  M3: …
 5 continentEur…    5.44     0.469      11.6  5.65e- 30   4.52       6.36  M3: …
 6 continentOce…    5.70     1.25        4.55 5.65e-  6   3.24       8.15  M3: …
 7 decade1960       2.96     0.490       6.04 1.91e-  9   2.00       3.92  M3: …
 8 decade1970       5.43     0.499      10.9  1.02e- 26   4.45       6.41  M3: …
 9 decade1980       8.64     0.505      17.1  1.54e- 60   7.65       9.63  M3: …
10 decade1990      10.6      0.510      20.8  8.75e- 86   9.62      11.6   M3: …
11 decade2000      11.4      0.523      21.8  6.19e- 93  10.4       12.4   M3: …

Lesson 9: Present uncertainty

library(broom)

tidy(m1_res, conf.int = T) %>%
  mutate(model = "M1: baseline") %>%
  filter(term != "(Intercept)") ->
  m1_res_tidy

tidy(m2_res, conf.int = T) %>%
  mutate(model = "M2: w/ continent FEs") %>%
  filter(term != "(Intercept)") ->
  m2_res_tidy

tidy(m3_res, conf.int = T) %>%
  mutate(model = "M3: w/ continent + decade FEs") %>%
  filter(term != "(Intercept)") ->
  m3_res_tidy

Lesson 9: Present uncertainty

library(broom)

tidy(m1_res, conf.int = T) %>%
  mutate(model = "M1: baseline") %>%
  filter(term != "(Intercept)") ->
  m1_res_tidy

tidy(m2_res, conf.int = T) %>%
  mutate(model = "M2: w/ continent FEs") %>%
  filter(term != "(Intercept)") ->
  m2_res_tidy

tidy(m3_res, conf.int = T) %>%
  mutate(model = "M3: w/ continent + decade FEs") %>%
  filter(term != "(Intercept)") ->
  m3_res_tidy

all_res_tidy <- bind_rows(m1_res_tidy, m2_res_tidy, m3_res_tidy)

Lesson 9: Present uncertainty

ggplot(m2_res_tidy,
       aes(y = fct_reorder(term, estimate),
           x = estimate,
           xmin = conf.low,
           xmax = conf.high))

Lesson 9: Present uncertainty

ggplot(m2_res_tidy,
       aes(y = fct_reorder(term, estimate),
           x = estimate,
           xmin = conf.low,
           xmax = conf.high)) +
  theme_cavis_vgrid

Lesson 9: Present uncertainty

ggplot(m2_res_tidy,
       aes(y = fct_reorder(term, estimate),
           x = estimate,
           xmin = conf.low,
           xmax = conf.high)) +
  theme_cavis_vgrid +
  geom_vline(
    xintercept = 1,
    linetype = 2,
    color = "grey"
  )

Lesson 9: Present uncertainty

ggplot(m2_res_tidy,
       aes(y = fct_reorder(term, estimate),
           x = estimate,
           xmin = conf.low,
           xmax = conf.high)) +
  theme_cavis_vgrid +
  geom_vline(
    xintercept = 1,
    linetype = 2,
    color = "grey"
  ) +
  geom_pointrange()

Lesson 9: Present uncertainty

ggplot(m2_res_tidy,
       aes(y = fct_reorder(term, estimate),
           x = estimate,
           xmin = conf.low,
           xmax = conf.high)) +
  theme_cavis_vgrid +
  geom_vline(
    xintercept = 1,
    linetype = 2,
    color = "grey"
  ) +
  geom_pointrange() +
  scale_x_continuous(
    sec.axis = dup_axis(name = NULL)
  )

Lesson 9: Present uncertainty

ggplot(m2_res_tidy,
       aes(y = fct_reorder(term, estimate),
           x = estimate,
           xmin = conf.low,
           xmax = conf.high)) +
  theme_cavis_vgrid +
  geom_vline(
    xintercept = 1,
    linetype = 2,
    color = "grey"
  ) +
  geom_pointrange() +
  scale_x_continuous(
    sec.axis = dup_axis(name = NULL)
  ) +
  labs(y = NULL, x = "Estimate")

Lesson 9: Present uncertainty

blues <- RColorBrewer::brewer.pal(9, "Blues")
blues <- blues[c(3, 6, 9)]

all_res_tidy
# A tibble: 19 × 8
   term          estimate std.error statistic   p.value conf.low conf.high model
   <chr>            <dbl>     <dbl>     <dbl>     <dbl>    <dbl>     <dbl> <chr>
 1 log(gdpPerca…    8.34     0.143      58.2  0           8.06       8.63  M1: …
 2 log(pop)         1.28     0.111      11.5  1.12e- 29   1.06       1.50  M1: …
 3 log(gdpPerca…    6.59     0.182      36.3  6.77e-214   6.23       6.94  M2: …
 4 log(pop)         0.866    0.111       7.83 8.48e- 15   0.649      1.08  M2: …
 5 continentAfr…   -4.67     0.494      -9.45 1.06e- 20  -5.64      -3.70  M2: …
 6 continentAme…    1.49     0.541       2.76 5.77e-  3   0.434      2.56  M2: …
 7 continentEur…    3.89     0.554       7.02 3.30e- 12   2.80       4.98  M2: …
 8 continentOce…    3.67     1.49        2.47 1.36e-  2   0.755      6.59  M2: …
 9 log(gdpPerca…    5.09     0.163      31.2  1.53e-169   4.77       5.41  M3: …
10 log(pop)         0.163    0.0966      1.68 9.24e-  2  -0.0268     0.352 M3: …
11 continentAfr…   -6.80     0.423     -16.1  3.06e- 54  -7.63      -5.97  M3: …
12 continentAme…    1.92     0.454       4.22 2.57e-  5   1.03       2.81  M3: …
13 continentEur…    5.44     0.469      11.6  5.65e- 30   4.52       6.36  M3: …
14 continentOce…    5.70     1.25        4.55 5.65e-  6   3.24       8.15  M3: …
15 decade1960       2.96     0.490       6.04 1.91e-  9   2.00       3.92  M3: …
16 decade1970       5.43     0.499      10.9  1.02e- 26   4.45       6.41  M3: …
17 decade1980       8.64     0.505      17.1  1.54e- 60   7.65       9.63  M3: …
18 decade1990      10.6      0.510      20.8  8.75e- 86   9.62      11.6   M3: …
19 decade2000      11.4      0.523      21.8  6.19e- 93  10.4       12.4   M3: …

Lesson 9: Present uncertainty

blues <- RColorBrewer::brewer.pal(9, "Blues")
blues <- blues[c(3, 6, 9)]

all_res_tidy %>%
  filter(term %in% c("log(gdpPercap)", "log(pop)"))
# A tibble: 6 × 8
  term           estimate std.error statistic   p.value conf.low conf.high model
  <chr>             <dbl>     <dbl>     <dbl>     <dbl>    <dbl>     <dbl> <chr>
1 log(gdpPercap)    8.34     0.143      58.2  0           8.06       8.63  M1: …
2 log(pop)          1.28     0.111      11.5  1.12e- 29   1.06       1.50  M1: …
3 log(gdpPercap)    6.59     0.182      36.3  6.77e-214   6.23       6.94  M2: …
4 log(pop)          0.866    0.111       7.83 8.48e- 15   0.649      1.08  M2: …
5 log(gdpPercap)    5.09     0.163      31.2  1.53e-169   4.77       5.41  M3: …
6 log(pop)          0.163    0.0966      1.68 9.24e-  2  -0.0268     0.352 M3: …

Lesson 9: Present uncertainty

blues <- RColorBrewer::brewer.pal(9, "Blues")
blues <- blues[c(3, 6, 9)]

all_res_tidy %>%
  filter(term %in% c("log(gdpPercap)", "log(pop)")) %>%
  ggplot(aes(y = fct_reorder(term, estimate),
             x = estimate,
             xmin = conf.low,
             xmax = conf.high,
             color = model))

Lesson 9: Present uncertainty

blues <- RColorBrewer::brewer.pal(9, "Blues")
blues <- blues[c(3, 6, 9)]

all_res_tidy %>%
  filter(term %in% c("log(gdpPercap)", "log(pop)")) %>%
  ggplot(aes(y = fct_reorder(term, estimate),
             x = estimate,
             xmin = conf.low,
             xmax = conf.high,
             color = model)) +
  theme_cavis_vgrid

Lesson 9: Present uncertainty

blues <- RColorBrewer::brewer.pal(9, "Blues")
blues <- blues[c(3, 6, 9)]

all_res_tidy %>%
  filter(term %in% c("log(gdpPercap)", "log(pop)")) %>%
  ggplot(aes(y = fct_reorder(term, estimate),
             x = estimate,
             xmin = conf.low,
             xmax = conf.high,
             color = model)) +
  theme_cavis_vgrid +
  geom_vline(xintercept = 0, linetype = 2, color = "grey")

Lesson 9: Present uncertainty

blues <- RColorBrewer::brewer.pal(9, "Blues")
blues <- blues[c(3, 6, 9)]

all_res_tidy %>%
  filter(term %in% c("log(gdpPercap)", "log(pop)")) %>%
  ggplot(aes(y = fct_reorder(term, estimate),
             x = estimate,
             xmin = conf.low,
             xmax = conf.high,
             color = model)) +
  theme_cavis_vgrid +
  geom_vline(xintercept = 0, linetype = 2, color = "grey") +
  geom_pointrange(
  )

Lesson 9: Present uncertainty

blues <- RColorBrewer::brewer.pal(9, "Blues")
blues <- blues[c(3, 6, 9)]

all_res_tidy %>%
  filter(term %in% c("log(gdpPercap)", "log(pop)")) %>%
  ggplot(aes(y = fct_reorder(term, estimate),
             x = estimate,
             xmin = conf.low,
             xmax = conf.high,
             color = model)) +
  theme_cavis_vgrid +
  geom_vline(xintercept = 0, linetype = 2, color = "grey") +
  geom_pointrange(
    position = position_dodge2(width = 0.5)
  )

Lesson 9: Present uncertainty

blues <- RColorBrewer::brewer.pal(9, "Blues")
blues <- blues[c(3, 6, 9)]

all_res_tidy %>%
  filter(term %in% c("log(gdpPercap)", "log(pop)")) %>%
  ggplot(aes(y = fct_reorder(term, estimate),
             x = estimate,
             xmin = conf.low,
             xmax = conf.high,
             color = model)) +
  theme_cavis_vgrid +
  geom_vline(xintercept = 0, linetype = 2, color = "grey") +
  geom_pointrange(
    position = position_dodge2(width = 0.5)
  ) +
  scale_color_manual(values = blues)

Lesson 9: Present uncertainty

blues <- RColorBrewer::brewer.pal(9, "Blues")
blues <- blues[c(3, 6, 9)]

all_res_tidy %>%
  filter(term %in% c("log(gdpPercap)", "log(pop)")) %>%
  ggplot(aes(y = fct_reorder(term, estimate),
             x = estimate,
             xmin = conf.low,
             xmax = conf.high,
             color = model)) +
  theme_cavis_vgrid +
  geom_vline(xintercept = 0, linetype = 2, color = "grey") +
  geom_pointrange(
    position = position_dodge2(width = 0.5)
  ) +
  scale_color_manual(values = blues) +
  theme(legend.position = "top")

Lesson 9: Present uncertainty

blues <- RColorBrewer::brewer.pal(9, "Blues")
blues <- blues[c(3, 6, 9)]

all_res_tidy %>%
  filter(term %in% c("log(gdpPercap)", "log(pop)")) %>%
  ggplot(aes(y = fct_reorder(term, estimate),
             x = estimate,
             xmin = conf.low,
             xmax = conf.high,
             color = model)) +
  theme_cavis_vgrid +
  geom_vline(xintercept = 0, linetype = 2, color = "grey") +
  geom_pointrange(
    position = position_dodge2(width = 0.5)
  ) +
  scale_color_manual(values = blues) +
  theme(legend.position = "top") +
  scale_x_continuous(
    sec.axis = dup_axis(name = NULL)
  ) +
  labs(y = NULL, x = "Estimate")

Lesson 10: Save graphics with code

Lesson 10: Save graphics with code

Please don’t use:

Lesson 10: Save graphics with code

ggplot(gapminder_aa_07, aes(x = gdpPercap, y = lifeExp)) +
  theme_cavis_grids +
  geom_point() +
  scale_x_continuous(trans = "log10") +
  geom_smooth(method = "lm", color = "grey30") +
  labs(y = "Life expectancy", 
       x = "GDP per capita (logged)") ->
  plot1

plot1

Lesson 10: Save graphics with code

width = 7
ggsave("plot/gdp_lifExp_width7.png", 
       width = width, 
       height = width/1.618)

Lesson 10: Save graphics with code

width = 14
ggsave("plot/gdp_lifExp_width14.png", 
       width = width, 
       height = width/1.618)

Lesson 10: Save graphics with code

  • PNG is a raster image with lossless compression (better quality than JPG)

  • But PDF – vector image – is often preferred, especially if you work with LaTeX

width = 8
ggsave("plot/gdp_lifExp.pdf", 
       width = width, 
       height = width/1.618,
       device = cairo_pdf # only for fancy font 
       )

Thank you

Website: https://brianleungkp.github.io/

Email: kpleung@uw.edu