【问题标题】:Merging two plots into one, each with a separate legend using R将两个图合并为一个,每个图都有一个使用 R 的单独图例
【发布时间】:2015-02-09 19:56:33
【问题描述】:

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

谢谢!

【问题讨论】:

  • 通常你将数据集与ggplot结合起来进行一次通话。你应该看看this question。如果答案没有提供您想要的,请更新您的问题。
  • 嗨,我确实看过你建议的问题,但这与我首先尝试的解决方案基本相同但没有奏效,返回了我在原始问题中描述的错误消息。此外,即使这可行,这也会给我一个图例,正如我在问题中所描述的,我需要两个单独的图例。
  • 有没有什么方法可以提供一些有代表性的数据?

标签: r plot merge legend gplots


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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-27
    • 1970-01-01
    相关资源
    最近更新 更多