I know you love formattable
’s color_tile
and color_bar
. I do too. In my mind, these two features are among the best visualization methods in a tabular format. You may wonder if it’s possible use it together with kableExtra
so you can have a nested table with awesome presentation at the same time. Here is how.
library(knitr)
library(kableExtra)
library(formattable)
library(dplyr)
mtcars[1:5, 1:4] %>%
mutate(
car = row.names(.),
mpg = color_tile("white", "orange")(mpg),
cyl = cell_spec(cyl, "html", angle = (1:5)*60,
background = "red", color = "white", align = "center"),
disp = ifelse(disp > 200,
cell_spec(disp, "html", color = "red", bold = T),
cell_spec(disp, "html", color = "green", italic = T)),
hp = color_bar("lightgreen")(hp)
) %>%
select(car, everything()) %>%
kable("html", escape = F) %>%
kable_styling("hover", full_width = F) %>%
column_spec(5, width = "3cm") %>%
add_header_above(c(" ", "Hello" = 2, "World" = 2))
car | mpg | cyl | disp | hp |
---|---|---|---|---|
Mazda RX4 | 21.0 |
6
|
160
|
110 |
Mazda RX4 Wag | 21.0 |
6
|
160
|
110 |
Datsun 710 | 22.8 |
4
|
108
|
93 |
Hornet 4 Drive | 21.4 |
6
|
258
|
110 |
Hornet Sportabout | 18.7 |
8
|
360
|
175 |
Here is a little bit more explanation. Both formattable::color_tile
and formattable::color_bar
returns a function object which can take a numeric vector. That’s why you can put things like (mpg)
after color_tile("white", "orange")
as you can see in the mutate
function. This way of using the function may look a little odd but is totally valid (, I think :P).
Also, if you are using it in this way, make sure you put escape = F
in kable
.
On the other hand, cell_spec()
is a new function in kableExtra()
to format cells before you pipe the table into kable
. Note that you can either specify format (html
or latex
) in function or do that via options(knitr.table.format)
so you don’t need to do it everytime.
I also added a few helper functions to use together with cell_spec. One good example is spec_color
, which gives you the ability to use viridis color map in your table. Others include spec_font_size
and spec_angle
.
iris[1:10, ] %>%
mutate_if(is.numeric, function(x){
cell_spec(x, "html", color = spec_color(x), bold = T)
}) %>%
kable("html", escape = F, align = "c") %>%
kable_styling("condensed", full_width = F)
Sepal.Length | Sepal.Width | Petal.Length | Petal.Width | Species |
---|---|---|---|---|
5.1
|
3.5
|
1.4
|
0.2
|
setosa |
4.9
|
3
|
1.4
|
0.2
|
setosa |
4.7
|
3.2
|
1.3
|
0.2
|
setosa |
4.6
|
3.1
|
1.5
|
0.2
|
setosa |
5
|
3.6
|
1.4
|
0.2
|
setosa |
5.4
|
3.9
|
1.7
|
0.4
|
setosa |
4.6
|
3.4
|
1.4
|
0.3
|
setosa |
5
|
3.4
|
1.5
|
0.2
|
setosa |
4.4
|
2.9
|
1.4
|
0.2
|
setosa |
4.9
|
3.1
|
1.5
|
0.1
|
setosa |
mtcars %>%
tidyr::gather(var, value) %>%
group_by(var) %>%
summarise(
mean = mean(value),
sd = sd(value),
sparkline = list(sparkline::sparkline(value))
) %>%
ungroup() %>%
mutate(
stat = paste0(mean, " (", sd, ")"),
stat = cell_spec(stat, color = spec_color(mean))
) %>%
select(var, stat, sparkline) %>%
kable("html", escape = F) %>%
kable_styling(full_width = F)
## Setting cell_spec format as html
var | stat | sparkline |
---|---|---|
am |
0.40625 (0.498990917235846)
|
1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 20, 60, 60, 20, 60, 20, 0, 0, 0, 1 |
carb |
2.8125 (1.61519997763185)
|
4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, 1, 1, 2, 2, 4, 2, 1, 2, 2, 4, 6, 8, 2, 20, 60, 60, 20, 60, 20, 0, 0, 0, 1 |
cyl |
6.1875 (1.78592164694654)
|
6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 8, 8, 8, 8, 4, 4, 4, 8, 6, 8, 4, 20, 60, 60, 20, 60, 20, 0, 0, 0, 1 |
disp |
230.721875 (123.938693831382)
|
160.0, 160.0, 108.0, 258.0, 360.0, 225.0, 360.0, 146.7, 140.8, 167.6, 167.6, 275.8, 275.8, 275.8, 472.0, 460.0, 440.0, 78.7, 75.7, 71.1, 120.1, 318.0, 304.0, 350.0, 400.0, 79.0, 120.3, 95.1, 351.0, 145.0, 301.0, 121.0, 20.0, 60.0, 60.0, 20.0, 60.0, 20.0, 0.0, 0.0, 0.0, 1.0 |
drat |
3.5965625 (0.534678736070971)
|
3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.92, 3.92, 3.07, 3.07, 3.07, 2.93, 3.00, 3.23, 4.08, 4.93, 4.22, 3.70, 2.76, 3.15, 3.73, 3.08, 4.08, 4.43, 3.77, 4.22, 3.62, 3.54, 4.11, 20.00, 60.00, 60.00, 20.00, 60.00, 20.00, 0.00, 0.00, 0.00, 1.00 |
gear |
3.6875 (0.737804065256947)
|
4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3, 3, 3, 3, 3, 4, 5, 5, 5, 5, 5, 4, 20, 60, 60, 20, 60, 20, 0, 0, 0, 1 |
hp |
146.6875 (68.5628684893206)
|
110, 110, 93, 110, 175, 105, 245, 62, 95, 123, 123, 180, 180, 180, 205, 215, 230, 66, 52, 65, 97, 150, 150, 245, 175, 66, 91, 113, 264, 175, 335, 109, 20, 60, 60, 20, 60, 20, 0, 0, 0, 1 |
mpg |
20.090625 (6.0269480520891)
|
21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.2, 17.8, 16.4, 17.3, 15.2, 10.4, 10.4, 14.7, 32.4, 30.4, 33.9, 21.5, 15.5, 15.2, 13.3, 19.2, 27.3, 26.0, 30.4, 15.8, 19.7, 15.0, 21.4, 20.0, 60.0, 60.0, 20.0, 60.0, 20.0, 0.0, 0.0, 0.0, 1.0 |
qsec |
17.84875 (1.78694323609684)
|
16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 22.90, 18.30, 18.90, 17.40, 17.60, 18.00, 17.98, 17.82, 17.42, 19.47, 18.52, 19.90, 20.01, 16.87, 17.30, 15.41, 17.05, 18.90, 16.70, 16.90, 14.50, 15.50, 14.60, 18.60, 20.00, 60.00, 60.00, 20.00, 60.00, 20.00, 0.00, 0.00, 0.00, 1.00 |
vs |
0.4375 (0.504016128774185)
|
0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 20, 60, 60, 20, 60, 20, 0, 0, 0, 1 |
wt |
3.21725 (0.978457442989697)
|
2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3.150, 3.440, 3.440, 4.070, 3.730, 3.780, 5.250, 5.424, 5.345, 2.200, 1.615, 1.835, 2.465, 3.520, 3.435, 3.840, 3.845, 1.935, 2.140, 1.513, 3.170, 2.770, 3.570, 2.780, 20.000, 60.000, 60.000, 20.000, 60.000, 20.000, 0.000, 0.000, 0.000, 1.000 |