Tables

Lecture 22

Dr. Benjamin Soltoff

Cornell University
INFO 3312/5312 - Spring 2024

April 25, 2023

Announcements

Announcements

  • Project 2

Setup

Packages + figures

# load packages
library(tidyverse)
library(scales)
library(colorspace)
library(colorblindr)
library(gt)

# set default theme for ggplot2
theme_set(theme_minimal(base_size = 12))

YAML options

knitr:
  opts_chunk:
    fig-width: 7
    fig-asp: 0.618
    fig.retina: 2
    dpi: 150
    out-width: "80%"

Data in tables

Tables vs. plots

Tables:

  • To look up or compare individual values
  • To display precise values
  • To include detail and summary values
  • To display quantitative values including more than one unit of measure

Plots:

  • To reveal relationships among whole sets of values
  • To display a message that is contained in the shape of the values (e.g., patterns, trends, exceptions)

Bachelor’s degrees

BA_degrees <- read_csv(here::here("slides/data/BA_degrees.csv"))
BA_degrees
# A tibble: 594 × 4
   field                                              year  count     perc
   <chr>                                             <dbl>  <dbl>    <dbl>
 1 Agriculture and natural resources                  1971  12672 0.0151  
 2 Architecture and related services                  1971   5570 0.00663 
 3 Area, ethnic, cultural, gender, and group studies  1971   2579 0.00307 
 4 Biological and biomedical sciences                 1971  35705 0.0425  
 5 Business                                           1971 115396 0.137   
 6 Communication, journalism, and related programs    1971  10324 0.0123  
 7 Communications technologies                        1971    478 0.000569
 8 Computer and information sciences                  1971   2388 0.00284 
 9 Education                                          1971 176307 0.210   
10 Engineering                                        1971  45034 0.0536  
# ℹ 584 more rows

In the next few slides:


Degrees awarded in 2015

# A tibble: 33 × 2
   field                                                           perc
   <chr>                                                          <dbl>
 1 Agriculture and natural resources                          0.0191   
 2 Architecture and related services                          0.00480  
 3 Area, ethnic, cultural, gender, and group studies          0.00411  
 4 Biological and biomedical sciences                         0.0580   
 5 Business                                                   0.192    
 6 Communication, journalism, and related programs            0.0478   
 7 Communications technologies                                0.00271  
 8 Computer and information sciences                          0.0314   
 9 Education                                                  0.0484   
10 Engineering                                                0.0516   
11 Engineering technologies                                   0.00910  
12 English language and literature/letters                    0.0242   
13 Family and consumer sciences/human sciences                0.0130   
14 Foreign languages, literatures, and linguistics            0.0103   
15 Health professions and related programs                    0.114    
16 Homeland security, law enforcement, and firefighting       0.0331   
17 Legal professions and studies                              0.00233  
18 Liberal arts and sciences, general studies, and humanities 0.0230   
19 Library science                                            0.0000522
20 Mathematics and statistics                                 0.0115   
21 Military technologies and applied sciences                 0.000146 
22 Multi/interdisciplinary studies                            0.0251   
23 Parks, recreation, leisure, and fitness studies            0.0259   
24 Philosophy and religious studies                           0.00584  
25 Physical sciences and science technologies                 0.0159   
26 Precision production                                       0.0000253
27 Psychology                                                 0.0620   
28 Public administration and social services                  0.0181   
29 Social sciences and history                                0.0881   
30 Theology and religious vocations                           0.00512  
31 Transportation and materials moving                        0.00249  
32 Visual and performing arts                                 0.0506   
33 Not classified by field of study                           0        

# A tibble: 33 × 2
   field                                                           perc
   <chr>                                                          <dbl>
 1 Business                                                   0.192    
 2 Health professions and related programs                    0.114    
 3 Social sciences and history                                0.0881   
 4 Psychology                                                 0.0620   
 5 Biological and biomedical sciences                         0.0580   
 6 Engineering                                                0.0516   
 7 Visual and performing arts                                 0.0506   
 8 Education                                                  0.0484   
 9 Communication, journalism, and related programs            0.0478   
