mmtable2: ggplot2 for tables
Written by Matt Dancho
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
- BONUS: Learn how to customize the basic table beyond
mmtable2defaults so you can wow your customers, your boss, and executives in your company.
This article was last updated on: March 8th, 2022
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. 👇
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.
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
Thank you both for everything you do!
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:
- Load Libraries: Load
- Import Data: We’re using the
mpgdataset that comes with
Our data looks like this.
Step 2: Tidy the Data
Our next step is to use
tidyr to get the data into the right format for the table. We’ll use 4 important data wrangling operations:
group_by(): Groups by our grouping columns: Manufacturer and Number of Engine Cylinders.
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.
ungroup(): Ungrouping is needed to remove any leftover groups.
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
ggplot2the “long” format is critical to plotting.
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:
- Setup the
mmtable(): This is just like
ggplot()function in ggplot2.
- Specify the headers locations: This tells the location for each header needed to organize the table.
- Format the header and table cells: This adds the lines that help to differentiate groups in our data.
The output of the basic table looks like this. Excellent work so far.
BONUS: Customize the table with
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.
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.
My Struggles with Learning Data Science
It took me a long time to learn how to apply data science to business. And I made a lot of mistakes as I fumbled through learning R.
I specifically had a tough time navigating the ever-increasing landscape of tools and packages, trying to pick between R and Python, and getting lost along the way.
If you feel like this, you’re not alone.
In fact, that’s the driving reason that I created Business Science and Business Science University (You can read about my personal journey here).
What I found out is that:
- Data Science does not have to be difficult, it just has to be taught from a business perspective
- Anyone can learn data science fast provided they are motivated.
How I can help
If you are interested in learning R and the ecosystem of tools at a deeper level, then I have a streamlined program that will get you past your struggles and improve your career in the process.
It’s my 5-Course R-Track System. It’s an integrated system containing 5 courses that work together on a learning path. Through 8 projects, you learn everything you need to help your organization: from data science foundations, to advanced machine learning, to web applications and deployment.
The result is that you break through previous struggles, learning from my experience & our community of 2653 data scientists that are ready to help you succeed.
Ready to take the next step? Then let’s get started.