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



我使用 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)

但这将一个图放在另一个图的顶部,但我需要将两个图结合起来,使它们基本上只是一个地块,但有两个单独的图例,因为每个地块都有不同的约定来指示不同的蜥蜴种群。

任何帮助将不胜感激。

****

编辑**** 十二月 12/2014

我已经设法将两个情节合二为一,但仍然存在单独传说的问题。为了简化问题,并根据 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 的三角形(,但只有一个组合图例,三角形叠加在圆上(。 如何获取两个单独的图例,每个数据框一个图例?

谢谢!

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

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

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

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

相关内容

最新更新