10 Homeland security, law enforcement, and firefighting       0.0331   
11 Computer and information sciences                          0.0314   
12 Parks, recreation, leisure, and fitness studies            0.0259   
13 Multi/interdisciplinary studies                            0.0251   
14 English language and literature/letters                    0.0242   
15 Liberal arts and sciences, general studies, and humanities 0.0230   
16 Agriculture and natural resources                          0.0191   
17 Public administration and social services                  0.0181   
18 Physical sciences and science technologies                 0.0159   
19 Family and consumer sciences/human sciences                0.0130   
20 Mathematics and statistics                                 0.0115   
21 Foreign languages, literatures, and linguistics            0.0103   
22 Engineering technologies                                   0.00910  
23 Philosophy and religious studies                           0.00584  
24 Theology and religious vocations                           0.00512  
25 Architecture and related services                          0.00480  
26 Area, ethnic, cultural, gender, and group studies          0.00411  
27 Communications technologies                                0.00271  
28 Transportation and materials moving                        0.00249  
29 Legal professions and studies                              0.00233  
30 Military technologies and applied sciences                 0.000146 
31 Library science                                            0.0000522
32 Precision production                                       0.0000253
33 Not classified by field of study                           0        

Field Percentage
Business 19.2%
Health professions and related programs 11.4%
Social sciences and history 8.8%
Psychology 6.2%
Biological and biomedical sciences 5.8%
Engineering 5.2%
Visual and performing arts 5.1%
Education 4.8%
Communication, journalism, and related programs 4.8%
Homeland security, law enforcement, and firefighting 3.3%
Computer and information sciences 3.1%
Parks, recreation, leisure, and fitness studies 2.6%
Multi/interdisciplinary studies 2.5%
English language and literature/letters 2.4%
Liberal arts and sciences, general studies, and humanities 2.3%
Agriculture and natural resources 1.9%
Public administration and social services 1.8%
Physical sciences and science technologies 1.6%
Family and consumer sciences/human sciences 1.3%
Mathematics and statistics 1.2%
Foreign languages, literatures, and linguistics 1.0%
Engineering technologies 0.9%
Philosophy and religious studies 0.6%
Theology and religious vocations 0.5%
Architecture and related services 0.5%
Area, ethnic, cultural, gender, and group studies 0.4%
Communications technologies 0.3%
Transportation and materials moving 0.2%
Legal professions and studies 0.2%
Military technologies and applied sciences 0.0%
Library science 0.0%
Precision production 0.0%
Not classified by field of study 0.0%

In the next few slides:


Degrees awarded in 2015

How should this information be displayed? And why?

In a table?

Popular Bachelor's degrees over the years
Year Business Health professions Social sciences and history Other
1971 13.7% 3.0% 18.5% 64.8%
1976 15.5% 5.8% 13.7% 65.1%
1981 21.4% 6.8% 10.7% 61.0%
1986 24.0% 6.6% 9.5% 59.9%
1991 22.8% 5.5% 11.4% 60.3%
1996 19.5% 7.4% 10.9% 62.3%
2001 21.2% 6.1% 10.3% 62.4%
2005 21.6% 5.6% 10.9% 61.8%
2006 21.4% 6.2% 10.9% 61.5%
2007 21.5% 6.7% 10.8% 61.1%
2008 21.4% 7.1% 10.7% 60.7%
2009 21.7% 7.5% 10.5% 60.2%
2010 21.7% 7.9% 10.5% 60.0%
2011 21.3% 8.4% 10.3% 60.0%
2012 20.5% 9.1% 10.0% 60.4%
2013 19.6% 9.8% 9.7% 60.9%
2014 19.1% 10.6% 9.3% 61.0%
2015 19.2% 11.4% 8.8% 60.6%

Or in a plot?

Tables, the making of

Tables with gt

We will use the gt (Grammar of Tables) package to create tables in R.

The gt philosophy: we can construct a wide variety of useful tables with a cohesive set of table parts.

Source: gt.rstudio.com

Generating a gt table

Start with the data frame

