Table | the R Graph Gallery (2024)


Tables are an indispensable tool in data visualization. They offer a straightforward way to present complex data in an organized, easy-to-read format. R excels in table creation and manipulation, offering a range of packages like kableExtra, gt, reactable, and DT that provide extensive functionalities to suit various needs.
This section is rich in examples for each table-creation strategy, guiding you on which approach is best suited for different types of data and visualization goals.

How to make a table with R

Tables are powerful but complicated figures. As a result, there is a myriad of packages allowing to build them with R. It is easy to get lost!

I've tried to summarize the main packages in the following diagram:

Table | the R Graph Gallery (1)

The following document is organized following those 4 big families. For each package, I tried to explain when to use it and provided a few example with code to get started quickly.

1️⃣ GT, gtsummary and gtExtras

GT: Customizable, user-friendly R table

The GT package, stands for "Grammar of Tables". It was created by the RStudio team and first released in 2018. It offers an intuitive, tidyverse-inspired syntax, making table creation accessible, including for beginners.

GT's user-friendly design for handling complex formatting has quickly gained popularity in the R community. Its ease of use and readability make it a go-to choice for many R users seeking to create clear and aesthetically pleasing tables.

Introduction to gt A set of simple examples showing how to use the GT library.
Sub-headers How to create sub-headers with the gt library
Custom title A set of examples showing how to customize the titles of a table made with GT
Custom footer How to customize the footer and the references section of a gt table

gtsummary: summary statistics tables

gtSummary is a companion package to gt, specifically designed to enhance gt's capabilities in summarizing statistical findings. It bridges the gap between data analysis and table creation, allowing users to seamlessly generate summary tables directly from their analytical outputs.

By integrating with gt, gtSummary simplifies the process of transforming statistical results into well-organized and clear tables, ideal for reports or presentations.

Display regression results How to display results of your regression models in a beautiful table
Summary table Create summary table with both descriptive and inferential statistics, and even add your own custom functions!

gtExtras: more styling for gt

gtExtras augments and expands the functionalities of the gt package. It allows to create even more sophisticated and visually appealing tables.

It comes with a set of themes to make your table good-looking with just one more line of code. It provides functions to easily add plots in table cells. It also has helper functions to help with colors and icons.

Line chart Learn how to add a line chart in every cell of a table with gtExtras
Distribution With gtExtras, you can add a distribution plot in every cell, with multiple type of charts
Percentage bar How to add a percentage bar in a table cell with gtExtras
Summary table with charts Create summary table with descriptive statistics and charts with just one line of code!
Excel theme You can use a theme to make your table look like an Excel table
Dot matrix theme You can use a theme to make your table look like a dot matrix table
Dark theme You can use a theme to make your table look like a dark theme

2️⃣ kable and kableExtra for R Markdown documents

kable(): the table function of knitr

The knitr package is mainly made to render R Markdown documents. But it also has a kable() function that offers a simple and efficient way to create tables in R markdown documents.

kable() allows for quick conversion of data frames into tables.

kableExtra(): extending kable()

On the other hand, kableExtra serves as an extension to kable, significantly enhancing its capabilities. With kableExtra, users gain access to a wide range of styling and formatting options, such as adding stripes to rows, formatting text and cells, and even creating complex table layouts.

Customize colors in kableExtra tables Change background color, cell color or use a gradient of colors
Add links and images How to add links and images in a kableExtra table

3️⃣ Interactive tables with DT, reactable or formattable.

DT: easy filtering & sorting

DT stands for "DataTables", the Javascript library it interacts with. DT stands out for its ability to handle large datasets efficiently and its rich array of features like searching, sorting, and pagination.

I love adding a DT table at the beginning of my data analysis Quarto report. It provides access to your raw data easily!

Please check my full introduction to DT for more! Oh and this is how a DT table looks like:

See code

And a couple of additional examples to learn how to customize DT tables:

HTML content in table cells Add images, links, or any HTML content in a DT table
Filter, edit and add caption How to add a data-dependent filter, edit cell values and add caption to a DT table


reactable: unlimited cell customization

reactable allows to create interactive tables with extensive cell formatting capabilities.

It simplifies the process of embedding images within cells, it allows to create heat map-like tables through its advanced cell coloring features. Additionally, it offers unique functionalities for integrating bars or bubbles, further enhancing the visual appeal and informative value of the tables.

Reactable is also exceptionally effective for designing tables with expandable rows, making it an ideal choice for aggregating and presenting complex data sets in a user-friendly manner.

Column formatting How to format columns with date, currency or any numeric value in a reactable table
Clickable, interactive, and custom table How to create a fully customized table charts and clickable elements

Reactable can create some pretty amazing tables. Check this work by szymanskir for the R Studio table contest! Note: you can scroll, images are links, and the line chart is interactive.

See code

formattable

formattable is another great alternative when it comes to build interactive tables with R. Check its github repository for examples.

4️⃣ Other useful libraries.

flextable: best option for non-html output

flextable is another solid option to create very polish static tables. It supports a wide range of formatting options, including merging cells, rotating text, and conditional formatting.

It stands out due to its compatibility with various R Markdown formats, including Word, PowerPoint, and HTML.

rhandsontable: to manually edit cells

Rhandsontable provides an interactive table interface, allowing for direct editing of tables within a Shiny app or R Markdown document. It differentiates itself with features like dropdown menus, checkboxes, and calendar aids for data entry, emphasizing interactivity and user input.

