Make Beautiful Tables with the Formattable Package

I love the formattable package, but I always struggle to remember its syntax. A quick Google search reveals that I’m not alone in this struggle. This post is intended as a reminder for myself of how the package works – and hopefully you’ll find it useful too!

Default formattable example

The table below is an R data frame (you can turn most things into a data frame using as.data.frame(x), where x is whatever you are converting). It’s by no means as bad as most R tables, but clearly it is not good enough to be shared with others.

 

If we give this table (called prevalence) to formattable, it does a great job just using defaults.

Column alignment

We can control column alignment using the align parameter. In the example below, I set the first column to left-aligned, and the remaining columns are right-aligned.

Simple column formatting

The main reason people love formattable is the formatting of columns. Below, the first column has been changed to grey, color bars have been added to Average, and the last column has been formatted as percentages. Note that the variable names are surrounded by backticks (the key above your Tab on English-language keyboards), not single quotation marks.

More customized column formatting

In the example above, prior to using formattable I divided the last column by 100, as formattable‘s percent function assumes the inputs are decimals. However, we can perform transformations within formattable. In the code below, I divide by 100 and I also color the values as red or green depending on their value. Note that in the bottom two lines, we define x as being the value by placing it to the left of the ~ and then use it in the function to the right (it is a lambda function, to use some jargon).

Below I extend this even further, replacing the percentages with ticks, crosses, and words.

Controlling the width of the bars

In the table below I have used the standard color bar, which scales the bars so that the bar lengths are proportional to the values being displayed. However, IQ cannot really be 0, so arguably the bars are misleading.

The fix to this problem is to provide a function that has a more appropriate mapping between the values and the length of the bars. In the code below, I create a function that returns a 0 for the lowest value (70), and a 1 for the highest value (150).

Formatting areas (ranges of cells)

It is possible to also set the shading of ranges of cells,  rather than just individual columns. In the example below, I’ve created a heatmap using two shades of green. In this case I have specified the area using just the columns, but row can also be supplied as well as or in place of col.

In this next example, I first format all the cells to be percentages, and then apply the color shading to the year columns. I have to wrap percent in another function, as percent only works on a single column of numbers.

Custom formatters

You can also write your own functions for controlling formatting. In the example below, rather than use formattable‘s in-built color_tile (as done in the previous example), I’ve instead customized it, controlling the padding, border radios, and font color.

Arrows (and hiding columns)

Sometimes it is useful to use arrows to show statistical significance. While formattable is not super flexible in this regard, it can do a good job nonetheless. The first step is to create a table where in addition to the data to be displayed, we also have a column containing z-scores.

In the code below I first hide the column called z (z = FALSE), add arrows for z-scores of less than -1.96 and greater than 1.96, and make z scores of greater than 0 green and less than 0 red.

A problem with this table is that the arrows are to the left of the numbers and are not lined up neatly. The only way I have figured out to avoid this is to put the arrows in a separate column, as shown here:

This is done by:

  • Replacing z with an invisible character ( ).

  • Replacing the values with the arrows.

 

Adding sparklines to tables

The sparklines package can be used to create sparklines

We can also include them in formattable tables.

The way that we do this is by converting the sparkline into text (character(htmltools::as.tags),  and then (in the last two lines), telling the formattable HTML widget that it also contains sparklines.

Putting it all together

In this final example, I combine many of the different ideas I’ve discussed into one table.

View all the source code and play around with these examples yourself

I’ve created all the examples in this post in a live Displayr document, so you can look at the code and play around with it yourself. Click here to view the code and tables discussed in this post.

Acknowledgements

The main example and many of the ideas in this post are from LITTLE MISS DATA, although I’ve reworked the code quite significantly. The hack for getting sparklines into the tables comes from HTML widget guru Kent Russell. Bert Wassink provided the trick for having a blank column name. My colleague Justin helped me a lot with this post.

View and modify the code in all these examples here!

Related