BA_degrees
# A tibble: 594 × 4
   field                                              year  count     perc
   <chr>                                             <dbl>  <dbl>    <dbl>
 1 Agriculture and natural resources                  1971  12672 0.0151  
 2 Architecture and related services                  1971   5570 0.00663 
 3 Area, ethnic, cultural, gender, and group studies  1971   2579 0.00307 
 4 Biological and biomedical sciences                 1971  35705 0.0425  
 5 Business                                           1971 115396 0.137   
 6 Communication, journalism, and related programs    1971  10324 0.0123  
 7 Communications technologies                        1971    478 0.000569
 8 Computer and information sciences                  1971   2388 0.00284 
 9 Education                                          1971 176307 0.210   
10 Engineering                                        1971  45034 0.0536  
# ℹ 584 more rows

Generating a gt table

Filter to keep desired rows

BA_degrees |>
  filter(year == 2015)
# A tibble: 33 × 4
   field                                              year  count    perc
   <chr>                                             <dbl>  <dbl>   <dbl>
 1 Agriculture and natural resources                  2015  36277 0.0191 
 2 Architecture and related services                  2015   9090 0.00480
 3 Area, ethnic, cultural, gender, and group studies  2015   7782 0.00411
 4 Biological and biomedical sciences                 2015 109896 0.0580 
 5 Business                                           2015 363799 0.192  
 6 Communication, journalism, and related programs    2015  90650 0.0478 
 7 Communications technologies                        2015   5135 0.00271
 8 Computer and information sciences                  2015  59581 0.0314 
 9 Education                                          2015  91623 0.0484 
10 Engineering                                        2015  97858 0.0516 
# ℹ 23 more rows

Generating a gt table

Select desired columns

BA_degrees |>
  filter(year == 2015) |>
  select(field, perc)
# A tibble: 33 × 2
   field                                                perc
   <chr>                                               <dbl>
 1 Agriculture and natural resources                 0.0191 
 2 Architecture and related services                 0.00480
 3 Area, ethnic, cultural, gender, and group studies 0.00411
 4 Biological and biomedical sciences                0.0580 
 5 Business                                          0.192  
 6 Communication, journalism, and related programs   0.0478 
 7 Communications technologies                       0.00271
 8 Computer and information sciences                 0.0314 
 9 Education                                         0.0484 
10 Engineering                                       0.0516 
# ℹ 23 more rows

Generating a gt table

Arrange the row order

BA_degrees |>
  filter(year == 2015) |>
  select(field, perc) |>
  arrange(desc(perc))
# A tibble: 33 × 2
   field                                                  perc
   <chr>                                                 <dbl>
 1 Business                                             0.192 
 2 Health professions and related programs              0.114 
 3 Social sciences and history                          0.0881
 4 Psychology                                           0.0620
 5 Biological and biomedical sciences                   0.0580
 6 Engineering                                          0.0516
 7 Visual and performing arts                           0.0506
 8 Education                                            0.0484
 9 Communication, journalism, and related programs      0.0478
10 Homeland security, law enforcement, and firefighting 0.0331
# ℹ 23 more rows

Generating a gt table

Initialize the gt() object

BA_degrees |>
  filter(year == 2015) |>
  select(field, perc) |>
  arrange(desc(perc)) |>
  gt()
field perc
Business 1.919851e-01
Health professions and related programs 1.141085e-01
Social sciences and history 8.810017e-02
Psychology 6.203752e-02
Biological and biomedical sciences 5.799463e-02
Engineering 5.164190e-02
Visual and performing arts 5.057274e-02
Education 4.835155e-02
Communication, journalism, and related programs 4.783808e-02
Homeland security, law enforcement, and firefighting 3.310036e-02
Computer and information sciences 3.144226e-02
Parks, recreation, leisure, and fitness studies 2.586159e-02
Multi/interdisciplinary studies 2.509639e-02
English language and literature/letters 2.419451e-02
Liberal arts and sciences, general studies, and humanities 2.303352e-02
Agriculture and natural resources 1.914420e-02
Public administration and social services 1.813414e-02
Physical sciences and science technologies 1.585174e-02
Family and consumer sciences/human sciences 1.297354e-02
Mathematics and statistics 1.153233e-02
Foreign languages, literatures, and linguistics 1.028690e-02
Engineering technologies 9.096887e-03
Philosophy and religious studies 5.842948e-03
Theology and religious vocations 5.123134e-03
Architecture and related services 4.797001e-03
Area, ethnic, cultural, gender, and group studies 4.106739e-03
Communications technologies 2.709857e-03
Transportation and materials moving 2.486102e-03
Legal professions and studies 2.332535e-03
Military technologies and applied sciences 1.456515e-04
Library science 5.224456e-05
Precision production 2.533070e-05
Not classified by field of study 0.000000e+00