It is best suited for applications requiring interactive data editing and manipulation within a web interface, such as Shiny applications.

modelsummary: for statistical model results

modelsummary is specialized for summarizing statistical models in R, offering a straightforward way to create elegant and comprehensive tables of model results.

It supports over one hundred types of models out-of-the-box, and allows users to report the results of those models side-by-side in a table, or in coefficient plots.

huxtable: for LaTeX output

huxtable focuses on creating simple yet elegant tables in R, with a strong emphasis on cross-format compatibility for LaTeX, HTML, and Word.

It is particularly appreciated by people in need for LaTeX outputs.

Related chart types

Barplot

Spider / Radar

Wordcloud

Parallel

Lollipop

Circular Barplot

Table | the R Graph Gallery (2024)

FAQs

What is a R graph gallery? ›

Welcome the R graph gallery, a collection of charts made with the R programming language. Hundreds of charts are displayed in several sections, always with their reproducible code available. The gallery makes a focus on the tidyverse and ggplot2.

What are the different types of plots in R? ›

With R, users can create simple charts such as pie, bar, and line graphs to more sophisticated plots like scatter plots, box plots, heat maps, and histograms.

What is the purpose of the ggplot library in R? ›

ggplot is used for creating a wide range of high-quality and customizable statistical graphs, such as scatter plots, bar charts, line plots, histograms, and more, to effectively explore and present data.

What is R used for in data visualization? ›

R offers a broad collection of visualization libraries along with extensive online guidance on their usage. R also offers data visualization in the form of 3D models and multipanel charts. Through R, we can easily customize our data visualization by changing axes, fonts, legends, annotations, and labels.

How to create a graph in R programming? ›

Graphing in R is like painting and uses a canvas approach; you start out with an empty plot (called a device). You'll add your data points, axis titles, graph title, color customizations, and other functions individually. Each time a graphics function is used, R 'paints' the new customizations onto your plot device.

How do you add a table to a graph? ›

Right-click inside graph layer (or right-click on a blank place in the Layout window) and select New Table... from the context menu. In the add_table_to_graph dialog that opens, specify the number of columns and rows in the table, the table title, etc. Click the OK button to add this new table to window.

How to write a table in R? ›

6.2 Creating Basic Tables: table() and xtabs() A contingency table is a tabulation of counts and/or percentages for one or more variables. In R, these tables can be created using table() along with some of its variations. To use table(), simply add in the variables you want to tabulate separated by a comma.

Can you plot a table? ›

Many plotting functions can plot data directly from a table. You pass the table as the first argument to the function followed by the variables you want to plot. You can specify either a table or a timetable, and in many cases, you can plot multiple data sets together in the same axes.

What are the three plotting systems in R? ›

Watch a video of this chapter. There are three different plotting systems in R and they each have different characteristics and modes of operation. They three systems are the base plotting system, the lattice system, and the ggplot2 system. This chapter (and this book) will focus primarily on the base plotting system.

What is the difference between Ggplot and R plot? ›

The graph made by ggplot is more clear. The default color is not grey and black and we can see the labels in x direction and we also have legends by default. However, the graph made by normal r packages doesn't have default legends and x labels.

What is the R function for plots? ›

The R base function plot() can generate a range of different plots from some user supplied data. If we provide one vector of continous data, it plots that on the y-axis against the index on the x-axis. If we want to plot a scatterplot between two continuous variables, we provide both to the plot() function.

Why is ggplot so good? ›

The answer is that ggplot2 is declaratively and efficient in creating data visualization based on The Grammar of Graphics. The layered grammar makes developing charts structural and effusive. Generating ggplot2 feels like playing with LEGO blocks.

Does ggplot need a data frame? ›

ggplot only works with data frames, so we need to convert this matrix into data frame form, with one measurement in each row. We can convert to this “long” form with the melt function in the library reshape2 .

What does the ggplot stand for? ›

ggplot. ggplot2 [library(ggplot2)] ) is a plotting library for R developed by Hadley Wickham, based on Leland Wilkinson's landmark book The Grammar of Graphics ["gg" stands for Grammar of Graphics]. Some documentation can be found on the ggplot website .

What is the R bar chart used for? ›

R charts are used to monitor the variation of a process based on samples taken from the process at given times (hours, shifts, days, weeks, months, etc.). The measurements of the samples at a given time constitute a subgroup.

What is graphical analysis in R? ›

R language is mostly used for statistics and data analytics purposes to represent the data graphically in the software. To represent those data graphically, charts and graphs are used in R.

How do you save an R graph as an image? ›

Plots panel –> Export –> Save as Image or Save as PDF

It's also possible to save the graph using R codes as follow: Specify files to save your image using a function such as jpeg(), png(), svg() or pdf(). Additional argument indicating the width and the height of the image can be also used.

What is grid R? ›

Grid is an add-on package for the R language and environment for statistical computing and graphics.

References

Top Articles
Latest Posts
Article information

Author: Duane Harber

Last Updated:

Views: 5486

Rating: 4 / 5 (51 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Duane Harber

Birthday: 1999-10-17

Address: Apt. 404 9899 Magnolia Roads, Port Royceville, ID 78186

Phone: +186911129794335

Job: Human Hospitality Planner

Hobby: Listening to music, Orienteering, Knapping, Dance, Mountain biking, Fishing, Pottery

Introduction: My name is Duane Harber, I am a modern, clever, handsome, fair, agreeable, inexpensive, beautiful person who loves writing and wants to share my knowledge and understanding with you.