Site icon R-bloggers

mmtable2: ggplot2 for tables

[This article was first published on business-science.io, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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. ?


mmtable2
A ggplot2-syntax for making tables

(Click image to play tutorial)

mmtable2
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:

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:


To leave a comment for the author, please follow the link and comment on their blog: business-science.io.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.