Slides here

# Necessary packages
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.3     ✓ purrr   0.3.4
## ✓ tibble  3.1.0     ✓ dplyr   1.0.5
## ✓ tidyr   1.1.3     ✓ stringr 1.4.0
## ✓ readr   1.4.0     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(gapminder)

gapminder
## # A tibble: 1,704 x 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
## Reduce the dataset to a summarized form
gapminder_sum <- gapminder %>%
    group_by(continent, year) %>%
    summarise(AvgLifeExp=mean(lifeExp),
                        AvgPop=mean(pop),
                        AvgGDP=mean(gdpPercap)) %>%
    filter(year>1990)
## `summarise()` has grouped output by 'continent'. You can override using the `.groups` argument.
gapminder_sum
## # A tibble: 20 x 5
## # Groups:   continent [5]
##    continent  year AvgLifeExp     AvgPop AvgGDP
##    <fct>     <int>      <dbl>      <dbl>  <dbl>
##  1 Africa     1992       53.6  12674645.  2282.
##  2 Africa     1997       53.6  14304480.  2379.
##  3 Africa     2002       53.3  16033152.  2599.
##  4 Africa     2007       54.8  17875763.  3089.
##  5 Americas   1992       69.6  29570964.  8045.
##  6 Americas   1997       71.2  31876016.  8889.
##  7 Americas   2002       72.4  33990910.  9288.
##  8 Americas   2007       73.6  35954847. 11003.
##  9 Asia       1992       66.5  94948248.  8640.
## 10 Asia       1997       68.0 102523803.  9834.
## 11 Asia       2002       69.2 109145521. 10174.
## 12 Asia       2007       70.7 115513752. 12473.
## 13 Europe     1992       74.4  18604760. 17062.
## 14 Europe     1997       75.5  18964805. 19077.
## 15 Europe     2002       76.7  19274129. 21712.
## 16 Europe     2007       77.6  19536618. 25054.
## 17 Oceania    1992       76.9  10459826. 20894.
## 18 Oceania    1997       78.2  11120715  24024.
## 19 Oceania    2002       79.7  11727414. 26939.
## 20 Oceania    2007       80.7  12274974. 29810.

Creating tables using GT

library(gt)

Simple GT table

gapminder_gt1 <- gapminder_sum %>%
    gt()
gapminder_gt1
year AvgLifeExp AvgPop AvgGDP
Africa
1992 53.62958 12674645 2281.810
1997 53.59827 14304480 2378.760
2002 53.32523 16033152 2599.385
2007 54.80604 17875763 3089.033
Americas
1992 69.56836 29570964 8044.934
1997 71.15048 31876016 8889.301
2002 72.42204 33990910 9287.677
2007 73.60812 35954847 11003.032
Asia
1992 66.53721 94948248 8639.690
1997 68.02052 102523803 9834.093
2002 69.23388 109145521 10174.090
2007 70.72848 115513752 12473.027
Europe
1992 74.44010 18604760 17061.568
1997 75.50517 18964805 19076.782
2002 76.70060 19274129 21711.732
2007 77.64860 19536618 25054.482
Oceania
1992 76.94500 10459826 20894.046
1997 78.19000 11120715 24024.175
2002 79.74000 11727414 26938.778
2007 80.71950 12274974 29810.188

Group columns

gapminder_gt3 <- gapminder_gt2 %>%
    tab_spanner(label="Location", columns=matches("country|continent")) %>%
    tab_spanner(label="Stats", columns=matches("lifeExp|pop|gdp"))
