r - 用于静态报告的类似 Valuebox 的函数

标签 r ggplot2 r-markdown

我真的很喜欢 R 的干净外观 valueboxes在flexdashboards和shinydashboards中:

enter image description here

是否有一个包或一个使用熟悉的工具(即ggplot2)的方法可以轻松地为静态报告生成类似的输出?例如,在由 RMarkdown 编写的 PDF 文档中

编辑:

经过几次尝试,我得到了一些可以使用 ggplot2 的东西(答案如下)。欢迎其他答案!

最佳答案

我通过使用Emojifont R package扩展了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)

resulting plot

我认为结果相当不错,可以进一步改进,更加美观。

关于r - 用于静态报告的类似 Valuebox 的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47105282/

相关文章:

r - 编写data.frames列表以使用lapply分隔CSV文件

r - 如何自动在R包中加载数据?

r - 为什么这个循环没有被取消切换?

R ggplot2 : legend should be discrete and not continuous

markdown - 如何在 Markdown 演示文稿中添加新行?

r - 使用 slidify 将图像添加到标题幻灯片

r - knitr - 如何对齐代码并并排绘制

r - ggplot2:组合组、颜色和线型

r - 使用 geom_smooth 将 glm 拟合到分数

r - 在 R Markdown 中向 Beamer 演示文稿添加左侧边栏