将多个 ggplot2 绘图与网格对齐

发布于 2024-11-30 11:15:21 字数 1038 浏览 1 评论 0原文

上下文

我想在同一页面上使用相同的图例绘制两个 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()


# 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 = "")

输出

示例图像

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

Example image

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心房敞 2024-12-07 11:15:21

执行相同操作但更通用的一种更简洁的方法是使用格式化程序 arg:

p1 <- ggplot(data1) +
    aes(x=x, y=y, colour=x) +
    geom_line() + 
    scale_y_continuous(formatter = function(x) format(x, width = 5))

对第二个图执行相同的操作,并确保设置宽度 >= 您在两个图上期望的最宽数字。

A cleaner way of doing the same thing but in a more generic way is by using the formatter arg:

p1 <- ggplot(data1) +
    aes(x=x, y=y, colour=x) +
    geom_line() + 
    scale_y_continuous(formatter = function(x) format(x, width = 5))

Do the same for your second plot and make sure to set the width >= the widest number you expect across both plots.

一直在等你来 2024-12-07 11:15:21

1.使用cowplot包:

library(cowplot)
plot_grid(p1, p2, ncol=1, align="v")

enter图片描述这里


2. 使用ggbio 包:

注意: 似乎有一个 bug,x 刻度没有对齐。 (测试于 17/03/2016,ggbio_1.18.5)

library(ggbio)
tracks(data1=p1,data2=p2)

在此处输入图像描述

1. Using cowplot package:

library(cowplot)
plot_grid(p1, p2, ncol=1, align="v")

enter image description here


2. Using tracks from ggbio package:

Note: There seems to be a bug, x ticks do not align. (tested on 17/03/2016, ggbio_1.18.5)

library(ggbio)
tracks(data1=p1,data2=p2)

enter image description here

烟沫凡尘 2024-12-07 11:15:21

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

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"))

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

1)为什么不在两者上使用相同的 y 轴?我觉得这是一种更直接的方法,在上面的示例中通过将 scale_y_continuous(limits = c(0, 10000)) 添加到 p1 可以轻松实现。

2)facet_wrap提供的功能在这里还不够吗?很难知道你的数据结构实际上是什么样的,但这里有一个我如何做到这一点的玩具示例:

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文