Generating a gt table

Decrease vertical padding

BA_degrees |>
  filter(year == 2015) |>
  select(field, perc) |>
  arrange(desc(perc)) |>
  gt() |>
  tab_style(
    style = "padding-top:0px;padding-bottom:0px;",
    locations = cells_body(columns = everything())
  )
field perc
Business 1.919851e-01
Health professions and related programs 1.141085e-01
Social sciences and history 8.810017e-02
Psychology 6.203752e-02
Biological and biomedical sciences 5.799463e-02
Engineering 5.164190e-02
Visual and performing arts 5.057274e-02
Education 4.835155e-02
Communication, journalism, and related programs 4.783808e-02
Homeland security, law enforcement, and firefighting 3.310036e-02
Computer and information sciences 3.144226e-02
Parks, recreation, leisure, and fitness studies 2.586159e-02
Multi/interdisciplinary studies 2.509639e-02
English language and literature/letters 2.419451e-02
Liberal arts and sciences, general studies, and humanities 2.303352e-02
Agriculture and natural resources 1.914420e-02
Public administration and social services 1.813414e-02
Physical sciences and science technologies 1.585174e-02
Family and consumer sciences/human sciences 1.297354e-02
Mathematics and statistics 1.153233e-02
Foreign languages, literatures, and linguistics 1.028690e-02
Engineering technologies 9.096887e-03
Philosophy and religious studies 5.842948e-03
Theology and religious vocations 5.123134e-03
Architecture and related services 4.797001e-03
Area, ethnic, cultural, gender, and group studies 4.106739e-03
Communications technologies 2.709857e-03
Transportation and materials moving 2.486102e-03
Legal professions and studies 2.332535e-03
Military technologies and applied sciences 1.456515e-04
Library science 5.224456e-05
Precision production 2.533070e-05
Not classified by field of study 0.000000e+00

Generating a gt table

Decrease font size

BA_degrees |>
  filter(year == 2015) |>
  select(field, perc) |>
  arrange(desc(perc)) |>
  gt() |>
  tab_style(
    style = "padding-top:0px;padding-bottom:0px;",
    locations = cells_body(columns = everything())
  ) |>
  tab_style(
    style = cell_text(size = "small"),
    locations = cells_body(columns = everything())
  )
field perc
Business 1.919851e-01
Health professions and related programs 1.141085e-01
Social sciences and history 8.810017e-02
Psychology 6.203752e-02
Biological and biomedical sciences 5.799463e-02
Engineering 5.164190e-02
Visual and performing arts 5.057274e-02
Education 4.835155e-02
Communication, journalism, and related programs 4.783808e-02
Homeland security, law enforcement, and firefighting 3.310036e-02
Computer and information sciences 3.144226e-02
Parks, recreation, leisure, and fitness studies 2.586159e-02
Multi/interdisciplinary studies 2.509639e-02
English language and literature/letters 2.419451e-02
Liberal arts and sciences, general studies, and humanities 2.303352e-02
Agriculture and natural resources 1.914420e-02
Public administration and social services 1.813414e-02
Physical sciences and science technologies 1.585174e-02
Family and consumer sciences/human sciences 1.297354e-02
Mathematics and statistics 1.153233e-02
Foreign languages, literatures, and linguistics 1.028690e-02
Engineering technologies 9.096887e-03
Philosophy and religious studies 5.842948e-03
Theology and religious vocations 5.123134e-03
Architecture and related services 4.797001e-03
Area, ethnic, cultural, gender, and group studies 4.106739e-03
Communications technologies 2.709857e-03
Transportation and materials moving 2.486102e-03
Legal professions and studies 2.332535e-03
Military technologies and applied sciences 1.456515e-04
Library science 5.224456e-05
Precision production 2.533070e-05
Not classified by field of study 0.000000e+00

Generating a gt table

Format percentages

