ggplot2:单行中有不同变量的多个图,单个分组图例 [英] ggplot2: multiple plots with different variables in a single row, single grouping legend

查看:438
本文介绍了ggplot2:单行中有不同变量的多个图,单个分组图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然有一些主题朝着相同的方向发展,但我还没有发现任何能够专门处理我的问题。因此,一个新的话题,并提前感谢所有的帮助。

情况

我有两个情节,需要单身例如:

  library(ggplot2)
dsamp < - diamonds [sample(nrow(diamonds), 1000),]
p1 <-qplot(价格,克拉,数据= dsamp,颜色=清晰度)
p2 < - qplot(价格,深度,数据= dsamp,颜色=净度)

虽然因变量在每个图中不同,但分组和独立性保持不变。因此,我只需要图中的一个传说来描述这些群体。



我尝试了什么,但没有奏效

按照 R食谱中所述使用解决方案。在该页面上给出的自定义 multiplot()函数使没有图例的图很好地呈现。但是,如果只需要一个图例,该功能就会失败。由于其中一个图形将包含图例,而另一个图形不包含,所以两个图形的宽度会相互不同(从链接提及请):

 多图(p1 + theme(legend.position =none),p2,cols = 2)

另一我找到的潜在解决方案是包装 gridExtra ,这个代码示例。它几乎做我需要的,除了图表垂直排列。我试着玩功能参数,但不知道如何水平排列图。希望有人对这个包/问题有更多的经验。谢谢!

解决方案

以下是使用包 gridExtra grid.arrange()。首先,制作三张图 - 其中一张带有传说(p1.leg),另外两张没有图例。

  p1.leg < -  ggplot (dsamp,aes(价格,克拉,颜色=清晰度))+ geom_point()+ 
主题(价格,克拉,颜色=净度))+ geom_point()
p1 <-ggplot (legend.position =none)
p2 <-ggplot(dsamp,aes(price,depth,color = clarity))+ geom_point()+
theme(legend.position =none )

现在您可以从函数 g_legend( )我从@Luciano Selzer借用回答 this question

  g_legend<  -  function(a.gplot){$ b $ (tpp $ grobs,function(x)x $ name)==guide-box)
(gpplot_build(a.gplot))
leg<传说< - tmp $ grobs [[leg]]
return(legend)}

leg< -g_legend(p1.leg)

现在您可以将图和图例与函数 arrangeGrob() grid.arrange()。在 arrangeGrob()中,您可以设置列的宽度,以便在图和图例之间获得所需的比例。

  library(gridExtra)
grid.arrange(arrangeGrob(arrangeGrob(p1,p2),leg,ncol = 2,widths = c(5 / 6,1 / 6)))



UPDATE



将所有图表放在同一行中:

  grid.arrange(arrangeGrob(p1,p2,leg,ncol = 3,widths = c(3 / 7,3 / 7,1 / 7)))


While there are some topics going in the same general direction, I haven't found any that would deal with my issue specifically. Hence a new topic, and thanks in advance for all the help.

Situation

I have two plots, that need to go in a single figure horizontally, e.g.:

library(ggplot2)
dsamp <- diamonds[sample(nrow(diamonds), 1000), ]    
p1 <- qplot(price, carat, data=dsamp, colour=clarity)
p2 <- qplot(price, depth, data=dsamp, colour=clarity)

While the dependent variable differs per plot, the grouping and the independent remains the same. Hence I only need but a single legend in the figure to describe the groups.

What I have tried and what did not work

I have tried to use the solution as described in the R Cookbook. The custom multiplot() function given on that page renders the plots without legends just fine. However, if one needs only a single legend, that function fails. Since one of the graphs would contain the legend, whereas the other not, the width of both graphs would differ in relation to each other (copy the multiplot function from the link mentioned please):

multiplot(p1 + theme(legend.position = "none"),p2,cols=2)

Another potential solution that I have found is the package gridExtra, with this code example. It almost does what I need, except that the graphs are arranged vertically. I tried playing with the function arguments, but could not figure how to arrange the plots horizontally. Hope someone has more experience with that package/issue. Thanks!

解决方案

Here is solution using package gridExtra and grid.arrange(). First, make three plots - one with legend (p1.leg) and two without legends.

p1.leg <- ggplot(dsamp,aes(price,carat,colour=clarity))+geom_point()
p1<-ggplot(dsamp,aes(price,carat,colour=clarity))+geom_point()+
      theme(legend.position="none")
p2 <-ggplot(dsamp,aes(price,depth,colour=clarity))+geom_point()+
     theme(legend.position="none")

Now you can get just legend from the first plot with function g_legend() that I borrowed from @Luciano Selzer answer to this question.

g_legend <- function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)}

leg<-g_legend(p1.leg)

Now you can combine both plots and legend with functions arrangeGrob() and grid.arrange(). In arrangeGrob() you can set widths for columns to get desired proportion between plots and legend.

library(gridExtra)
grid.arrange(arrangeGrob(arrangeGrob(p1,p2),leg,ncol=2,widths=c(5/6,1/6)))

UPDATE

To put all plots in the same row:

grid.arrange(arrangeGrob(p1,p2,leg,ncol=3,widths=c(3/7,3/7,1/7)))

这篇关于ggplot2:单行中有不同变量的多个图,单个分组图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