在ggplot中对齐和排列图表 [英] Aligning and arranging charts in ggplot

查看:187
本文介绍了在ggplot中对齐和排列图表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



使用:


$ b $在ggplot中有两个图表,我想要对齐轴线上的类似范围。 b

  library(grid)
grid.newpage()
grid.draw(rbind(ggplotGrob(g1),ggplotGrob(g2),size =last ))

其中 size ='last'确保网格对齐为x,我得到这样的布局:


最上面的图表对于我想描述的简单行具有过多的垂直范围。



我想要更符合以下内容的图片:



或者,将g1图表对齐到g2图表的轴线下方。 (一般来说,假设我希望能够选择任何一种,或者甚至在g2上覆盖g1。

组件简单地设置为g2图表中y = 0的图层;但是,该图表太混乱,无法用该布局读取,所以我认为应该将原始的y = 0元素移动到单独的面板(即g1)中。<



50
df1 = data.frame(x = seq(1,size),y = sample(-100:100,size,rep = TRUE))
df2 = data.frame(x = seq 1,size),r = replicate(size,paste(sample(c(letters,LETTERS),3),collapse ='')))
g1 = ggplot(df1)+ geom_point(aes(x = x ,y = y))
g2 = ggplot(df2)+ geom_text(aes(x = x,y = 0,label = r),angle = 45,size = 2)
library(grid)
grid.newpage()
grid.draw(rbind(ggplotGrob(g1),ggplotGrob(g2),size =last))

我希望标签条(g2)相对于散点图整齐排列,g2条是t的一部分他原来的图表是 geom_text()元素在 y = 0 ,但是图表太乱了,放置在那里也是。


$ b您只需编辑gtable的高度$ b

  library(ggplot2)
library(grid)
g1 < - g2 < - ggplotGrob(qplot(1,1))

g < - rbind(g1,g2,size =last)
id < - g $ layout $ t [g $ layout $ name ==panel]
g $ heights [id] < - lapply(c(1,4),unit,null)
grid.newpage()
grid.draw(g)

编辑:使用更具体的指令,我会建议一个不同的策略:将第一个面板仅添加到第二个plot的gtable ,

  p1 < -  ggplot(df1)+ geom_point(aes(x = x,y = y))
p2 < - ggplot(df2)+ geom_text(aes(x = x,y = 0,label = r),angle = 45,size = 2)
snug< - theme_bw()+ theme .margin = unit(c(0.5,0.5,0,0),line))

library(gtable)
g1 = gtable_filter( ggplotGrob(p1 +紧贴),pattern =panel,trim = TRUE,fixed = TRUE)
g2 = ggplotGrob(p2 +贴身)

g < - g2
g< ; gtable_add_rows(g,unit(0.2,null),0)
g < - gtable_add_grob(g,g1,1,4)
grid.newpage()
grid.draw (g)


I have two plots in ggplot with similar ranges on the axis that I would like to align.

Using:

library(grid)
grid.newpage()
grid.draw(rbind(ggplotGrob(g1), ggplotGrob(g2), size = "last"))

where size='last' ensures the grids are aligned for x, I get a layout such as this:

The top chart has an excessive vertical range for the simple row I want to depict.

I would like to have an image more in keeping with the following:

Or alternatively, with the g1 chart aligned below the axis of the g2 chart. (For generality, let's say I want to be able to choose either, or even overlay g1 on top of g2.

(My original model was a single chart with the g1 component simply set as a layer in the g2 chart with y=0; however, the chart is too cluttered to read with that layout, so I thought I should move the original y=0 elements into a separate panel (i.e. g1).

Minimal Example:

size=50
df1=data.frame(x=seq(1,size),y=sample(-100:100,size,rep=TRUE))
df2=data.frame(x=seq(1,size),r=replicate(size,paste(sample(c(letters, LETTERS),3),collapse='')))
g1=ggplot(df1)+geom_point(aes(x=x,y=y))
g2=ggplot(df2)+geom_text(aes(x=x,y=0,label=r),angle=45,size=2)
library(grid) 
grid.newpage()
grid.draw(rbind(ggplotGrob(g1), ggplotGrob(g2), size = "last"))

I want the label strip (g2) to be positioned neatly with respect to the scatterplot. The g2 strip was part of the original chart as a geom_text() element at y=0 but the chart was too cluttered around there with the labels placed there too.

解决方案

you simply need to edit the heights of the gtable,

library(ggplot2)
library(grid)
g1 <- g2 <- ggplotGrob(qplot(1,1))

g <- rbind(g1,g2,size="last")
id <- g$layout$t[g$layout$name == "panel"]
g$heights[id] <- lapply(c(1,4), "unit", "null")
grid.newpage()
grid.draw(g)

Edit: with the more specific instructions, I'd suggest a different strategy: add the first panel only to the gtable of the second plot,

p1 <- ggplot(df1)+geom_point(aes(x=x,y=y))
p2 <- ggplot(df2)+geom_text(aes(x=x,y=0,label=r),angle=45,size=2)
snug <- theme_bw() + theme(plot.margin=unit(c(0.5,0.5,0,0),"line"))

library(gtable)
g1=gtable_filter(ggplotGrob(p1 + snug), pattern = "panel", trim = TRUE, fixed=TRUE)
g2=ggplotGrob(p2 + snug)

g <- g2
g <- gtable_add_rows(g, unit(0.2, "null"), 0)
g <- gtable_add_grob(g, g1, 1, 4)
grid.newpage()
grid.draw(g)

这篇关于在ggplot中对齐和排列图表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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