BA_degrees |>
  filter(year == 2015) |>
  select(field, perc) |>
  arrange(desc(perc)) |>
  gt() |>
  tab_style(
    style = "padding-top:0px;padding-bottom:0px;",
    locations = cells_body(columns = everything())
  ) |>
  tab_style(
    style = cell_text(size = "small"),
    locations = cells_body(columns = everything())
  ) |>
  fmt_percent(
    columns = perc,
    decimals = 1
  )
field perc
Business 19.2%
Health professions and related programs 11.4%
Social sciences and history 8.8%
Psychology 6.2%
Biological and biomedical sciences 5.8%
Engineering 5.2%
Visual and performing arts 5.1%
Education 4.8%
Communication, journalism, and related programs 4.8%
Homeland security, law enforcement, and firefighting 3.3%
Computer and information sciences 3.1%
Parks, recreation, leisure, and fitness studies 2.6%
Multi/interdisciplinary studies 2.5%
English language and literature/letters 2.4%
Liberal arts and sciences, general studies, and humanities 2.3%
Agriculture and natural resources 1.9%
Public administration and social services 1.8%
Physical sciences and science technologies 1.6%
Family and consumer sciences/human sciences 1.3%
Mathematics and statistics 1.2%
Foreign languages, literatures, and linguistics 1.0%
Engineering technologies 0.9%
Philosophy and religious studies 0.6%
Theology and religious vocations 0.5%
Architecture and related services 0.5%
Area, ethnic, cultural, gender, and group studies 0.4%
Communications technologies 0.3%
Transportation and materials moving 0.2%
Legal professions and studies 0.2%
Military technologies and applied sciences 0.0%
Library science 0.0%
Precision production 0.0%
Not classified by field of study 0.0%

Generating a gt table

Create human-readable column headers

BA_degrees |>
  filter(year == 2015) |>
  select(field, perc) |>
  arrange(desc(perc)) |>
  gt() |>
  tab_style(
    style = "padding-top:0px;padding-bottom:0px;",
    locations = cells_body(columns = everything())
  ) |>
  tab_style(
    style = cell_text(size = "small"),
    locations = cells_body(columns = everything())
  ) |>
  fmt_percent(
    columns = perc,
    decimals = 1
  ) |>
  cols_label(
    field = "Field",
    perc = "Percentage"
  )
Field Percentage
Business 19.2%
Health professions and related programs 11.4%
Social sciences and history 8.8%
Psychology 6.2%
Biological and biomedical sciences 5.8%
Engineering 5.2%
Visual and performing arts 5.1%
Education 4.8%
Communication, journalism, and related programs 4.8%
Homeland security, law enforcement, and firefighting 3.3%
Computer and information sciences 3.1%
Parks, recreation, leisure, and fitness studies 2.6%
Multi/interdisciplinary studies 2.5%
English language and literature/letters 2.4%
Liberal arts and sciences, general studies, and humanities 2.3%
Agriculture and natural resources 1.9%
Public administration and social services 1.8%
Physical sciences and science technologies 1.6%
Family and consumer sciences/human sciences 1.3%
Mathematics and statistics 1.2%
Foreign languages, literatures, and linguistics 1.0%
Engineering technologies 0.9%
Philosophy and religious studies 0.6%
Theology and religious vocations 0.5%
Architecture and related services 0.5%
Area, ethnic, cultural, gender, and group studies 0.4%
Communications technologies 0.3%
Transportation and materials moving 0.2%
Legal professions and studies 0.2%
Military technologies and applied sciences 0.0%
Library science 0.0%
Precision production 0.0%
Not classified by field of study 0.0%

Generating a gt table

Add a title

BA_degrees |>
  filter(year == 2015) |>
  select(field, perc) |>
  arrange(desc(perc)) |>
  gt() |>
  tab_style(
    style = "padding-top:0px;padding-bottom:0px;",
    locations = cells_body(columns = everything())
  ) |>
  tab_style(
    style = cell_text(size = "small"),
    locations = cells_body(columns = everything())
  ) |>
  fmt_percent(
    columns = perc,
    decimals = 1
  ) |>
  cols_label(
    field = "Field",
    perc = "Percentage"
  ) |>
  tab_header(
    title = "Bachelor's degrees awarded in 2015"
  )
