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

查看:40
本文介绍了将多个 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, 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()# 来源: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")网格.排列(p1 + opts(legend.position="none"),p2 + opts(legend.position="none"),图例=图例,主要=",左=")

输出

解决方案

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

p1 <- ggplot(data1) +aes(x=x, y=y, 颜色=x) +geom_line() +scale_y_continuous(breaks = seq(200, 1000, 200),标签 = 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 = 值, 颜色 = x)) +geom_line() +facet_wrap(~变量,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天全站免登陆