HAV*_*AVB 5 r ggplot2 r-markdown
我真的很喜欢Flexdashboards和Shinydashboards 中R值框的简洁外观:
是否有使用熟悉的工具(例如ggplot2
)的包装或配方来轻松生成静态报告的类似输出?例如,在从RMarkdown编织的PDF文档中
编辑:
经过几次尝试,我使用ggplot2得到了一些有用的东西(下面的答案)。欢迎其他答案!
Ste*_*vey 15
我通过使用Emojifont R 包扩展了 HAVB 的答案,使外观更像值框。
下面是从 HAVB 的答案中修改的代码,包括字体很棒的图标和表情符号的混合。我还将文本部分分成大 ( value
) 和小 ( info
) 文本,以便我可以不同地调整它们的大小。
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)
Run Code Online (Sandbox Code Playgroud)
我认为结果非常好,可以进一步改进以更美观。
我创建了一个函数来使 @StefanAvey 的代码更加可移植。
我把它放在 GitHub 的存储库中,这样任何人都可以做得更好。
现在,框的高度和宽度、框之间的填充以及行数都是参数。
有一些事情我还没有实现,比如对长度不等的行的处理。
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)
}
Run Code Online (Sandbox Code Playgroud)
它会被这样调用:
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)
Run Code Online (Sandbox Code Playgroud)
ggplot2
'sgeom_tile
可用于制作彩色矩形,然后geom_text
添加标签/信息。与使用infobox
/ valuebox
in的易用性相比,该解决方案很笨拙shiny
,但它确实可以。
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)
Run Code Online (Sandbox Code Playgroud)