Bachelor's degrees awarded in 2015
Field Percentage
Business 19.2%
Health professions and related programs 11.4%
Social sciences and history 8.8%
Psychology 6.2%
Biological and biomedical sciences 5.8%
Engineering 5.2%
Visual and performing arts 5.1%
Education 4.8%
Communication, journalism, and related programs 4.8%
Homeland security, law enforcement, and firefighting 3.3%
Computer and information sciences 3.1%
Parks, recreation, leisure, and fitness studies 2.6%
Multi/interdisciplinary studies 2.5%
English language and literature/letters 2.4%
Liberal arts and sciences, general studies, and humanities 2.3%
Agriculture and natural resources 1.9%
Public administration and social services 1.8%
Physical sciences and science technologies 1.6%
Family and consumer sciences/human sciences 1.3%
Mathematics and statistics 1.2%
Foreign languages, literatures, and linguistics 1.0%
Engineering technologies 0.9%
Philosophy and religious studies 0.6%
Theology and religious vocations 0.5%
Architecture and related services 0.5%
Area, ethnic, cultural, gender, and group studies 0.4%
Communications technologies 0.3%
Transportation and materials moving 0.2%
Legal professions and studies 0.2%
Military technologies and applied sciences 0.0%
Library science 0.0%
Precision production 0.0%
Not classified by field of study 0.0%

Plots in tables

Should these data be displayed in a table or a plot?

Popular Bachelor's degrees over the years
Field 1971 1976 1981 1986 1991 1996 2001 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
Business 14% 15% 21% 24% 23% 19% 21% 22% 21% 21% 21% 22% 22% 21% 20% 20% 19% 19%
Health professions 3% 6% 7% 7% 5% 7% 6% 6% 6% 7% 7% 8% 8% 8% 9% 10% 11% 11%
Social sciences and history 18% 14% 11% 9% 11% 11% 10% 11% 11% 11% 11% 11% 10% 10% 10% 10% 9% 9%
Other 65% 65% 61% 60% 60% 62% 62% 62% 62% 61% 61% 60% 60% 60% 60% 61% 61% 61%

Add visualizations to your table

Example: Add sparklines to display trend alongside raw data


Popular Bachelor's degrees over the years
Field Trend 1971 1976 1981 1986 1991 1996 2001 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
Business 14% 15% 21% 24% 23% 19% 21% 22% 21% 21% 21% 22% 22% 21% 20% 20% 19% 19%
Health professions 3% 6% 7% 7% 5% 7% 6% 6% 6% 7% 7% 8% 8% 8% 9% 10% 11% 11%
Social sciences and history 18% 14% 11% 9% 11% 11% 10% 11% 11% 11% 11% 11% 10% 10% 10% 10% 9% 9%
Other 65% 65% 61% 60% 60% 62% 62% 62% 62% 61% 61% 60% 60% 60% 60% 61% 61% 61%

Custom function with ggplot()

plot_spark <- function(df) {
  ggplot(df, aes(x = year, y = perc)) +
    geom_line(size = 20) +
    theme_void()
}

BA_degrees_other |>
  filter(field == "Social sciences and history") |>
  plot_spark()

Prep the data

BA_degrees_other_plots <- BA_degrees_other |>
  nest(field_df = c(year, perc)) |>
  mutate(plot = map(.x = field_df, .f = plot_spark))
BA_degrees_other_plots
# A tibble: 4 × 3
  field                       field_df          plot  
  <fct>                       <list>            <list>
1 Business                    <tibble [18 × 2]> <gg>  
2 Health professions          <tibble [18 × 2]> <gg>  
3 Social sciences and history <tibble [18 × 2]> <gg>  
4 Other                       <tibble [18 × 2]> <gg>  

Widen the table

BA_degrees_other |>
  pivot_wider(names_from = year, values_from = perc) |>
  mutate(ggplot = NA, .after = field)
# A tibble: 4 × 20
  field    ggplot `1971` `1976` `1981` `1986` `1991` `1996` `2001` `2005` `2006`
  <fct>    <lgl>   <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>  <dbl>
