【问题标题】:How to use map and ggplot to add custom titles to each chart?如何使用 map 和 ggplot 为每个图表添加自定义标题?
【发布时间】:2021-10-19 18:59:15
【问题描述】:

我有一个数据集,我想通过使用 purrr 中的 map() 为每个具有适当标题的变量制作简单的条形图。

我的数据集有 4 个变量,我有一个带有 4 个标题的向量。如何通过这些标题,以便 R 输出一个具有适当标题的图?对于我的最终结果,我想要 4 个地块,每个地块都有适当的标题(而不是 16 个地块,每个地块都是标题和地块的可能排列)。

我试过查看这个answer,但我的标题包含在另一个向量中。这个post 让我尽我所能。

这是我的数据和标题向量:

library(dplyr)
test <- tibble(s1 = c("Agree", "Neutral", "Strongly Disagree"),
               s2rl = c("Agree", "Neutral", "Strongly Disagree"),
               f1 = c("Strongly Agree", "Disagree", "Strongly Disagree"),
               f2rl = c("Strongly Agree", "Disagree", "Strongly Disagree"))

titles <- c("S1 results", "Results for S2RL", "Fiscal Results for F1", "Financial Status of F2RL")

这是我正在构建的自定义函数,它需要 2 个输入、变量和标题(数据集是硬编码的),以及我的地图函数不起作用:

library(purrr)
library(dplyr)
library(ggplot2)

my_plots = function(variable) {
  test %>%
    count({{variable}}) %>%
    mutate(percent = 100*(n / sum(n, na.rm = T))) %>%
    ggplot(aes(x = {{variable}}, y = percent, fill = {{variable}})) + 
    geom_bar(stat = "identity") +
    ylab("Percentage") +
    ggtitle(paste(title, "N =", n)) +
    coord_flip() +
    theme_minimal() +
    scale_fill_manual(values = c("Strongly disagree" = "#CA001B", "Disagree" = "#1D28B0", "Neutral" = "#D71DA4", "Agree" = "#00A3AD", "Strongly agree" = "#FF8200")) +
    scale_x_discrete(labels = c("Strongly disagree" = "Strongly\nDisagree", "Disagree" = "Disagree", "Neutral" = "Neutral", "Agree" = "Agree", "Strongly agree" = "Strongly\nAgree"), drop = FALSE) + 
    theme(axis.title.y = element_blank(),
          axis.text = element_text(size = 9, color = "gray28", face = "bold", hjust = .5),
          axis.title.x = element_text(size = 12, color = "gray32", face = "bold"),
          legend.position = "none",
          text = element_text(family = "Arial"),
          plot.title = element_text(size = 14, color = "gray32", face = "bold", hjust = .5)) +
    ylim(0, 100)
}

test%>%
  map(~my_plots(variable = .x, titles))

【问题讨论】:

    标签: r function loops ggplot2 purrr


    【解决方案1】:

    最好转换为symbol 并评估 (!!) 而不是{{}} 或使用.data

    library(dplyr)
    library(purrr)
    library(ggplot2)
    library(ggpubr)
    my_plots = function(variable, title) {
      test %>%
        count(!! rlang::sym(variable)) %>%
        mutate(percent = 100*(n / sum(n, na.rm = TRUE))) %>%
        ggplot(aes(x = !! rlang::sym(variable), y = percent, fill = .data[[variable]])) + 
        geom_bar(stat = "identity") +
        ylab("Percentage") +
        ggtitle(title) +
        coord_flip() +
        theme_minimal() +
        scale_fill_manual(values = c("Strongly disagree" = "#CA001B", "Disagree" = "#1D28B0", "Neutral" = "#D71DA4", "Agree" = "#00A3AD", "Strongly agree" = "#FF8200")) +
        scale_x_discrete(labels = c("Strongly disagree" = "Strongly\nDisagree", "Disagree" = "Disagree", "Neutral" = "Neutral", "Agree" = "Agree", "Strongly agree" = "Strongly\nAgree"), drop = FALSE) + 
        theme(axis.title.y = element_blank(),
              axis.text = element_text(size = 9, color = "gray28", face = "bold", hjust = .5),
              axis.title.x = element_text(size = 12, color = "gray32", face = "bold"),
              legend.position = "none",
              text = element_text(family = "Arial"),
              plot.title = element_text(size = 14, color = "gray32", face = "bold", hjust = .5)) +
        ylim(0, 100)
    }
    

    然后遍历map2中的列名和标题并应用函数

    out <- map2(names(test), titles, ~ my_plots(variable = .x, title = .y))
    ggarrange(plotlist = out, ncol = 2, nrow = 2)
    

    -输出

    【讨论】:

    • 谢谢!一个问题,在我的原始标题中,我希望它具有名称和样本大小 n。当我尝试您的代码但在 ggtitle 中添加我的原始粘贴参数时,我收到此错误 - 有什么想法吗?粘贴错误(标题,“n”,n):无法将“闭包”类型强制转换为“字符”类型的向量
    • @J.Sabree 那部分我不确定你真正想要什么,因为n 是一个值向量,你可能需要折叠或取第一个
    • 我会尝试崩溃。在我的实际数据中,一些调查回复有 NA,所以我想显示非缺失回复的数量。在这个例子中,它们都是 3,但在真实的例子中,它们几乎肯定会有所不同。再次感谢!
    • @J.Sabree 此外,当您尝试使用ggtitle 时,n 可能无法使用。您可能需要toString(unique(.data[["n"]])) 左右。我没有测试过
    猜你喜欢
    • 2022-10-21
    • 1970-01-01
    • 2022-01-26
    • 2010-11-01
    • 1970-01-01
    • 2021-12-29
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多