0

我已经使用 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 为三角形),但只有一个组合图例,三角形叠加在圆圈上)。如何获得两个单独的图例,每个数据框一个?

谢谢!

4

1 回答 1

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 相同,所以我想使用相同的颜色但不同的符号来表示人群的差异。一种是圆形,另一种是三角形。

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

于 2014-12-12T05:55:12.843 回答