knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
R has some great packages for creating nice-looking tables. Packages like gt
and flextable
allow the user to create a wide range of tables, with lots of flexibility in how these are presented and formatted. The downside to these Swiss army knife-style packages is that simple tasks, like creating a frequency table, require a lot of typing. Enter ivo_table
, a package for creating great-looking frequency tables and contingency tables without any hassle.
Let's look at some examples using the penguins
data from the palmerpenguins
package. Say that we want to create a contingency table showing the counts of the categorical variables species
, sex
, and island
. We can use ftable
along with dplyr
's select
:
library(dplyr) library(palmerpenguins) penguins |> select(species, sex, island) |> ftable()
While informative, the formatting isn't great, and it's not something that we can easily export to a report or presentation.
ivo.table
uses the same syntax, but with ivo_table
instead of ftable
:
library(ivo.table) penguins |> select(species, sex, island) |> ivo_table()
The resulting table can easily be exported to a Word document:
penguins |> select(species, sex, island) |> ivo_table() |> flextable::save_as_docx(path = "example_table.docx")
You can add row and column sums:
penguins |> select(species, sex, island) |> ivo_table(colsums = TRUE, rowsums = TRUE)
Or show percentages instead of counts, e.g. computed by column:
penguins |> select(species, sex, island) |> ivo_table(percent_by = "col")
ivo_table
has lots of options for customising the look of your table. You can change the colours and fonts used, highlight columns, rows or cells, make columns bold, and more. Let's look at some examples.
Change the font to Courier, use red instead of green, and make the names in the sex
column bold:
penguins |> select(species, sex, island) |> ivo_table(color = "red", font_name = "Courier", bold_cols = 1)
Add a caption and highlight the cell on the fourth row of the third column:
penguins |> select(species, sex, island) |> ivo_table(caption = "A table with penguins in it", highlight_cols = 3, highlight_rows = 4)
ivo_table
returns a flextable
object, meaning that all functions used to style flextables can be used. For instance, you can change the font size used in different parts of the table using flextable::fontsize
and change the background colour using flextable::bg
:
penguins |> select(species, sex, island) |> ivo_table(color = "darkblue") |> flextable::fontsize(size = 8, part = "body") |> flextable::fontsize(size = 12, part = "header") |> flextable::bg(bg = "pink", part = "all")
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.