I really like the clean look of R's valueboxes in flexdashboards and shinydashboards:
Is there a package, or a recipe using familiar tools (i.e. ggplot2
) to easily produce similar output for a static report? For example, in a PDF document knitted from RMarkdown
EDIT:
After a few tries I got something usable using ggplot2 (answer below). Other answers welcome!
I created a function to make the code from @StefanAvey more portable.
I put it on GitHub in a repository, so anyone can make it better.
Right now, the height and width of the boxes, padding between them, and the number of rows are arguments.
There are a few things I have not implemented, like treatment for rows of unequal length.
createValueBoxes <- function(df, h = 4, w = 6, padding=0.5, rows = 2){
# required packages
library(ggplot2)
library(emojifont)
# verify our inputs
if (!is.data.frame(df)) {
stop(paste("Argument", deparse(substitute(df)), "must be a data.frame."))
}
if(!all(i <- rlang::has_name(df,c("values", "infos", "icons")))){
stop(sprintf(
"%s does not contain: %s",
deparse(substitute(df)),
paste(columns[!i], collapse=", ")))
}
boxes = nrow(df) # number of items passed
# calculate the grid
cols = boxes/rows
plotdf <- data.frame(
x = rep(seq(0, (w+padding)*cols-1, w+padding), times=rows),
y = rep(seq(0, (h+padding)*rows-1, h+padding), each=cols),
h = rep(h, boxes),
w = rep(w, boxes),
value = df$values,
info = df$infos,
icon = fontawesome(df$icons),
font_family = c(rep("fontawesome-webfont", boxes)),
color = factor(1:boxes)
)
print(plotdf)
ggplot(plotdf, aes(x, y, height = h, width = w, label = info)) +
## Create the tiles using the `color` column
geom_tile(aes(fill = color)) +
## Add the numeric values as text in `value` column
geom_text(color = "white", fontface = "bold", size = 10,
aes(label = value, x = x - w/2.2, y = y + h/4), hjust = 0) +
## Add the labels for each box stored in the `info` column
geom_text(color = "white", fontface = "bold",
aes(label = info, x = x - w/2.2, y = y-h/4), hjust = 0) +
coord_fixed() +
scale_fill_brewer(type = "qual",palette = "Dark2") +
## Use `geom_text()` to add the icons by specifying the unicode symbol.
geom_text(size = 20, aes(label = icon, family = font_family,
x = x + w/4, y = y + h/8), alpha = 0.25) +
theme_void() +
guides(fill = FALSE)
}
It would be called like so:
df <- data.frame(
values=c("50%", "7", "1245"),
infos=c("now", "super", "hours"),
icons=c("fa-gear", "fa-diamond", "fa-tasks")
)
createValueBoxes(df, rows=1)
The result from this example is:
I extended HAVB's answer to make the appearance more like valueboxes by using the Emojifont R package.
Below is the code modified from HAVB's answer including a mix of font-awesome icons and emojis. I also broke up the text parts into the large (value
) and small (info
) text so that I could size them differently.
library(ggplot2)
library(emojifont)
df <- data.frame(
x = rep(seq(2, 15, 6.5), 2),
y = c(rep(6.5, 3), rep(2,3)),
h = rep(4.25, 6),
w = rep(6.25, 6),
value = c("78%",
"+10K",
"8/10",
"ALL",
"6",
"< 0.5"),
info = c("meaningless plots",
"hours wasted",
"zombies prefer brains",
"dogs go to heaven",
"infoboxes",
"target pvalue"),
icon = c(fontawesome(search_fontawesome("chart")),
emoji("athletic_shoe")),
font_family = c(rep("fontawesome-webfont", 5),
"EmojiOne"),
color = factor(1:6)
)
ggplot(df, aes(x, y, height = h, width = w, label = info)) +
## Create the tiles using the `color` column
geom_tile(aes(fill = color)) +
## Add the numeric values as text in `value` column
geom_text(color = "white", fontface = "bold", size = 10,
aes(label = value, x = x - 2.9, y = y + 1), hjust = 0) +
## Add the labels for each box stored in the `info` column
geom_text(color = "white", fontface = "bold",
aes(label = info, x = x - 2.9, y = y - 1), hjust = 0) +
coord_fixed() +
scale_fill_brewer(type = "qual",palette = "Dark2") +
## Use `geom_text()` to add the icons by specifying the unicode symbol.
geom_text(size = 20, aes(label = icon, family = font_family,
x = x + 1.5, y = y + 0.5), alpha = 0.25) +
theme_void() +
guides(fill = FALSE)
I think the result is quite nice and could be further improved to be more aesthetically pleasing.
ggplot2
's geom_tile
can be used to make colorful rectangles, and then geom_text
to add the labels/info. The solution is clunky compared with the ease of using infobox
/ valuebox
in shiny
, but it will do.
library(ggplot2)
df <- data.frame(
x = rep(seq(2, 15, 6.5), 2),
y = c(rep(2,3), rep(6.5, 3)),
h = rep(4, 6),
w = rep(6, 6),
info = c("78%\nmeaningless plots",
"+10K\nhours wasted",
"8/10\nzombies prefer brains",
"ALL\ndogs go to heaven",
"6\ninfoboxes",
"< 0.5\ntarget pvalue"),
color = factor(1:6)
)
ggplot(df, aes(x, y, height = h, width = w, label = info, fill = color)) +
geom_tile() +
geom_text(color = "white", fontface = "bold") +
coord_fixed() +
scale_fill_brewer(type = "qual",palette = "Dark2") +
theme_void() +
guides(fill = F)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With