In most cases research results, business and other reports are communicated through a combination of data visualization, tables, and narrative text.
In this topic we present examples on how to make beautiful tables using various R packages.
gt
Here is a brief example of how to use R gt library to create a table from the included sp500 dataset, out of six datasets included in gt: countrypops, sza, gtcars, sp500, pizzaplace, and exibble. These datasets are useful for experimenting how to create tables with gt's functions.
We can use gtsave() function with options to export tables into html, png, rtf, LaTeX, and docx.
The gt table is exported to an HTML file with inlined CSS, which is necessary for include the table as part of an HTML email, using the option inline_css = TRUE.
Saving as a PNG file results in a cropped image of an HTML table. The amount of whitespace can be set with the expand option.
Any use of the .tex, .ltx, or .rnw will result in the output of a LaTeX document.
Other table export functions include, as_latex(), as_raw_html(), as_rtf(), as_word(), extract_cells(), extract_summary().
Here is the code that produces a table using sp500 dataset:
- library(gt)
- library(tidyverse)
- library(glue)
- #Define the start and end dates for the data range
- start_date <- "2010-06-07"
- end_date <- "2010-06-14"
- # Create a gt table based on preprocessed 'sp500' table data
- tab_1 <- sp500 %>%
- filter(date >= start_date & date <= end_date) %>%
- select(-adj_close) %>%
- gt() %>%
- tab_header(
- title = "S&P 500",
- subtitle = glue("{start_date} to {end_date}")
- ) %>%
- fmt_date(
- columns = date,
- date_style = 3
- ) %>%
- fmt_currency(
- columns = c(open, high, low, close),
- currency = "USD"
- ) %>%
- fmt_number(
- columns = volume,
- suffixing = TRUE
- )
- tab_1 %>% gtsave(filename = "tab_1.html", inline_css = TRUE)
- tab_1 %>% gtsave("tab_1.rtf")
- tab_1 %>% gtsave("tab_1.tex")
- #These requires webshot2 package
- #tab_1 %>% gtsave("tab_1.pdf")
- #tab_1 %>% gtsave("tab_1.png", expand = 10)
See reference.
gtExtras
gtExtras package provides some additional helper functions to assist in creating beautiful tables with gt.
The package includes seven different themes, for examples gt_theme_538() styled after FiveThirtyEight style tables, the gt_theme_espn() styled after ESPN style tables, and the gt_theme_nytimes() styled after The New York Times tables.
- library(gt)
- library(gtExtras)
- library(tidyverse)
- library(glue)
- #Using gt_theme_538() theme
- tab_2 <- head(mtcars) %>%
- gt() %>%
- gt_theme_538()
- tab_2 %>% gtsave("tab_2.html")
- #gt_theme_espn()
- tab_2 <- head(mtcars) %>%
- gt() %>%
- gt_theme_espn()
- tab_2 %>% gtsave("tab_2.rtf")
- #gt_theme_nytimes()
- tab_3 <- head(mtcars) %>%
- gt() %>%
- gt_theme_nytimes() %>%
- tab_header(title = "Table styled like the NY Times")
- tab_3 %>% gtsave("tab_3.html")
- #Hulk data_color
- #Basic usage, where a specific column is passed.
- tab_4 <- head(mtcars) %>%
- gt::gt() %>%
- gt_hulk_col_numeric(mpg)
- tab_4 %>% gtsave("tab_4.html")
- #Trim provides a tighter range of purple/green so the colors are less pronounced.
- tab_5 <- head(mtcars) %>%
- gt::gt() %>%
- #Trim gives smaller range of colors so the green and purples are not as dark
- gt_hulk_col_numeric(mpg:carb, trim = TRUE)
- tab_5 %>% gtsave("tab_5.html")
- #Reverse makes higher values represented by purple and lower by green
- #The default is to have high = green, low = purple
- #Option to reverse the color palette so that purple is higher
- tab_6 <- head(mtcars) %>%
- gt::gt() %>%
- #reverse = green for low, purple for high
- gt_hulk_col_numeric(mpg:disp, reverse = FALSE)
- tab_6 %>% gtsave("tab_6.html")
gt_color_rows()
The gt_color_rows() function is a thin boilerplate wrapper around gt::data_color(). It’s simpler to use but still provides rich color choices thanks to the inclusion of paletteer::paletteer_d(). Here are the code snippets in action:
- library(gt)
- library(gtExtras)
- library(tidyverse)
- library(glue)
- #Basic usage
- tab_7 <- mtcars %>%
- head() %>%
- gt() %>%
- gt_color_rows(mpg:disp)
- tab_7 %>% gtsave("tab_7.html")
- #You can change the specific palette with palette = "package_name::palette_name"
- #Recognizes all of the dynamic palettes from paletteer
- tab_8 <- mtcars %>%
- head() %>%
- gt() %>%
- gt_color_rows(mpg:carb, palette = "ggsci::blue_material")
- tab_8 %>% gtsave("tab_8.html")
- #You can also use custom-defined palettes with named colors in R or hex color values
- tab_9 <- mtcars %>%
- head() %>%
- gt() %>%
- gt_color_rows(mpg:disp, palette = c("blue", "green"))
- #could also use palette = c("#ffffff", "##00FF00")
- tab_9 %>% gtsave("tab_9.html")
- #Lastly, you can also provide categorical or discrete data to be colored
- #Provide type = "discrete"
- tab_10 <- mtcars %>%
- head() %>%
- gt() %>%
- gt_color_rows(
- cyl,
- palette = "ggthemes::colorblind",
- #Note that you can manually define range like c(4, 6, 8)
- domain = range(mtcars$cyl),
- pal_type = "discrete"
- )
- tab_10 %>% gtsave("tab_10.html")
See reference.