将多个ggplot2图与网格对齐 [英] Align multiple ggplot2 plots with grid

查看:173
本文介绍了将多个ggplot2图与网格对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文



我想用同一个图例在同一页面上绘制两个ggplot2。 http://code.google.com/p/gridextra/wiki/arrangeGrob 描述,如何做到这一点。这已经很好看了。但是......在我的例子中,我有两个具有相同x轴和不同y轴的图。当y轴的范围至少比另一个绘图高10倍(例如10000而不是1000)时,ggplot2(或者网格?)不能使绘图正确对齐(见下面的输出)。



问题



如何使用两个不同的y轴对齐图的左侧?



示例代码



  x = c(1,2)
y = c(10,1000)
data1 = data.frame(x,y)
p1 < - ggplot(data1)+ aes(x = x,y = y,color = x)+ geom_line )

y = c(10,10000)
data2 = data.frame(x,y)
p2 < - ggplot(data2)+ aes(x = x,y = y,color = x)+ geom_line()


#来源:http://code.google.com/p/gridextra/wiki/arrangeGrob
腿< - ggplotGrob(p1 + opts(keep =legend_box))
legend = gTree(children = gList(leg),cl =legendGrob)
widthDetails.legendGrob< - function(x)unit (3,cm)
grid.arrange(
p1 + opts(legend.position =none),
p2 + opts(legend.position =none),
legend = legend, main =,left =)



输出



解决方案

如果你不介意无耻的混乱,只需在 p1 中最长的标签添加一个额外的字符,如下所示:

  p1 < -  ggplot(data1)+ 
aes(x = x,y = y,color = x)+
geom_line()+
scale_y_continuous(breaks = seq(200,1000,200),
labels = c(seq(200,800,200),1000))

我有两个基本问题,我希望如果您有理由,请原谅:



<1>为什么不在两者上使用相同的y轴?我觉得这是一种更直接的方法,并且通过将 scale_y_continuous(limits = c(0,10000))添加到 p1



<2>由 facet_wrap 提供的功能在这里不够用吗?很难知道你的数据结构实际上是什么样子,但这里有一个玩具的例子:我们可以这样做: (ggplot2)

#也许你的数据集是这样的
x< - data.frame(x = c(1,2),
y1 = c(0,1000) ,
y2 = c(0,10000))

#熔化数据使ggplot中的许多事情变得更容易
x.melt< - melt(x,id.var =x,measure.var = c(y1,y2))

#绘制 - 一页,两个方面,相同的轴(尽管你可以改变它们),
#一个图例
ggplot(x.melt,aes(x = x,y = value,color = x))+
geom_line()+
facet_wrap(〜variable,nrow = 2)


Context

I want to plot two ggplot2 on the same page with the same legend. http://code.google.com/p/gridextra/wiki/arrangeGrob discribes, how to do this. This already looks good. But... In my example I have two plots with the same x-axis and different y-axis. When the range of the the y-axis is at least 10 times higher than of the other plot (e.g. 10000 instead of 1000), ggplot2 (or grid?) does not align the plots correct (see Output below).

Question

How do I also align the left side of the plot, using two different y-axis?

Example Code

x = c(1, 2)
y = c(10, 1000)
data1 = data.frame(x,y)
p1 <- ggplot(data1) + aes(x=x, y=y, colour=x) + geom_line()

y = c(10, 10000)
data2 = data.frame(x,y)
p2 <- ggplot(data2) + aes(x=x, y=y, colour=x) + geom_line()


# Source: http://code.google.com/p/gridextra/wiki/arrangeGrob
leg <- ggplotGrob(p1 + opts(keep="legend_box"))
legend=gTree(children=gList(leg), cl="legendGrob")
widthDetails.legendGrob <- function(x) unit(3, "cm")
grid.arrange(
  p1 + opts(legend.position="none"),
  p2 + opts(legend.position="none"),
  legend=legend, main ="", left = "")

Output

解决方案

If you don't mind a shameless kludge, just add an extra character to the longest label in p1, like this:

p1 <- ggplot(data1) +
    aes(x=x, y=y, colour=x) +
    geom_line() + 
    scale_y_continuous(breaks = seq(200, 1000, 200),
                       labels = c(seq(200, 800, 200), " 1000"))

I have two underlying questions, which I hope you'll forgive if you have your reasons:

1) Why not use the same y axis on both? I feel like that's a more straight-forward approach, and easily achieved in your above example by adding scale_y_continuous(limits = c(0, 10000)) to p1.

2) Is the functionality provided by facet_wrap not adequate here? It's hard to know what your data structure is actually like, but here's a toy example of how I'd do this:

library(ggplot2)

# Maybe your dataset is like this
x <- data.frame(x = c(1, 2),
                y1 = c(0, 1000),
                y2 = c(0, 10000))

# Molten data makes a lot of things easier in ggplot
x.melt <- melt(x, id.var = "x", measure.var = c("y1", "y2"))

# Plot it - one page, two facets, identical axes (though you could change them),
# one legend
ggplot(x.melt, aes(x = x, y = value, color = x)) +
    geom_line() +
    facet_wrap( ~ variable, nrow = 2)

这篇关于将多个ggplot2图与网格对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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