绘制四个图以及一个共同的图例

时间:2019-07-19 18:00:39

标签: r graph bar-chart

我已经编写了用于制作条形图的代码,以绘制四种栖息地类型中物种的数量。我总共有8种,但并不是每个栖息地都存在所有物种。我想要一张包含四种栖息地类型的多面板图,以及该物种的共同传说。当前,每个条形图都有自己的图例,其颜色对应于不同的物种。

[Graph of RV][1]
[Graph of CG][2]
[Graph of U][3]
[Graph of SRG][4]

下面是使用的代码

ggplot(SRG, aes(x = Species)) +
geom_bar(aes(color = Species),
stat = "count", position = position_dodge(0.8),
width = 0.9)+
labs(y= "Count", x= "Species")

ggplot(U, aes(x = Species)) +
geom_bar(aes(color = Species, , fill = Species),
stat = "count", position = position_dodge(0.8),
width = 0.9)+
labs(y= "Count", x= "Species")

ggplot(CG, aes(x = Species)) +
geom_bar(aes(color = Species, fill = Species),
stat = "count", position = position_dodge(0.8),
width = 0.9)+
labs(y= "Count", x= "Species")

ggplot(RV, aes(x = Species)) +
geom_bar(aes(color = Species, fill = Species),
stat = "count", position = position_dodge(0.8),
width = 0.9)+
labs(y= "Count", x= "Species")

2 个答案:

答案 0 :(得分:0)

您可以尝试par(mfrow = c(2,2)

#Code 
 par(mfrow = c(2,2)
 #Code of Graph 1
 #Code of Graph 2
 #Code of Graph 3
 #Code of Graph 4

答案 1 :(得分:0)

由于所有数据的格式相同,因此可以将数据框与rbind结合使用。

combined_df <- rbind(SRG, CG, U, RV)

ggplot(combined_df, aes(x = Species)) +
geom_bar(aes(color = Species),
stat = "count", position = position_dodge(0.8),
width = 0.9)+
labs(y= "Count", x= "Species")