3MW (Guidelines for better tables)

Guten Tag!

Many greetings from Ulm, Germany. Today, we're going to create sexy tables. That's right. You read that correctly. Sexy tables.

Well, maybe "sexy" is a stretch. I'm not even sure what would classify a table as sexy (do not try to google that 🙈). But we're certainly going to create tables that are more aesthetically pleasing than this one.

Shamefully, I must admit that there was a time (not so long ago) that I totally created such monstrosities. Luckily, I've learned a couple of guidelines to improve tables. And today I'll share what I've learned.

Avoid vertical lines

This is the guideline that gives you the biggest bang for your buck. The above table uses waaaay to many grid lines. Without vertical lines, the table will look less cramped. Have a look for yourself.

Bonus: I'm using the {gt} package to create this plot in R and it comes with a few sensible defaults. Texts are left-aligned and numbers are right-aligned. This ensures better readability compared to the all-centered approach in the original table.

Use better column names

Currently, the column names take up too much space. We can avoid that by combining columns. This can also avoid having to repeat partial names in multiple columns. Here's how that could look.

Replace repetitive columns with grouped tables

The island column is somewhat repetitive. In cases like these, I'd rather remove the column. Instead, I would group the table using additional rows. I like to think that this comes with better readability.

Remove missing numbers

Notice that our table has a lot of zeroes in it. For better readability, let us replace the zeroes with something more lightweight.

Add summary lines

Now, this table looks already cleaner than what we started with. In this format, we could even add more information at little cost. For example, we could add a summary for each group. In this case, a summary could be as simple as a total count (over all years).

Though, I'm a bit worried that this table will get too long. That's why I've made it a little more compact (by reducing the row heights). Check it out.

Alright, I like to think that this table looks much better now. Of course, there's still a lot that could be customized. But that's a story for another time.

Recommended Resources

The {gt} package: All tables (except the first one) were produced with the amazing {gt} package. The documentation is pretty great because it contains many instructive examples. Find them here. Also, if {gt} is not enough for you, you can always extend its features with packages such as {gtExtras} and {gtsummary}.

{knitr} and the {kableExtra} package: The {knitr} package comes with another powerful engine to create tables with R. Personally, I use it whenever I need to create math-heavy tables for a LaTeX document. Technically, {gt} can do that too. But last time I checked, its math output wasn't as good. Also, you can enhance the features of knitr::kable() with the {kableExtra} package.

That's a wrap! As always, I'm happy to hear your feedback about this week's issue. Feel free to write me on Twitter or reply to this mail.

Enjoy the rest of your day!Albert

Reply

or to participate.