Tables

Lecture 22

Dr. Mine Çetinkaya-Rundel

Duke University
STA 313 - Spring 2023

Warm up

Announcements

  • HW grading

Setup

# load packages
library(countdown)
library(tidyverse)
library(sf)
library(stars)
library(tmap)
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
)

Using tmap

From last time

elev <- read_stars("data/easter_island/ei_elev.tif")
border <- read_sf("data/easter_island/ei_border.gpkg")
roads <- read_sf("data/easter_island/ei_roads.gpkg")
points <- read_sf("data/easter_island/ei_points.gpkg")

Thematic maps with tmap

tmap is an actively maintained open-source R-library for drawing thematic maps. The API is based on A Layered Grammar of Graphics and resembles the syntax of ggplot2, a popular R-library for drawing charts.

Border

tm_shape(border) +
  tm_borders()

Border + more

tm_shape(border) +
  tm_borders() +
  tm_layout(
    main.title = "Easter Island",
    bg.color = "lightblue"
  )

Roads

tm_shape(border) +
  tm_borders() +
  tm_shape(roads) +
  tm_lines(
    lwd = "strokelwd",
    legend.lwd.show = FALSE
  ) +
  tm_layout(
    main.title = "Easter Island",
    bg.color = "lightblue"
  )

Roads + more

tm_shape(border) +
  tm_borders() +
  tm_shape(roads) +
  tm_lines(
    lwd = "strokelwd",
    legend.lwd.show = FALSE,
    col = "type"
  ) +
  tm_layout(
    main.title = "Easter Island",
    bg.color = "lightblue"
  )

Elevation

tm_shape(elev) +
  tm_raster() +
  tm_shape(border) +
  tm_borders() +
  tm_shape(roads) +
  tm_lines(
    lwd = "strokelwd",
    legend.lwd.show = FALSE
  ) +
  tm_layout(
    main.title = "Easter Island",
    bg.color = "lightblue"
  )

Elevation + more

tm_shape(elev) +
  tm_raster(
    style = "cont",
    title = "Elevation (m)"
  ) +
  tm_shape(border) +
  tm_borders() +
  tm_shape(roads) +
  tm_lines(
    lwd = "strokelwd",
    legend.lwd.show = FALSE
  ) +
  tm_layout(
    main.title = "Easter Island",
    bg.color = "lightblue"
  )

Elevation + even more

tm_shape(elev) +
  tm_raster(
    style = "cont",
    title = "Elevation (m)",
    palette = "-RdYlGn"
  ) +
  tm_shape(border) +
  tm_borders() +
  tm_shape(roads) +
  tm_lines(
    lwd = "strokelwd",
    legend.lwd.show = FALSE
  ) +
  tm_layout(
    main.title = "Easter Island",
    bg.color = "lightblue"
  )

Volcanoes

tm_shape(elev) +
  tm_raster(
    style = "cont",
    title = "Elevation (m)",
    palette = "-RdYlGn"
  ) +
  tm_shape(border) +
  tm_borders() +
  tm_shape(roads) +
  tm_lines(
    lwd = "strokelwd",
    legend.lwd.show = FALSE
  ) +
  tm_shape(
    points |> filter(type == "volcano")
  ) +
  tm_symbols(
    shape = 24,
    size = "elevation",
    title.size = "Volcanoes (m)"
  ) +
  tm_layout(
    main.title = "Easter Island",
    bg.color = "lightblue"
  )

Other stuff

tm_graticules(alpha = 0.25) +
  tm_shape(elev) +
  tm_raster(
    style = "cont",
    title = "Elevation (m)",
    palette = "-RdYlGn"
  ) +
  tm_shape(border) +
  tm_borders() +
  tm_shape(roads) +
  tm_lines(
    lwd = "strokelwd",
    legend.lwd.show = FALSE
  ) +
  tm_shape(
    points |> filter(type == "volcano")
  ) +
  tm_symbols(
    shape = 24,
    size = "elevation",
    title.size = "Volcanoes (m)"
  ) +
  tm_layout(
    main.title = "Easter Island",
    bg.color = "lightblue"
  ) +
  tm_compass(position = c("right", "top")) +
  tm_scale_bar() +
  tm_credits("Author, 2021") +
  tm_add_legend(type = "line", col = "black", title = "Roads")

Other stuff

tmap modes

map <- tm_graticules(alpha = 0.25) +
  tm_shape(elev) +
  tm_raster(
    style = "cont",
    title = "Elevation (m)",
    palette = "-RdYlGn"
  ) +
  tm_shape(border) +
  tm_borders() +
  tm_shape(roads) +
  tm_lines(
    lwd = "strokelwd",
    legend.lwd.show = FALSE
  ) +
  tm_shape(
    points |> filter(type == "volcano")
  ) +
  tm_symbols(
    shape = 24,
    size = "elevation",
    title.size = "Volcanoes (m)"
  ) +
  tm_layout(
    main.title = "Easter Island",
    bg.color = "lightblue"
  ) +
  tm_compass(position = c("right", "top")) +
  tm_scale_bar() +
  tm_credits("Author, 2021") +
  tm_add_legend(type = "line", col = "black", title = "Roads")

