如何使用map和ggploy为每个图表添加自定义标题? [英] How to use map and ggplot to add custom titles to each chart?
本文介绍了如何使用map和ggploy为每个图表添加自定义标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据集,我希望使用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")
下面是我正在构建的自定义函数,它接受两个输入,变量和标题(数据集是硬编码的),以及不起作用的map函数:
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
Disagree", "Disagree" = "Disagree", "Neutral" = "Neutral", "Agree" = "Agree", "Strongly agree" = "Strongly
Agree"), 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))
推荐答案
转换为sym
并计算(!!
)可能比{{}}
或使用.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
Disagree", "Disagree" = "Disagree", "Neutral" = "Neutral", "Agree" = "Agree", "Strongly agree" = "Strongly
Agree"), 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)
-输出
这篇关于如何使用map和ggploy为每个图表添加自定义标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文