1 Business NA     0.137  0.155  0.214  0.240  0.228  0.195  0.212  0.216  0.214 
2 Health … NA     0.0300 0.0582 0.0681 0.0661 0.0547 0.0739 0.0610 0.0561 0.0619
3 Social … NA     0.185  0.137  0.107  0.0950 0.114  0.109  0.103  0.109  0.109 
4 Other    NA     0.648  0.651  0.610  0.599  0.603  0.623  0.624  0.618  0.615 
# ℹ 9 more variables: `2007` <dbl>, `2008` <dbl>, `2009` <dbl>, `2010` <dbl>,
#   `2011` <dbl>, `2012` <dbl>, `2013` <dbl>, `2014` <dbl>, `2015` <dbl>

Basic gt table

BA_degrees_other |>
  pivot_wider(names_from = year, values_from = perc) |>
  mutate(ggplot = NA, .after = field) |>
  gt()
field ggplot 1971 1976 1981 1986 1991 1996 2001 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
Business NA 0.13742036 0.15465473 0.21442886 0.23961783 0.22764399 0.19456092 0.2117997 0.2164815 0.21413480 0.21490238 0.21448445 0.21734496 0.21705247 0.21277490 0.20491161 0.19609363 0.19149908 0.19198505
Health professions NA 0.03003704 0.05820711 0.06808072 0.06611407 0.05470345 0.07390762 0.0610310 0.0560599 0.06192459 0.06680043 0.07131995 0.07519675 0.07856325 0.08360056 0.09132819 0.09843016 0.10628934 0.11410846
Social sciences and history NA 0.18496898 0.13653421 0.10748444 0.09499678 0.11430119 0.10858505 0.1029087 0.1090085 0.10872639 0.10772512 0.10707333 0.10523111 0.10472150 0.10324215 0.09961929 0.09659250 0.09257653 0.08810017
Other NA 0.64757362 0.65060395 0.61000599 0.59927133 0.60335137 0.62294641 0.6242607 0.6184501 0.61521422 0.61057207 0.60712227 0.60222718 0.59966277 0.60038239 0.60414092 0.60888370 0.60963506 0.60580632

Basic gt table with sparklines

BA_degrees_other |>
  pivot_wider(names_from = year, values_from = perc) |>
  mutate(ggplot = NA, .after = field) |>
  gt() |>
  text_transform(
    locations = cells_body(columns = ggplot),
    fn = function(x) {
      map(
        .x = BA_degrees_other_plots$plot, .f = ggplot_image,
        height = px(15), aspect_ratio = 4
      )
    }
  )
field ggplot 1971 1976 1981 1986 1991 1996 2001 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
Business 0.13742036 0.15465473 0.21442886 0.23961783 0.22764399 0.19456092 0.2117997 0.2164815 0.21413480 0.21490238 0.21448445 0.21734496 0.21705247 0.21277490 0.20491161 0.19609363 0.19149908 0.19198505
Health professions 0.03003704 0.05820711 0.06808072 0.06611407 0.05470345 0.07390762 0.0610310 0.0560599 0.06192459 0.06680043 0.07131995 0.07519675 0.07856325 0.08360056 0.09132819 0.09843016 0.10628934 0.11410846
Social sciences and history 0.18496898 0.13653421 0.10748444 0.09499678 0.11430119 0.10858505 0.1029087 0.1090085 0.10872639 0.10772512 0.10707333 0.10523111 0.10472150 0.10324215 0.09961929 0.09659250 0.09257653 0.08810017
Other 0.64757362 0.65060395 0.61000599 0.59927133 0.60335137 0.62294641 0.6242607 0.6184501 0.61521422 0.61057207 0.60712227 0.60222718 0.59966277 0.60038239 0.60414092 0.60888370 0.60963506 0.60580632

Final formatting

BA_degrees_other |>
  pivot_wider(names_from = year, values_from = perc) |>
  mutate(ggplot = NA, .after = field) |>
  gt() |>
  text_transform(
    locations = cells_body(columns = ggplot),
    fn = function(x) {
      map(
        .x = BA_degrees_other_plots$plot, .f = ggplot_image,
        height = px(15), aspect_ratio = 4
      )
    }
  ) |>
  cols_width(ggplot ~ px(100)) |>
  cols_align(align = "left", columns = field) |>
  fmt_percent(columns = where(is.numeric), decimals = 0) |>
  cols_label(field = "Field", ggplot = "Trend") |>
  tab_spanner(
    label = "Popular Bachelor's degrees over the years",
    columns = everything()
  ) |>
  tab_style(
    style = cell_text(weight = "bold"),
    locations = cells_column_spanners()
  )

