library(gapminder)
library(dplyr)
library(tidyr)
library(stringr)
library(purrr)
library(gt)
library(mmtable2)
mmtable2 outputs tables using the gt package’s format.
This means you can alter formatting using many existing gt styling commands.
gm_table_formatted <-
gapminder_mm %>%
filter(var != "Life expectancy") %>%
mmtable(cells = value) +
header_top(year) +
header_left(country) +
header_top_left(var) +
header_left_top(continent) +
cells_format(cell_predicate = T, style = list(cell_text(align = "right"))) +
header_format(header = year, style = list(cell_text(align = "right"))) +
header_format("all_cols", style = list(cell_text(weight = "bolder"))) +
header_format("all_rows", style = list(cell_text(weight = "bolder"))) +
header_format(continent,scope = "table",style = list(cell_borders(sides = "top",color = "grey")))
print(gm_table_formatted)
GDP | Population | ||||||||
1992 | 1997 | 2002 | 2007 | 1992 | 1997 | 2002 | 2007 | ||
Africa | Botswana | 8.0 | 8.6 | 11.0 | 12.6 | 1.3 | 1.5 | 1.6 | 1.6 |
Gabon | 13.5 | 14.7 | 12.5 | 13.2 | 1.0 | 1.1 | 1.3 | 1.5 | |
Americas | Canada | 26.3 | 29.0 | 33.3 | 36.3 | 28.5 | 30.3 | 31.9 | 33.4 |
United States | 32.0 | 35.8 | 39.1 | 43.0 | 256.9 | 272.9 | 287.7 | 301.1 | |
Asia | Kuwait | 34.9 | 40.3 | 35.1 | 47.3 | 1.4 | 1.8 | 2.1 | 2.5 |
Singapore | 24.8 | 33.5 | 36.0 | 47.1 | 3.2 | 3.8 | 4.2 | 4.6 | |
Europe | Ireland | 17.6 | 24.5 | 34.1 | 40.7 | 3.6 | 3.7 | 3.9 | 4.1 |
Norway | 34.0 | 41.3 | 44.7 | 49.4 | 4.3 | 4.4 | 4.5 | 4.6 | |
Oceania | Australia | 23.4 | 27.0 | 30.7 | 34.4 | 17.5 | 18.6 | 19.5 | 20.4 |
New Zealand | 18.4 | 21.1 | 23.2 | 25.2 | 3.4 | 3.7 | 3.9 | 4.1 |
Table headers can be merged with header_merged_cols()
.
This supports an aributrary number of column headers.
style_list <- list(cell_borders(sides = "top",color = "lightgrey",weight = px(3)))
gm_df <- gapminder_mm %>% filter(var != "Life expectancy")
style_list3 = list(cell_text(align = "left"))
gm_table_merged <-
gm_df %>%
mmtable(cells = value) +
header_left(year) +
header_top(country) +
header_left_top(var) +
header_top_left(continent) +
header_format(var,scope = "table",style = style_list) +
header_format(continent,style_list3 ) +
header_merged_cols()
print(gm_table_merged)
|
Botswana | Gabon | Canada | United States | Kuwait | Singapore | Ireland | Norway | Australia | New Zealand | |||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
GDP | 1992 | 8.0 | 13.5 | 26.3 | 32.0 | 34.9 | 24.8 | 17.6 | 34.0 | 23.4 | 18.4 | ||
1997 | 8.6 | 14.7 | 29.0 | 35.8 | 40.3 | 33.5 | 24.5 | 41.3 | 27.0 | 21.1 | |||
2002 | 11.0 | 12.5 | 33.3 | 39.1 | 35.1 | 36.0 | 34.1 | 44.7 | 30.7 | 23.2 | |||
2007 | 12.6 | 13.2 | 36.3 | 43.0 | 47.3 | 47.1 | 40.7 | 49.4 | 34.4 | 25.2 | |||
Population | 1992 | 1.3 | 1.0 | 28.5 | 256.9 | 1.4 | 3.2 | 3.6 | 4.3 | 17.5 | 3.4 | ||
1997 | 1.5 | 1.1 | 30.3 | 272.9 | 1.8 | 3.8 | 3.7 | 4.4 | 18.6 | 3.7 | |||
2002 | 1.6 | 1.3 | 31.9 | 287.7 | 2.1 | 4.2 | 3.9 | 4.5 | 19.5 | 3.9 | |||
2007 | 1.6 | 1.5 | 33.4 | 301.1 | 2.5 | 4.6 | 4.1 | 4.6 | 20.4 | 4.1 |
Adding the add_
prefix to functions allows use of
%>%
in place of +
.
gm_table_piped <-
gapminder_mm %>%
filter(var != "Life expectancy") %>%
mmtable(cells = value, use_default_formats = T) %>%
add_header_top(year) %>%
add_header_left(country) %>%
add_header_top_left(var) %>%
add_header_left_top(continent) %>%
add_cells_format(cell_predicate = T, style = list(cell_text(align = "right"))) %>%
add_header_format(header = year, style = list(cell_text(align = "right"))) %>%
add_header_format("all_cols", style = list(cell_text(weight = "bolder"))) %>%
add_header_format("all_rows", style = list(cell_text(weight = "bolder"))) %>%
add_header_format(continent,scope = "table",style = style_list)
print(gm_table_piped)
GDP | Population | ||||||||
1992 | 1997 | 2002 | 2007 | 1992 | 1997 | 2002 | 2007 | ||
Africa | Botswana | 8.0 | 8.6 | 11.0 | 12.6 | 1.3 | 1.5 | 1.6 | 1.6 |
Gabon | 13.5 | 14.7 | 12.5 | 13.2 | 1.0 | 1.1 | 1.3 | 1.5 | |
Americas | Canada | 26.3 | 29.0 | 33.3 | 36.3 | 28.5 | 30.3 | 31.9 | 33.4 |
United States | 32.0 | 35.8 | 39.1 | 43.0 | 256.9 | 272.9 | 287.7 | 301.1 | |
Asia | Kuwait | 34.9 | 40.3 | 35.1 | 47.3 | 1.4 | 1.8 | 2.1 | 2.5 |
Singapore | 24.8 | 33.5 | 36.0 | 47.1 | 3.2 | 3.8 | 4.2 | 4.6 | |
Europe | Ireland | 17.6 | 24.5 | 34.1 | 40.7 | 3.6 | 3.7 | 3.9 | 4.1 |
Norway | 34.0 | 41.3 | 44.7 | 49.4 | 4.3 | 4.4 | 4.5 | 4.6 | |
Oceania | Australia | 23.4 | 27.0 | 30.7 | 34.4 | 17.5 | 18.6 | 19.5 | 20.4 |
New Zealand | 18.4 | 21.1 | 23.2 | 25.2 | 3.4 | 3.7 | 3.9 | 4.1 |
mmtable2 uses the style features of gt. This gives you a lot of control over the format of your table. As an example, a rainbow histogram is constructed below.
The code below generates a set of random numbers and bins them in 10s.
library(mmtable2)
library(tidyverse)
set.seed(123)
df <-
tibble(data = rnorm(100,100,30) %>% round()) %>%
mutate(rounded = floor(data/10)*10) %>%
mutate(rounded_chr = paste0(rounded,"'s")) %>%
arrange(rounded,desc(data))
df %>% glimpse
#> Rows: 100
#> Columns: 3
#> $ data <dbl> 31, 49, 41, 54, 69, 69, 69, 68, 68, 66, 66, 63, 62, 62, 79…
#> $ rounded <dbl> 30, 40, 40, 50, 60, 60, 60, 60, 60, 60, 60, 60, 60, 60, 70…
#> $ rounded_chr <chr> "30's", "40's", "40's", "50's", "60's", "60's", "60's", "6…
The code below constructs a table with these values, and then adds a list of formatting commands.
# Generate random data
df <-
tibble(data = rnorm(100,100,30) %>% round()) %>%
mutate(rounded = floor(data/10)*10) %>%
mutate(rounded_chr = paste0(rounded,"'s")) %>%
arrange(rounded,desc(data))
# Create table
table <-
df %>%
mmtable(cells = data) +
header_top_left(rounded_chr) +
header_format(rounded_chr,
list(gt::cell_fill(color = "black"),
gt::cell_text(color = "white")))
# Format table
colors <- c("#ff0000","#ffa500","#ffff00","#008000",
"#0000ff","#4b0082","#ee82ee")
color_list <-
map2(unique(df$rounded_chr),rep(colors,2),
function(nums,colours){
cells_format(cell_predicate =nchar(data) %in% c(2,3) &
rounded_chr == nums,
gt::cell_fill(color = colours))
}
)
print(append(list(table),color_list) %>% reduce(`+`))
100’s | 110’s | 120’s | 130’s | 140’s | 150’s | 160’s | 190’s | 30’s | 50’s | 60’s | 70’s | 80’s | 90’s |
99 | |||||||||||||
109 | 89 | 99 | |||||||||||
109 | 89 | 98 | |||||||||||
109 | 89 | 98 | |||||||||||
108 | 87 | 96 | |||||||||||
107 | 119 | 79 | 87 | 94 | |||||||||
106 | 118 | 69 | 78 | 86 | 94 | ||||||||
104 | 118 | 129 | 57 | 69 | 76 | 85 | 93 | ||||||
103 | 117 | 128 | 56 | 68 | 75 | 85 | 93 | ||||||
103 | 116 | 124 | 56 | 67 | 74 | 85 | 92 | ||||||
103 | 116 | 123 | 55 | 64 | 73 | 84 | 92 | ||||||
102 | 114 | 123 | 138 | 53 | 62 | 72 | 83 | 92 | |||||
102 | 113 | 122 | 134 | 164 | 52 | 62 | 71 | 82 | 90 | ||||
101 | 111 | 121 | 133 | 157 | 163 | 51 | 61 | 71 | 81 | 90 | |||
101 | 110 | 121 | 132 | 143 | 155 | 160 | 197 | 38 | 50 | 61 | 70 | 80 | 90 |