plot

tmap_mode("plot")
map

view

tmap_mode("view")
map

Tiles

tm_basemap(c(osm = "OpenStreetMap", esri = "Esri.WorldStreetMap")) +
  tm_shape(border, is.master = TRUE) +
  tm_borders() +
  tm_shape(roads) +
  tm_lines(
    lwd = "strokelwd",
    legend.lwd.show = FALSE
  ) +
  tm_shape(
    points |> filter(type == "volcano")
  ) +
  tm_symbols(
    size = "elevation",
    legend.size.show = FALSE
  )

Tiles

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 1.51e-2
 2 Architecture and related services          1971   5570 6.63e-3
 3 Area, ethnic, cultural, gender, and grou…  1971   2579 3.07e-3
 4 Biological and biomedical sciences         1971  35705 4.25e-2
 5 Business                                   1971 115396 1.37e-1
 6 Communication, journalism, and related p…  1971  10324 1.23e-2
 7 Communications technologies                1971    478 5.69e-4
 8 Computer and information sciences          1971   2388 2.84e-3
 9 Education                                  1971 176307 2.10e-1
10 Engineering                                1971  45034 5.36e-2
# ℹ 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                      1.91e-2
 2 Architecture and related services                      4.80e-3
 3 Area, ethnic, cultural, gender, and group studies      4.11e-3
 4 Biological and biomedical sciences                     5.80e-2
 5 Business                                               1.92e-1
 6 Communication, journalism, and related programs        4.78e-2
 7 Communications technologies                            2.71e-3
 8 Computer and information sciences                      3.14e-2
 9 Education                                              4.84e-2
10 Engineering                                            5.16e-2
11 Engineering technologies                               9.10e-3
12 English language and literature/letters                2.42e-2
13 Family and consumer sciences/human sciences            1.30e-2
14 Foreign languages, literatures, and linguistics        1.03e-2
15 Health professions and related programs                1.14e-1
16 Homeland security, law enforcement, and firefighting   3.31e-2
17 Legal professions and studies                          2.33e-3
18 Liberal arts and sciences, general studies, and human… 2.30e-2
19 Library science                                        5.22e-5
20 Mathematics and statistics                             1.15e-2
21 Military technologies and applied sciences             1.46e-4
22 Multi/interdisciplinary studies                        2.51e-2
23 Parks, recreation, leisure, and fitness studies        2.59e-2
24 Philosophy and religious studies                       5.84e-3
25 Physical sciences and science technologies             1.59e-2
26 Precision production                                   2.53e-5
27 Psychology                                             6.20e-2
28 Public administration and social services              1.81e-2
29 Social sciences and history                            8.81e-2
30 Theology and religious vocations                       5.12e-3
31 Transportation and materials moving                    2.49e-3
32 Visual and performing arts                             5.06e-2
33 Not classified by field of study                       0      

# A tibble: 33 × 2
   field                                                     perc
   <chr>                                                    <dbl>
 1 Business                                               1.92e-1
 2 Health professions and related programs                1.14e-1
 3 Social sciences and history                            8.81e-2
 4 Psychology                                             6.20e-2
 5 Biological and biomedical sciences                     5.80e-2
 6 Engineering                                            5.16e-2
 7 Visual and performing arts                             5.06e-2
 8 Education                                              4.84e-2
 9 Communication, journalism, and related programs        4.78e-2
10 Homeland security, law enforcement, and firefighting   3.31e-2
11 Computer and information sciences                      3.14e-2
12 Parks, recreation, leisure, and fitness studies        2.59e-2
13 Multi/interdisciplinary studies                        2.51e-2
14 English language and literature/letters                2.42e-2
15 Liberal arts and sciences, general studies, and human… 2.30e-2
16 Agriculture and natural resources                      1.91e-2
17 Public administration and social services              1.81e-2
18 Physical sciences and science technologies             1.59e-2
19 Family and consumer sciences/human sciences            1.30e-2
20 Mathematics and statistics                             1.15e-2
21 Foreign languages, literatures, and linguistics        1.03e-2
22 Engineering technologies                               9.10e-3
23 Philosophy and religious studies                       5.84e-3
24 Theology and religious vocations                       5.12e-3
25 Architecture and related services                      4.80e-3
26 Area, ethnic, cultural, gender, and group studies      4.11e-3
27 Communications technologies                            2.71e-3
28 Transportation and materials moving                    2.49e-3
29 Legal professions and studies                          2.33e-3
30 Military technologies and applied sciences             1.46e-4
31 Library science                                        5.22e-5
32 Precision production                                   2.53e-5
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…


Popular Bachelor’s degrees over the years


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

Livecoding: Recreate this table of Bachelor’s degrees awarded in 2015.

  • Go to ae-XX repo and clone it
  • Install the gt package: install.packages("gt")
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%

Livecoding: 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%

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%
10:00

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: We will work with these when we learn Shiny! - DT - reactable

Table inspiration