Generate the table

Popular Bachelor's degrees over the years
Field Trend 1971 1976 1981 1986 1991 1996 2001 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
Business 14% 15% 21% 24% 23% 19% 21% 22% 21% 21% 21% 22% 22% 21% 20% 20% 19% 19%
Health professions 3% 6% 7% 7% 5% 7% 6% 6% 6% 7% 7% 8% 8% 8% 9% 10% 11% 11%
Social sciences and history 18% 14% 11% 9% 11% 11% 10% 11% 11% 11% 11% 11% 10% 10% 10% 10% 9% 9%
Other 65% 65% 61% 60% 60% 62% 62% 62% 62% 61% 61% 60% 60% 60% 60% 61% 61% 61%

Adding color text

Popular Bachelor's degrees over the years
Field Trend 1971 1976 1981 1986 1991 1996 2001 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
Business 14% 15% 21% 24% 23% 19% 21% 22% 21% 21% 21% 22% 22% 21% 20% 20% 19% 19%
Health professions 3% 6% 7% 7% 5% 7% 6% 6% 6% 7% 7% 8% 8% 8% 9% 10% 11% 11%
Social sciences and history 18% 14% 11% 9% 11% 11% 10% 11% 11% 11% 11% 11% 10% 10% 10% 10% 9% 9%
Other 65% 65% 61% 60% 60% 62% 62% 62% 62% 61% 61% 60% 60% 60% 60% 61% 61% 61%
plot_spark_color <- function(df) {
  ggplot(df, aes(x = year, y = perc, color = line_color)) +
    geom_line(size = 20) +
    theme_void() +
    scale_color_identity()
}

BA_degrees_other_plots_color <- BA_degrees_other |>
  mutate(line_color = case_when(
    field == "Business" ~ "#9D6C06",
    field == "Health professions" ~ "#077DAA",
    field == "Social sciences and history" ~ "#026D4E",
    field == "Other" ~ "#A39A09"
  )) |>
  nest(field_df = c(year, perc, line_color)) |>
  mutate(plot = map(field_df, plot_spark_color))

BA_degrees_other |>
  pivot_wider(names_from = year, values_from = perc) |>
  mutate(ggplot = NA, .after = field) |>
  gt() |>
  text_transform(
    locations = cells_body(columns = ggplot),
    fn = function(x) {
      map(BA_degrees_other_plots_color$plot, ggplot_image, height = px(15), aspect_ratio = 4)
    }
  ) |>
  cols_width(ggplot ~ px(100)) |>
  cols_align(align = "left", columns = field) |>
  fmt_percent(columns = where(is.numeric), decimals = 0) |>
  tab_style(style = cell_text(color = "#9D6C06"), locations = cells_body(rows = 1, columns = field)) |>
  tab_style(style = cell_text(color = "#077DAA"), locations = cells_body(rows = 2, columns = field)) |>
  tab_style(style = cell_text(color = "#026D4E"), locations = cells_body(rows = 3, columns = field)) |>
  tab_style(style = cell_text(color = "#A39A09"), locations = cells_body(rows = 4, columns = field)) |>
  cols_label(field = "Field", ggplot = "Trend") |>
  tab_spanner(label = "Popular Bachelor's degrees over the years", columns = everything()) |>
  tab_style(style = cell_text(weight = "bold"), locations = cells_column_spanners())

10 guidelines for better tables

10 guidelines for better tables

  1. Offset the heads from the body
  2. Use subtle dividers rather than heavy gridlines
  3. Right-align numbers and heads
  4. Left-align text and heads
  5. Select the appropriate level of precision
  6. Guide your reader with space between rows and columns
  7. Remove unit repetition
  8. Highlight outliers
  9. Group similar data and increase white space
  10. Add visualizations when appropriate

Table resources

Other packages

  • knitr::kable(): “Cheapest” pretty tables in Quarto
  • Interactivity: useful with Shiny applications!