gapminder_gt3
Gapminder Table
using GT
year Stats
AvgLifeExp AvgPop AvgGDP
Africa
1992 53.62958 12674645 2281.810
1997 53.59827 14304480 2378.760
2002 53.32523 16033152 2599.385
2007 54.80604 17875763 3089.033
Americas
1992 69.56836 29570964 8044.934
1997 71.15048 31876016 8889.301
2002 72.42204 33990910 9287.677
2007 73.60812 35954847 11003.032
Asia
1992 66.53721 94948248 8639.690
1997 68.02052 102523803 9834.093
2002 69.23388 109145521 10174.090
2007 70.72848 115513752 12473.027
Europe
1992 74.44010 18604760 17061.568
1997 75.50517 18964805 19076.782
2002 76.70060 19274129 21711.732
2007 77.64860 19536618 25054.482
Oceania
1992 76.94500 10459826 20894.046
1997 78.19000 11120715 24024.175
2002 79.74000 11727414 26938.778
2007 80.71950 12274974 29810.188
More information on the GT package.

Group data and add color

gapminder_gt4 <- gapminder_gt3 %>%
    tab_options(
        # Row groups
        row_group.background.color="#3C5488", #FFEFDB80
        row_group.border.top.color="#989898",
    )
gapminder_gt4
Gapminder Table
using GT
year Stats
AvgLifeExp AvgPop AvgGDP
Africa
1992 53.62958 12674645 2281.810
1997 53.59827 14304480 2378.760
2002 53.32523 16033152 2599.385
2007 54.80604 17875763 3089.033
Americas
1992 69.56836 29570964 8044.934
1997 71.15048 31876016 8889.301
2002 72.42204 33990910 9287.677
2007 73.60812 35954847 11003.032
Asia
1992 66.53721 94948248 8639.690
1997 68.02052 102523803 9834.093
2002 69.23388 109145521 10174.090
2007 70.72848 115513752 12473.027
Europe
1992 74.44010 18604760 17061.568
1997 75.50517 18964805 19076.782
2002 76.70060 19274129 21711.732
2007 77.64860 19536618 25054.482
Oceania
1992 76.94500 10459826 20894.046
1997 78.19000 11120715 24024.175
2002 79.74000 11727414 26938.778
2007 80.71950 12274974 29810.188
More information on the GT package.

Add LOTS more color!

gapminder_gt5 <- gapminder_gt4 %>%
    tab_options(
        # Headings; Titles
        heading.background.color="#3C5488",
        heading.border.bottom.color="#989898",
        heading.title.font.size="12px",
        heading.subtitle.font.size="11px",
        # Column labels
        column_labels.background.color="#4DBBD5", #B09C85FF
        column_labels.font.size="12px",
        # Stubs
        stub.background.color="#4DBBD5", #B09C85FF
        stub.border.style="dashed",
        stub.border.color="#989898",
        stub.border.width="1px",
        # Row groups
        row_group.background.color="#3C5488", #FFEFDB80
        row_group.border.top.color="#989898",
        row_group.border.bottom.style="none",
        row_group.font.size="12px",
        # Summary rows
        summary_row.border.color="#989898",
        # summary_row.background.color="#FFEBEE",
        # grand_summary_row.background.color="#FFFFFF",
        # Table
        table.font.color="#323232",
        table_body.hlines.color="#989898",
        table_body.border.top.color="#989898",
        table.font.size="10px",
        table.width="80%"
    )
gapminder_gt5
Gapminder Table
using GT
year Stats
AvgLifeExp AvgPop AvgGDP
Africa
1992 53.62958 12674645 2281.810
1997 53.59827 14304480 2378.760
2002 53.32523 16033152 2599.385
2007 54.80604 17875763 3089.033
Americas
1992 69.56836 29570964 8044.934
1997 71.15048 31876016 8889.301
2002 72.42204 33990910 9287.677
2007 73.60812 35954847 11003.032
Asia
1992 66.53721 94948248 8639.690
1997 68.02052 102523803 9834.093
2002 69.23388 109145521 10174.090
2007 70.72848 115513752 12473.027
Europe
1992 74.44010 18604760 17061.568
1997 75.50517 18964805 19076.782
2002 76.70060 19274129 21711.732
2007 77.64860 19536618 25054.482
Oceania
1992 76.94500 10459826 20894.046
1997 78.19000 11120715 24024.175
2002 79.74000 11727414 26938.778
2007 80.71950 12274974 29810.188
More information on the GT package.

How to reach me?