Tables

Lecture 22

Dr. Mine Çetinkaya-Rundel

Duke University
STA 313 - Spring 2024

Warm up

Setup

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

# set theme for ggplot2
ggplot2::theme_set(ggplot2::theme_minimal(base_size = 14))

# set width of code output
options(width = 65)

# set figure parameters for knitr
knitr::opts_chunk$set(
  fig.width = 7, # 7" width
  fig.asp = 0.618, # the golden ratio
  fig.retina = 3, # dpi multiplier for displaying HTML output on retina
  fig.align = "center", # center align figures
  dpi = 300 # higher dpi, sharper image
)

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("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



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        

Degrees awarded in 2015

# 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        

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%

In the next few slides…


Popular Bachelor’s degrees over the years


How should this information be displayed? And why?

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

ae-17: Task 1

Recreate this table of 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%

Why not both?

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%

ae-17: Task 2

Recreate this table of popular Bachelor’s degrees awarded over time.


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%

ae-17: Task 3

Your turn: Add color to the previous 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%

Making 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 R Markdown
  • Other (than HTML) outputs:
  • gtsummary: For summarizing statistical output with gt
  • Interactivity: DT, reactable

Table inspiration

In Python

$ pip install great_tables