Automated Summaries: Generating instant visual overviews of any dataset.
Inline Graphics: Inserting distribution plots (sparklines) and bar charts directly into table cells.
Dynamic Styling: Using heatmaps and row highlighting to draw attention to key data.
Professional Theming: Applying styles from famous publications like The Guardian, ESPN, and the NY Times.
1. Environment Setup and Instant Summaries
The gtExtras package extends the gt (grammar of tables) package to provide high-level functions for data visualization within tables.
Code
# Load required librarieslibrary(svglite)library(gtExtras)library(tidyverse)library(RColorBrewer)library(gt)# Create an instant visual summary of the Iris datasetiris %>%gt_plt_summary(title ="Iris Dataset Summary")
Iris Dataset Summary
150 rows x 5 cols
Column
Plot Overview
Missing
Mean
Median
SD
Sepal.Length
0.0%
5.8
5.8
0.8
Sepal.Width
0.0%
3.1
3.0
0.4
Petal.Length
0.0%
3.8
4.3
1.8
Petal.Width
0.0%
1.2
1.3
0.8
Species
setosa, versicolor and virginica
0.0%
—
—
—
2. Inserting Graphics into Tables
Instead of looking at raw numbers, we can embed the distribution of data directly into the table using list-columns. This creates “sparklines” that provide visual context for numerical summaries.
Code
# Prepare data with a list-column for distributionmtcars_summary <- mtcars %>%group_by(cyl) %>%summarize(Median =round(median(mpg), 1),Mean =round(mean(mpg), 1),Distribution =list(mpg))# Visualize with sparklines (gt_plt_dist) and a professional thememtcars_summary %>%gt() %>%gt_plt_dist(Distribution) %>%gt_theme_guardian() %>%tab_header(title ="Miles Per Gallon Statistics",subtitle ="Comparing performance by cylinder count")
Miles Per Gallon Statistics
Comparing performance by cylinder count
cyl
Median
Mean
Distribution
4
26.0
26.7
6
19.7
19.7
8
15.2
15.1
3. Advanced Country Analysis (Gapminder)
We can create highly complex tables that combine text, numbers, heatmaps, and bar charts to communicate multifaceted stories.
Code
library(gapminder)# Step 1: Data Preparation and Base Tableplot_data <- gapminder %>%rename(Country = country) %>%filter(continent =="Asia") %>%group_by(Country) %>%summarise("GDP per capita"=round(mean(gdpPercap)),"Population size"=round(mean(pop)),"Life expectancy"=list(lifeExp)) %>%arrange(desc(`GDP per capita`)) %>%head(10) %>%gt() %>%gt_plt_dist("Life expectancy") %>%tab_header(title ="The GDP and Population Size of Asia") %>%cols_align(align ="left")# Display with ESPN themeplot_data %>%gt_theme_espn()
The GDP and Population Size of Asia
Country
GDP per capita
Population size
Life expectancy
Kuwait
65333
1206496
Saudi Arabia
20262
12478368
Bahrain
18078
373913
Japan
17751
111758808
Singapore
17425
2667817
Hong Kong, China
16229
4792259
Israel
14161
3845611
Oman
12139
1438205
Taiwan
10225
16874724
Korea, Rep.
8217
36499386
Adding Percentage Bars and Heatmap Colors
Using gt_plt_bar_pct, we can represent numerical values as horizontal bar charts within the cells, making comparisons much more intuitive.
Code
# Step 2: Adding visual intensity with bars and color scalesenhanced_plot <- plot_data %>%gt_theme_espn() %>%gt_color_rows(column ="Population size", palette ="Pastel1") %>%gt_plt_bar_pct("GDP per capita",fill ="#d580ff",height =15,width =120)enhanced_plot
The GDP and Population Size of Asia
Country
GDP per capita
Population size
Life expectancy
Kuwait
1206496
Saudi Arabia
12478368
Bahrain
373913
Japan
111758808
Singapore
2667817
Hong Kong, China
4792259
Israel
3845611
Oman
1438205
Taiwan
16874724
Korea, Rep.
36499386
4. Highlighting and Professional Themes
To make specific findings stand out, we can highlight rows based on logical conditions—perfect for focusing on specific countries or outliers.
Row Highlighting
Code
# Highlighting specific countries (e.g., Bangladesh and China)asian_table <- enhanced_plot %>%gt_highlight_rows(rows = Country %in%c("Bangladesh", "China"),fill ="#f2e6ff")asian_table
The GDP and Population Size of Asia
Country
GDP per capita
Population size
Life expectancy
Kuwait
1206496
Saudi Arabia
12478368
Bahrain
373913
Japan
111758808
Singapore
2667817
Hong Kong, China
4792259
Israel
3845611
Oman
1438205
Taiwan
16874724
Korea, Rep.
36499386
5. Exploring Professional Themes
gtExtras provides built-in functions to mimic the typography, padding, and color schemes of world-class data journalism outlets. Use the tabs below to compare the styles.