Making Tables in R

Post Reply
User avatar
Eli
Senior Expert Member
Reactions: 183
Posts: 5334
Joined: 9 years ago
Location: Tanzania
Has thanked: 75 times
Been thanked: 88 times
Contact:

#1

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:

  1. library(gt)
  2. library(tidyverse)
  3. library(glue)
  4.  
  5. #Define the start and end dates for the data range
  6. start_date <- "2010-06-07"
  7. end_date <- "2010-06-14"
  8.  
  9. # Create a gt table based on preprocessed 'sp500' table data
  10. tab_1 <- sp500 %>%
  11.   filter(date >= start_date & date <= end_date) %>%
  12.   select(-adj_close) %>%
  13.   gt() %>%
  14.   tab_header(
  15.     title = "S&P 500",
  16.     subtitle = glue("{start_date} to {end_date}")
  17.   ) %>%
  18.   fmt_date(
  19.     columns = date,
  20.     date_style = 3
  21.   ) %>%
  22.   fmt_currency(
  23.     columns = c(open, high, low, close),
  24.     currency = "USD"
  25.   ) %>%
  26.   fmt_number(
  27.     columns = volume,
  28.     suffixing = TRUE
  29.   )
  30.  
  31. tab_1 %>% gtsave(filename = "tab_1.html", inline_css = TRUE)
  32. tab_1 %>% gtsave("tab_1.rtf")
  33. tab_1 %>% gtsave("tab_1.tex")
  34. #These requires  webshot2 package
  35. #tab_1 %>% gtsave("tab_1.pdf")
  36. #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.

  1. library(gt)
  2. library(gtExtras)
  3. library(tidyverse)
  4. library(glue)
  5.  
  6. #Using gt_theme_538() theme
  7. tab_2 <- head(mtcars) %>%
  8.   gt() %>%
  9.   gt_theme_538()
  10. tab_2 %>% gtsave("tab_2.html")
  11.  
  12. #gt_theme_espn()
  13. tab_2 <- head(mtcars) %>%
  14.   gt() %>%
  15.   gt_theme_espn()
  16. tab_2 %>% gtsave("tab_2.rtf")
  17.  
  18. #gt_theme_nytimes()
  19. tab_3 <- head(mtcars) %>%
  20.   gt() %>%
  21.   gt_theme_nytimes() %>%
  22.   tab_header(title = "Table styled like the NY Times")
  23. tab_3 %>% gtsave("tab_3.html")
  24.  
  25. #Hulk data_color
  26. #Basic usage, where a specific column is passed.
  27. tab_4 <- head(mtcars) %>%
  28.   gt::gt() %>%
  29.   gt_hulk_col_numeric(mpg)
  30. tab_4 %>% gtsave("tab_4.html")
  31.  
  32. #Trim provides a tighter range of purple/green so the colors are less pronounced.
  33. tab_5 <- head(mtcars) %>%
  34.   gt::gt() %>%
  35.   #Trim gives smaller range of colors so the green and purples are not as dark
  36.   gt_hulk_col_numeric(mpg:carb, trim = TRUE)
  37. tab_5 %>% gtsave("tab_5.html")
  38.  
  39. #Reverse makes higher values represented by purple and lower by green
  40. #The default is to have high = green, low = purple
  41. #Option to reverse the color palette so that purple is higher
  42. tab_6 <- head(mtcars) %>%
  43.   gt::gt() %>%
  44.   #reverse = green for low, purple for high
  45.   gt_hulk_col_numeric(mpg:disp, reverse = FALSE)
  46. 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:

  1. library(gt)
  2. library(gtExtras)
  3. library(tidyverse)
  4. library(glue)
  5.  
  6. #Basic usage
  7. tab_7 <- mtcars %>%
  8.   head() %>%
  9.   gt() %>%
  10.   gt_color_rows(mpg:disp)
  11. tab_7 %>% gtsave("tab_7.html")
  12.  
  13. #You can change the specific palette with palette = "package_name::palette_name"
  14. #Recognizes all of the dynamic palettes from paletteer
  15. tab_8 <- mtcars %>%
  16.   head() %>%
  17.   gt() %>%
  18.   gt_color_rows(mpg:carb, palette = "ggsci::blue_material")
  19. tab_8 %>% gtsave("tab_8.html")
  20.  
  21. #You can also use custom-defined palettes with named colors in R or hex color values
  22. tab_9 <- mtcars %>%
  23.   head() %>%
  24.   gt() %>%
  25.   gt_color_rows(mpg:disp, palette = c("blue", "green"))
  26.     #could also use palette = c("#ffffff", "##00FF00")
  27. tab_9 %>% gtsave("tab_9.html")
  28.  
  29. #Lastly, you can also provide categorical or discrete data to be colored
  30. #Provide type = "discrete"
  31. tab_10 <- mtcars %>%
  32.   head() %>%
  33.   gt() %>%
  34.   gt_color_rows(
  35.     cyl,
  36.     palette = "ggthemes::colorblind",
  37.     #Note that you can manually define range like c(4, 6, 8)
  38.     domain = range(mtcars$cyl),
  39.     pal_type = "discrete"
  40.    )
  41. tab_10 %>% gtsave("tab_10.html")

See reference.

0
TSSFL -- A Creative Journey Towards Infinite Possibilities!
Post Reply

Return to “R”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 1 guest