两个ggplot2数字从上到下对齐

时间:2012-02-08 17:44:29

标签: r ggplot2

我意识到align.plots包中的ggExtra函数已被弃用并删除。但是,我使用自己的版本,因为它似乎提供了我需要的特定功能。我已经研究了解决问题的方面,但我认为它不适用于我的特定问题。似乎问题是当我在其中一个上使用coord_equal时,从上到下的图像不对齐。这似乎不会影响从左到右。这是我想要实现的简化版(或者至少是我能做到的简单版)。

创建一些虚拟数据框:

source('https://raw.github.com/jbryer/multilevelPSA/master/r/align.R')
require(psych)
df = data.frame(x=rnorm(100, mean=50, sd=10),
            y=rnorm(100, mean=48, sd=10),
            group=rep(letters[1:10], 10))
dfx = describe.by(df$x, df$group, mat=TRUE)[,c('group1', 'mean', 'n', 'min', 'max')]
names(dfx) = c('group', 'x', 'x.n', 'x.min', 'x.max')
dfy = describe.by(df$y, df$group, mat=TRUE)[,c('group1', 'mean', 'n', 'min', 'max')]
names(dfy) = c('group', 'y', 'y.n', 'y.min', 'y.max')
df2 = cbind(dfx, dfy[,2:ncol(dfy)])
range = c(0,100)

这将设置三个图:

p1a = ggplot(df2, aes(x=x, y=y, colour=group)) + geom_point() + 
    opts(legend.position='none') +
    scale_x_continuous(limits=range) + scale_y_continuous(limits=range)
p1 = p1a + coord_equal(ratio=1)
p2 = ggplot(df, aes(x=x, y=group, colour=group)) + geom_point() +   
    scale_x_continuous(limits=range) + opts(legend.position='none')
p3 = ggplot(df, aes(x=group, y=y, colour=group)) + geom_point() + 
    scale_y_continuous(limits=range) + opts(legend.position='none')

从上到下的对齐方式不适用于coord_equal

grid_layout <- grid.layout(nrow=2, ncol=2, widths=c(1,2), heights=c(2,1))
grid.newpage()
pushViewport( viewport( layout=grid_layout, width=1, height=1 ) )
align.plots(grid_layout, list(p1, 1, 2), list(p3, 1, 1), list(p2, 2, 2))

Broken Plot http://bryer.org/alignplots1.png

修复方法是将respect=TRUE添加到grid.layout来电:

grid_layout <- grid.layout(nrow=2, ncol=2, widths=c(1,2), heights=c(2,1), respect=TRUE)

但如果我不使用coord_equal,那么对齐工作正常:

grid_layout <- grid.layout(nrow=2, ncol=2, widths=c(1,2), heights=c(2,1))
grid.newpage()
pushViewport( viewport( layout=grid_layout, width=1, height=1 ) )
align.plots(grid_layout, list(p1a, 1, 2), list(p3, 1, 1), list(p2, 2, 2))

Working Plot http://bryer.org/alignplots2.png

3 个答案:

答案 0 :(得分:4)

ggplot2现在有ggplotGrob(),这可能对此有帮助。

首先,我们需要更新用于生成图的代码:

p1a = ggplot(df2, aes(x=x, y=y, colour=group)) + geom_point()  +
  scale_x_continuous(limits=range) + scale_y_continuous(limits=range)
p1 = p1a + coord_equal(ratio=1) + theme_minimal() + theme(legend.position='none')
p2 = ggplot(df, aes(x=x, y=group, colour=group)) + geom_point() +   
  scale_x_continuous(limits=range) + theme_minimal() + theme(legend.position='none')
p3 = ggplot(df, aes(x=group, y=y, colour=group)) + geom_point() + 
  scale_y_continuous(limits=range) + theme_minimal() + theme(legend.position='none') 
p4 <- ggplot(df, aes(x = group, y = y)) + 
  geom_blank() +
  theme(line = element_blank(),
        rect = element_blank(),
        text = element_blank(),
        title = element_blank())

p4 将为空白;我们只需要grob画画。

然后我们加载 grid 包并使用cbind()rbind()在行和列中排列的列表中绘制grobs。

library(grid)
grid.newpage()
grid.draw(
  cbind(
    rbind(ggplotGrob(p3), ggplotGrob(p4), size = "first"),
    rbind(ggplotGrob(p1), ggplotGrob(p2), size = "first"), 
    size = "first"))

Grid plot

我不确定这种方法是否会让你以不同的宽度绘制p3,将p2绘制在不同的高度,就像在原始示例中一样;我通常需要一个类似大小的图形网格,并且不需要计算出不同的尺寸。

此答案部分基于部分基于https://stackoverflow.com/a/17463184/393354

答案 1 :(得分:3)

以下是一个例子:

m <- matrix(c(3, 1, 0, 2), 2, byrow = T)
lay <- gglayout(m, widths = c(1, 3), heights = c(3, 1))
ggtable(p1, p2, p3, layout = lay)

您可以通过

使用它
install.packages('devtools')
library(devtools)
dev_mode()
install_github("ggplot2", "kohske", "cutting-edge")
library(ggplot2)

请注意,此分支是实验性的,因此可能存在错误。

enter image description here

答案 2 :(得分:0)

要使用align.plots方法解决问题,请在布局调用上指定respect=TRUE

grid_layout <- grid.layout(nrow=2, ncol=2, widths=c(1,2), heights=c(2,1), respect=TRUE)