mmtable2: ggplot2 for tables

Written by Matt Dancho on May 4, 2021

This article is part of R-Tips Weekly, a weekly video tutorial that shows you step-by-step how to do common R coding tasks.

Here are the links to get set up. 👇

A ggplot2-syntax for making tables

(Click image to play tutorial)

This R package uses ggplot2 syntax to create great tables

I love ggplot2 for plotting. The grammar of graphics allows us to add elements to plots. Tables seem to be forgotten in terms of an intuitive grammar with tidy data philosophy - Until now. mmtable2 aims to be the ggplot2 for tables, leveraging the awesome GT table package.

The mmtable2 package aims to make it easy to create tables by:

  1. Using a ggplot2-style syntax for using a grammar of table operations.
  2. Extends the amazing GT table package.

Here's what we're making today:

Make professional tables using a ggplot-syntax

Before we get started, get the Cheat Sheet

mmtable2 is great for making tables fast. But, you'll still need to learn how to wrangle data. For those topics, I'll use the Ultimate R Cheat Sheet to refer to dplyr code in my workflow.

Quick example - Clicking the "CS" next to "dplyr" opens the Data Transformation with Dplyr Cheat Sheet.

Now you're ready to quickly reference dplyr functions. Ok, onto the tutorial.

Step 1: Load Libraries

The libraries we'll need today are mmtable2, gt, and tidyverse. As of this post, mmtable2 is not on CRAN so you'll need to install with github.

Step 2: Wrangle Data into Long Format

Like ggplot2, mmtable2 standardizes on the long-format (tidy-data format). According to the tidyr vignette:

In tidy data:

  1. Every column is a variable.
  2. Every row is an observation.
  3. Every cell is a single value.

To achieve the "tidy-data" format we need to leverage dplyr and tidyr (use the Ultimate R Cheatsheet to pull up data wrangling doc's).

A. We start with Raw Data

This is the mpg data set, which contains fuel economy and other attributes on a number of automobile manufacturers and car models.

B. We tidy with dplyr and tidyr

This is a standard data wrangling operation. I teach data wrangling in-depth in the R for Business Analysis course.

C. And we output "tidy data"

The data is now in "tidy" format, ready for making a table. Every column is a variable, every row is an observation, every cell is a single value.

Get the Code

Step 3: Make the table with mmtable2

The data is now wrangled into the tidy format. We can use mmtable2 to make the plot. To see mmtable2 in action, I have a full-tutorial on YouTube. The important points are:

  • mmtable() - The main argument (other than the incoming data) is our value column. In our case it's fuel_economy, the measure of average vehicle fuel efficiency.
  • Header Top and Header Top Left - These add column headers from features.
  • Head Left and Header Left Top - These add row headers from features.
  • Header Format and Table Format - These allow you to apply GT formatting functions.
Get the Code

And here is the professional table that was created, perfect for reports.

Code available in our Free R-Tips Github Repository

In Summary

You just quickly made a professional table using the ggplot2-style table package, mmtable2. This is an amazing accomplishment!!

You should be proud.

This article is part of R-Tips Weekly, a weekly video tutorial that shows you step-by-step how to do common R coding tasks. Join today.

👇 Top R-Tips Tutorials you might like: