Valuebox like function for static reports
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)
Extending your solution to display a data frame using your design in the same arrangement as it would print to the console:
The data frame:
mydata = data.frame(x1 = c(1,2,3,4,5),
x2 = c(9,8,7,6,5),
label = c("description a",
"description b",
"description c",
"description d",
"description e"))
Intended to display as:
1 9 description a
2 8 description b
3 7 description c
4 6 description d
5 5 description e
Done with the following:
ht = 3
wd1 = 3
wd2 = 9
gap = 0.5
nc = ncol(mydata)
nr = nrow(mydata)
x = rep(c(seq(0,(nc-2)*(wd1+gap), wd1+gap), (nc-2)*(wd1+gap) + gap + 0.5*(wd2+wd1)), nr)
y = rep(seq(0,(nr-1)*(ht+gap), ht+gap), nc) %>% sort()
h = rep(ht, nr * nc)
w = rep(c(rep(wd1, nc-1), wd2), nr)
info = as.vector(t(as.matrix(mydata[nr:1,])))
df = data.frame(x = x, y = y, h = h, w = w, info = info)
ggplot(df, aes(x, y, height = h, width = w, label = info)) +
geom_tile() +
geom_text(color = "white", fontface = "bold") +
coord_fixed() +
scale_fill_brewer(type = "qual",palette = "Dark2") +
theme_void() +
guides(fill = F)
With the following result: Resulting Valuebox-like plot
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: