将两个图合并为一个,每个图使用一个单独的图例

时间:2014-12-11 10:51:08

标签: r plot merge legend gplots

我已经使用ggplot2创建了两个单独的散点图,我需要将它们组合成一个单独的图。每个图是针对三种不同处理(背景)下的蜥蜴种群。 对于每个情节我都有以下内容:

csMS = data.frame()
ellMS = data.frame()
centroidsMS = data.frame()  

csplotMS = ggplot(csMS, aes(x = RG, y =  GB, colour = Background)) + geom_point(size = 3, shape = 17) + #colour by background, circles size 3
  geom_path(data = ell.AS, aes(x = RG, y = GB ,colour = Background), size = 1, linetype = 2) + #adding the ellipses
  geom_point(data = centroidsMS, size = 3, shape = 17) + #added centroids     
  geom_errorbar(data = centroidsMS, aes(ymin = GB - se.GB, ymax = GB + se.GB), width = 0) + #add y error bars
  geom_errorbarh(data = centroidsMS, aes(xmin = RG - se.RG, xmax = RG + se.RG), height = 0) +
  theme_bw() + #white background
  theme(axis.title.y = element_text(vjust = 2), axis.title.x = element_text(vjust = -0.3)) + #distance of axis titles from axis
  theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), #no grids
        axis.line = element_line(colour = "black")) + #black axes
  theme(text = element_text(size = 30)) +  #font size
  ylab("(G-B)/(G+B)") + xlab("(R-G)/(R+G)") + # Set text for axes labels
  scale_colour_manual(values = c("black","#FF6600", "yellow1")) + #changed default colours
  labs(colour = "Murray Sunset NP") +
  theme(legend.title = element_text(size = "20")) + #changes the legend title
  theme(legend.text = element_text(size = "20")) + #changes the legend title
  theme(legend.key = element_blank()) + #removed little squares around legend symbols
  theme(legend.direction = "horizontal", legend.position = c(.5, .85))

我试过

csASMS = csplotAS + csplotMS

但是我收到一条错误消息:“p + o中的错误:二元运算符的非数字参数另外:警告消息:”+的不兼容方法(“+ .gg”,“Ops.data.frame”) “”

我也试过

csASMS = grid.arrange(csplotAS, csplotMS)

但这会将一个地块放在另一个地方之上,但我需要将两个地块结合起来,这样它们基本上只是一个地块,但有两个不同的图例,因为每个地块都有不同的惯例来表示不同的蜥蜴种群。

非常感谢任何帮助。

****编辑**** 2014年12月12日

我已经设法将这两个情节合二为一,但仍然存在单独的传说问题。为了尝试简化问题,并根据cdeterman的请求,我添加了一个更简单的代码形式和一些示例数据:

data frames: p1 and p2

> p1
  treatment x y
1     Black 1 1
2    Orange 2 2
3    Yellow 3 3

> p2
  treatment x y
1    Black  4 4
2    Orange 5 5
3    Yellow 6 6

我使用以下代码制作包含两个数据框的图:

plot = ggplot(p1, aes(x = x, y =  y, colour = treatment)) +     geom_point(size = 3) + #colour by background, circles size 3
  theme_bw() + #white background
  theme(axis.title.y = element_text(vjust = 2), axis.title.x = element_text(vjust = -0.3)) + #distance of axis titles from axis
  theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), #no grids
  axis.line = element_line(colour = "black")) + #black axes
  theme(text = element_text(size = 30)) +  #font size
  scale_colour_manual(values = c("black","#FF6600", "yellow1")) + #changed default colours
  labs(colour = "p1") +
  theme(legend.title = element_text(size = "20")) + #changes the legend title
  theme(legend.text = element_text(size = "20")) + #changes the     legend title
  theme(legend.key = element_blank()) + #removed little squares around legend symbols
  theme(legend.direction = "horizontal", legend.position = c(.33, 1)) +

  # Now to add the second plot/ No need to code for axis titles, titles positions,etc b/c it's already coded in the first plot
  geom_point(data = p2, aes(x = x, y = y, colour = treatment), size = 3, shape = 17)

这会生成一个图表,每个数据框用不同的符号表示(p1的圆圈和p2的三角形),但只有一个组合的图例,三角形叠加在圆圈上)。如何获得两个单独的图例,每个数据框一个?

谢谢!

1 个答案:

答案 0 :(得分:1)

在做了一些研究并尝试不同的事情后,我能够解决我的问题的一部分。要将两个图一起添加,首先需要绘图仪,另一个需要使用

绘制第一个图
geom.point()

我的新代码如下所示:

csplotASMS = ggplot(csAS, aes(x = RG, y =  GB, colour = Background)) + geom_point(size = 3) + #colour by background, circles size 3
  geom_path(data = ell.AS, aes(x = RG, y = GB ,colour = Background), size = 1, linetype = 1) + #adding the ellipses
  geom_point(data = centroidsAS, size = 4) + #added centroids     
  geom_errorbar(data = centroidsAS, aes(ymin = GB - se.GB, ymax = GB + se.GB), width = 0) + #add y error bars
  geom_errorbarh(data = centroidsAS, aes(xmin = RG - se.RG, xmax = RG + se.RG), height = 0) +
  theme_bw() + #white background
  theme(axis.title.y = element_text(vjust = 2), axis.title.x = element_text(vjust = -0.3)) + #distance of axis titles from axis
  theme(panel.border = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), #no grids
  axis.line = element_line(colour = "black")) + #black axes
  theme(text = element_text(size = 30)) +  #font size
  ylab("(G-B)/(G+B)") + xlab("(R-G)/(R+G)") + # Set text for axes labels
  scale_colour_manual(values = c("black","#FF6600", "yellow1")) + #changed default colours
  labs(colour = "Alice Springs") +
  theme(legend.title = element_text(size = "20")) + #changes the legend title
  theme(legend.text = element_text(size = "20")) + #changes the legend title
  theme(legend.key = element_blank()) + #removed little squares around legend symbols
  theme(legend.direction = "horizontal", legend.position = c(.33, 1)) +

  # Now to add the second plot/ No need to code for axis titles, titles positions,etc b/c it's already coded in the first plot
  geom_point(data = csMS, aes(x = RG, y = GB, colour = Background), size = 3, shape = 17) +
  geom_path(data = ell.MS, aes(x = RG, y = GB ,colour = Background), size = 1, linetype = 2) + #adding the ellipses
  geom_point(data = centroidsMS, size = 4, shape = 17) + #added centroids     
  geom_errorbar(data = centroidsMS, aes(ymin = GB - se.GB, ymax = GB + se.GB), width = 0) + #add y error bars
  geom_errorbarh(data = centroidsMS, aes(xmin = RG - se.RG, xmax = RG + se.RG), height = 0) #add x error bars

并且该图描绘了两个群体的散点图,每个群体具有三个处理。因为两个种群的tratments是相同的,我想使用相同的颜色但不同的符号来表示种群的差异。一个群体是圆圈,另一个群体是三角形。

现在,我还不能回答的部分是如何有两个单独的传说,每个“情节”一个。即一个用于圆圈,一个用于三角形。此刻有一个“组合图例,显示叠加在圆圈上的三角形。每个图例都应有自己的标题。