Tables

Lecture 15

Dr. Mine Çetinkaya-Rundel

Duke University
STA 313 - Spring 2026

Warm up

Announcements

  • HW 3 due tomorrow Thursday at 5 pm

Setup

# load packages
library(tidyverse)
library(tidymodels)
library(openintro)
library(scales)
library(ggrepel)
library(colorspace)
library(ggthemes)
library(paletteer)
library(gt)

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

# 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
)

From last time

Color scales in the colorspace package

colorspace package offers some order

Scale name: scale_<aesthetic>_<datatype>_<colorscale>()

  • <aesthetic>: name of the aesthetic (fill, color, colour)
  • <datatype>: type of variable plotted (discrete, continuous, binned)
  • <colorscale>: type of the color scale (qualitative, sequential, diverging, divergingx)
Scale function Aesthetic Data type Palette type
scale_color_discrete_qualitative() color discrete qualitative
scale_fill_continuous_sequential() fill continuous sequential
scale_colour_continous_divergingx() colour continuous diverging

scale_fill_continuous_sequential() + Multi-hue

p_temp_months +
  scale_fill_continuous_sequential(
    palette = "YlGnBu",
    rev = FALSE
  )

scale_fill_continuous_sequential() + Viridis

p_temp_months +
  scale_fill_continuous_sequential(
    palette = "Viridis",
    rev = FALSE
  )

scale_fill_continuous_sequential() + Inferno

p_temp_months +
  scale_fill_continuous_sequential(
    palette = "Inferno",
    begin = 0.15,
    rev = FALSE
  )

HCL palettes: Sequential

HCL: Hue-Chroma-Luminance

colorspace::hcl_palettes(type = "sequential", plot = TRUE)

HCL palettes: Diverging

colorspace::hcl_palettes(type = "diverging", plot = TRUE, n = 9)

HCL palettes: Divergingx

colorspace::divergingx_palettes(plot = TRUE, n = 9)

Setting colors manually

Default discrete scale

No color scale defined, default is scale_color_hue():

p_popgrowth <- ggplot(
  popgrowth,
  aes(
    x = pop2010,
    y = popgrowth,
    color = region
  )
) +
  geom_point(size = 3) +
  scale_x_log10()

p_popgrowth

scale_color_hue()

p_popgrowth +
  scale_color_hue()

scale_color_colorblind()

Uses Okabe-Ito colors:

p_popgrowth +
  scale_color_colorblind()

scale_color_manual()

Qualitative scales are best set manually:

p_popgrowth +
  scale_color_manual(
    values = c(
      West = "#E69F00",
      South = "#56B4E9",
      Midwest = "#009E73",
      Northeast = "#CC79A7"
    )
  )

Okabe-Ito RGB codes

Name Hex code R, G, B (0-255)
orange #E69F00 230, 159, 0
sky blue #56B4E9 86, 180, 233
bluish green #009E73 0, 158, 115
yellow #F0E442 240, 228, 66
blue #0072B2 0, 114, 178
vermilion #D55E00 213, 94, 0
reddish purple #CC79A7 204, 121, 167
black #000000 0, 0, 0

Other color scales

palateeer package

paletteer is a comprehensive collection of color palettes in R using a common interface:

emilhvitfeldt.github.io/paletteer

palateeer + nord::aurora

https://github.com/jkaupp/nord

p_popgrowth +
  scale_color_paletteer_d("nord::aurora")

palateeer + LaCroixColoR::PassionFruit

https://github.com/johannesbjork/LaCroixColoR

p_popgrowth +
  scale_color_paletteer_d("LaCroixColoR::PassionFruit")

palateeer + tayloRswift::taylor1989

https://asteves.github.io/tayloRswift/

p_popgrowth +
  scale_color_paletteer_d("tayloRswift::taylor1989")

palateeer + scico::lajolla

https://github.com/thomasp85/scico

p_temp_months +
  scale_fill_paletteer_c("scico::batlow")

Good practices for using colors

1. Don’t use too many colors

  • Qualitative color scales work best with 3 to 5 categories

  • Once you reach 8-10 or more categories, matching colors to categories becomes too burdensome for viewers

Tip

Use direct labeling instead of relying on color legends when you have many categories.

2. Avoid gratuitous coloring

Only use color when it serves a clear communicative purpose:

Problems with unnecessary color:

  • Overly saturated colors make figures hard to examine
  • “Rainbow effects” provide no analytical benefit
  • Visual clutter distracts from the data

When to use color:

  • Distinguish meaningful categories
  • Encode quantitative values
  • Highlight specific elements
  • Support your narrative

3. Don’t use rainbow color scales

The rainbow (or “jet”) color scale is non-monotonic – it violates sequential design principles:

  • Colors change at inconsistent rates across the scale
  • Similar colors appear at both ends (red wraps to red)
  • Creates artificial “bands” that emphasize arbitrary thresholds
  • Perceptually non-uniform (yellow appears brighter than other colors)

4. Keep diverging scales balanced

Diverging scales should progress symmetrically from the neutral center to both extremes, ensuring that equal visual weight is given to positive and negative deviations:

  • Light colors at the center (neutral point)
  • Dark colors at both ends
  • Same rate of change in both directions

5. Avoid high chroma

High chroma: Toys

Low chroma: “Elegance”

6. Be aware of color-vision deficiency

Roughly 5%–8% of men are color blind and 0.5% of women are color blind, with red-green color-vision deficiency being the most common type:

  • Red-green color-vision deficiency is the most common:

  • Blue-green color-vision deficiency is rare but does occur:

  • Choose colors that can be distinguished with CVD:

CVD + size

  • CVD is worse for thin lines and tiny dots

  • When in doubt, run CVD simulations

Further reading

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/15", "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-10: 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-10: 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-10: 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

In Python

$ pip install great_tables