mmtable2: ggplot2 for tables

Written by Matt Dancho on May 4, 2021



ggplot2… for tables?! Holy cow 🐮 - when I saw mmtable2 I couldn’t believe my eyes. Someone figured out how to make creating tables just like creating plots with ggplots, using a grammar of graphics… err, I mean tables.

My initial thought is how VALUABLE you are going to be to your organization when you can make professional reports that highlight key insights AND look super professional. I mean, just send a report to a customer with one of these tables in it. Dang! You are getting BONUS POINTS for sure.

Now, for the record, saying “ggplot2 for tables” is a bold statement. So let’s figure out what making professional tables with mmtable2 looks like in this 5-minute tutorial.

  • Learn how to make a basic table with mmtable2.
  • BONUS: Learn how to customize the basic table beyond mmtable2 defaults so you can wow your customers, your boss, and executives in your company.

This article was last updated on: March 8th, 2022

R-Tips Weekly

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

Video Tutorial

Learn how to use the mmtable2 package in our 8-minute YouTube video tutorial.

What you make in this R-Tip

By the end of this tutorial, you’ll make the 4 most helpful plots for explaining machine learning models.

Thank You Developers.

Before we move on, please recognize that mmtable2 was developed by Ian Moran. Ian has put a ton of work into the mmtable2 documentation.

AND, I’d be remiss if I didn’t also credit Richard Iannone who created and manages the gt package, which mmtable2 leverages heavily. (And yes, I have another tutorial in the works on the amazing gt package).

Thank you both for everything you do!

mmtable2 Tutorial

Let’s get up and running with mmtable2 so we can make a killer table that impresses your bosses and helps you make reports that get you promoted.

Goal for our table

Our goal is to analyze the mpg dataset (fuel economy of vehicles by important vehicle attributes like manufacturer, number of cylinders, etc). The table we put to into our report:

  • Summarizes the average fuel econmony (City and Highway)
  • By two categories: Car Manufacturer and Number of Engine Cylinders

Our final table structure looks like this:

Step 1: Load the Libraries and Data

First, run this code to:

  1. Load Libraries: Load mmtable2 , gt, and tidyverse.
  2. Import Data: We’re using the mpg dataset that comes with ggplot2.

Get the code.

Our data looks like this.

Step 2: Tidy the Data

Our next step is to use dplyr and tidyr to get the data into the right format for the table. We’ll use 4 important data wrangling operations:

  1. group_by(): Groups by our grouping columns: Manufacturer and Number of Engine Cylinders.
  2. summarise(): We’ll calculate the average fuel economy for both City and Highway. We combine with the across() function which makes it easy to summarize multiple columns. We use the mean() function to calculate the averages by group.
  3. ungroup(): Ungrouping is needed to remove any leftover groups.
  4. pivot_longer(): Used to convert from a “wide” to a “long” data frame, which stacks the City and Highway average fuel economy on top of each other. If you’re familiar with ggplot2 the “long” format is critical to plotting.

Get the code.

The resulting data (post data wrangle) looks like this.

Step 3: Make the basic table

With the mpg data summarized and in the long format, we can now use mmtable2 to make a table, just like we would use ggplot2 to make a plot. We perform 3 actions:

  1. Setup the mmtable(): This is just like ggplot() function in ggplot2.
  2. Specify the headers locations: This tells the location for each header needed to organize the table.
  3. Format the header and table cells: This adds the lines that help to differentiate groups in our data.

Get the code.

The output of the basic table looks like this. Excellent work so far.

BONUS: Customize the table with gt

The magic of mmtable2 is that it actually uses another awesome package called gt, which is what allows mmtable2 to produce awesome-looking tables.

So, if we know how to use gt, we can customize our basic table!

Let’s give it a go by adding some gt headers, which give the table a title and subtitle.

And boom! Now we have a nice title and subtitle that describe what our report readers will be looking at.

Recap

We learned how to use the mmtable2 to not only create basic tables using ggplot2-style grammar of tables, but we showed how to customize the table using the gt package. Great work! But, there’s a lot more to becoming a data scientist.

If you’d like to become a data scientist (and have an awesome career, improve your quality of life, enjoy your job, and all the fun that comes along), then I can help with that.

Step 1: Watch my Free 40-Minute Webinar

Learning data science on your own is hard. I know because IT TOOK ME 5-YEARS to feel confident.

AND, I don’t want it to take that long for you.

So, I put together a FREE 40-minute webinar (a masterclass) that provides a roadmap for what worked for me.

Literally 5-years of learning, consolidated into 40-minutes. It’s jammed packed with value. I wish I saw this when I was starting… It would have made a huge difference.

Step 2: Take action

For my action-takers, if you are ready to take your skills to the next level and DON’T want to wait 5-years to learn data science for business, AND you want a career you love that earns you $100,000+ salary (plus bonuses), and you’d like someone to help you do this in UNDER 6-MONTHS or less….

Then I can help with that too. There’s a link in the FREE 40-minute webinar for a special price (because you are special!) and taking that action will kickstart your journey with me in your corner.

Get ready. The ride is wild. And the destination is AMAZING!


👇 Top R-Tips Tutorials you might like:

  1. mmtable2: ggplot2 for tables
  2. ggdist: Make a Raincloud Plot to Visualize Distribution in ggplot2
  3. ggside: Plot linear regression with marginal distributions
  4. DataEditR: Interactive Data Editing in R
  5. openxlsx: How to Automate Excel in R
  6. officer: How to Automate PowerPoint in R
  7. DataExplorer: Fast EDA in R
  8. esquisse: Interactive ggplot2 builder
  9. gghalves: Half-plots with ggplot2
  10. rmarkdown: How to Automate PDF Reporting
  11. patchwork: How to combine multiple ggplots
  12. Geospatial Map Visualizations in R

Want these tips every week? Join R-Tips Weekly.