【发布时间】:2021-05-04 10:11:37
【问题描述】:
我在 R 中制作了 10 个 ggplots(条形图),并使用 ggarrange 函数将它们放在公共页面上。我想在页面底部的中间有一个共同的图例。问题是我的图排列在 2 列(和 5 行)中,其中 4 种颜色的图形位于第 1 列,5 种颜色的图形(其中 4 种颜色与第 1 列中图形的颜色相同)位于第二列。
设置ggarrange 参数common.legend=T 不能正常工作,因为它使用了第一张图的图例。所以,图例中只有 4 种颜色。
我想用所有这 5 种颜色制作一个独特的图例(并且我确实需要按照该顺序排列图)。
有什么建议吗?感谢您的所有帮助。
这是一个示例代码(只有 4 个图更短):
# libraries
library(ggplot2)
library(ggpubr)
# example data
value = runif(n=36, min=0, max=2)
specie = c(rep(c(rep("Before", 4) , rep("After", 4)), 2),
rep(c(rep("Before", 5) , rep("After", 5)), 2))
condition = c(rep(rep(c("cont" , "Be" , "Bc", "OZS"), 2), 2),
rep(rep(c("cont" , "Be" , "Bc", "OZS", "Cm"), 2), 2))
# 1st graph
data1 = data.frame(specie=specie[1:8], condition= condition[1:8],
value=value[1:8])
data1$specie = factor(data1$specie, levels = c("Before", "After"))
data1$condition = factor(data1$condition,
levels = c("cont" , "Be" , "Bc", "OZS"))
g1 = ggplot(data1, aes(fill=condition, y=value, x=specie)) +
geom_bar(position="dodge", stat="identity") +
guides(fill = guide_legend(title="Treatment: ")) +
theme(legend.position = "none",
plot.title = element_text(hjust = 0.5, vjust = 0,
size = 10, face = "bold")) +
ggtitle("(a)") +
scale_fill_manual(values = c("Be" = "steelblue2",
"Bc" = "gray47",
"cont" = "chartreuse3",
"OZS" = "indianred1"))
# 2nd graph
data2 = data.frame(specie=specie[9:16], condition= condition[9:16],
value=value[9:16])
data2$specie = factor(data2$specie, levels = c("Before", "After"))
data2$condition = factor(data2$condition,
levels = c("cont" , "Be" , "Bc", "OZS"))
g2 = ggplot(data2, aes(fill=condition, y=value, x=specie)) +
geom_bar(position="dodge", stat="identity") +
guides(fill = guide_legend(title="Treatment: ")) +
theme(legend.position = "none",
plot.title = element_text(hjust = 0.5, vjust = 0,
size = 10, face = "bold")) +
ggtitle("(b)") +
scale_fill_manual(values = c("Be" = "steelblue2",
"Bc" = "gray47",
"cont" = "chartreuse3",
"OZS" = "indianred1"))
# 3rd graph
data3 = data.frame(specie=specie[17:26], condition= condition[17:26],
value=value[17:26])
data3$specie = factor(data3$specie, levels = c("Before", "After"))
data3$condition = factor(data3$condition,
levels = c("cont" , "Be" , "Bc", "OZS", "Cm"))
g3 = ggplot(data3, aes(fill=condition, y=value, x=specie)) +
geom_bar(position="dodge", stat="identity") +
guides(fill = guide_legend(title="Treatment: ")) +
theme(legend.position = "none",
plot.title = element_text(hjust = 0.5, vjust = 0,
size = 10, face = "bold")) +
ggtitle("(c)") +
scale_fill_manual(values = c("Be" = "steelblue2",
"Bc" = "gray47",
"cont" = "chartreuse3",
"OZS" = "indianred1",
"Cm"= "darkgoldenrod1"))
# 4th graph
data4 = data.frame(specie=specie[27:36], condition= condition[27:36],
value=value[27:36])
data4$specie = factor(data4$specie, levels = c("Before", "After"))
data4$condition = factor(data4$condition,
levels = c("cont" , "Be" , "Bc", "OZS", "Cm"))
g4 = ggplot(data4, aes(fill=condition, y=value, x=specie)) +
geom_bar(position="dodge", stat="identity") +
guides(fill = guide_legend(title="Treatment: ")) +
theme(legend.position = "none",
plot.title = element_text(hjust = 0.5, vjust = 0,
size = 10, face = "bold")) +
ggtitle("(d)") +
scale_fill_manual(values = c("Be" = "steelblue2",
"Bc" = "gray47",
"cont" = "chartreuse3",
"OZS" = "indianred1",
"Cm"= "darkgoldenrod1"))
# all graphs together
ggarrange(g1, g3, g2, g4,
ncol=2, nrow=2, common.legend = TRUE, legend="bottom")
# ... the 5th color is missing in the legend
【问题讨论】: