簡體   English   中英

使用facetted ggplot 2.0.0和gridExtra排列公共繪圖寬度

[英]Arrange common plot width with facetted ggplot 2.0.0 & gridExtra

由於我已經更新到ggplot2 2.0.0,我無法使用gridExtra正確排列圖表。 問題是分面圖表會被壓縮,而其他圖表會擴展。 寬度基本搞砸了。 我想安排它們類似於這些單面圖的方式: 左對齊兩個圖形邊(ggplot)

我把一個可重現的代碼

library(grid) # for unit.pmax()
library(gridExtra)

plot.iris <- ggplot(iris, aes(Sepal.Length, Sepal.Width)) + 
  geom_point() + 
  facet_grid(. ~ Species) + 
  stat_smooth(method = "lm")

plot.mpg <- ggplot(mpg, aes(x = cty, y = hwy, colour = factor(cyl))) + 
  geom_point(size=2.5)

g.iris <- ggplotGrob(plot.iris) # convert to gtable
g.mpg <- ggplotGrob(plot.mpg) # convert to gtable

iris.widths <- g.iris$widths # extract the first three widths, 
mpg.widths <- g.mpg$widths # same for mpg plot
max.widths <- unit.pmax(iris.widths, mpg.widths)

g.iris$widths <- max.widths # assign max. widths to iris gtable
g.mpg$widths <- max.widths # assign max widths to mpg gtable

grid.arrange(g.iris,g.mpg,ncol=1)

在此輸入圖像描述

正如您將看到的,頂部圖表,第一個方面被擴展,而另外兩個方面被壓縮。 底部圖表未涵蓋所有寬度。

是不是新的ggplot2版本搞亂了gtable的寬度?

有人知道解決方法嗎?

非常感謝你

編輯:添加圖表的圖片

我正在尋找類似的東西:

在此輸入圖像描述

一種選擇是將每個地塊按摩成3x3 gtable,其中中央細胞包裹所有的情節面板。

使用@SandyMuspratt中的示例

# devtools::install_github("baptiste/egg") 
grid.draw(egg::ggarrange(plots=plots, ncol=1))

在此輸入圖像描述

優點是,一旦采用這種標准化格式,無論面板,圖例,軸,條帶等的數量如何,繪圖都可以更容易地組合成各種布局。

grid.newpage()
grid.draw(ggarrange(plots=list(p1,  p4, p2, p3), widths = c(2,1), debug=TRUE))

在此輸入圖像描述

我不確定你是否還在尋找解決方案,但這是相當普遍的。 我正在使用ggplot 2.1.0(現在在CRAN上)。 它基於這個解決方案 我將問題分成兩部分。 首先,我處理圖的左側,確保軸材料的寬度相同。 這已經由其他人完成,並且有SO的解決方案。 但我不認為結果看起來不錯。 我更喜歡面板也在右側對齊。 第二,該過程確保面板右側的列寬度相同。 它通過在每個圖的右側添加適當寬度的列來實現。 (可能有更簡潔的方法去做。有 - 見@baptiste解決方案。)

library(grid)    # for pmax
library(gridExtra) # to arrange the plots
library(ggplot2)   # to construct the plots
library(gtable)   # to add columns to gtables of plots without legends

mpg$g = "Strip text"  

# Four fairly irregular plots: legends, faceting, strips
p1 <- ggplot(mpg, aes(displ, 1000*cty)) + 
  geom_point() + 
  facet_grid(. ~ drv) + 
  stat_smooth(method = "lm")

p2 <- ggplot(mpg, aes(x = hwy, y = cyl, colour = factor(cyl))) + 
  geom_point() + 
  theme(legend.position=c(.8,.6),
        legend.key.size = unit(.3, "cm"))

p3 <- ggplot(mpg, aes(displ, cty, colour = factor(drv))) + 
  geom_point() + 
  facet_grid(. ~ drv) 


p4 <- ggplot(mpg, aes(displ, cty, colour = factor(drv))) + 
  geom_point() + 
  facet_grid(g ~ .) 

# Sometimes easier to work with lists, and it generalises nicely
plots = list(p1, p2, p3, p4)

# Convert to gtables
g = lapply(plots, ggplotGrob) 

# Apply the un-exported unit.list function for grid package to each plot
g.widths = lapply(g, function(x) grid:::unit.list(x$widths)) 


## Part 1: Make sure the widths of left axis materials are the same across the plots
# Get first three widths from each plot
g3.widths <- lapply(g.widths, function(x) x[1:3])

# Get maximum widths for first three widths across the plots
g3max.widths <- do.call(unit.pmax, g3.widths)

# Apply the maximum widths to each plot
for(i in 1:length(plots)) g[[i]]$widths[1:3] = g3max.widths

# Draw it
do.call(grid.arrange, c(g, ncol = 1))


## Part 2: Get the right side of the panels aligned
# Locate the panels
panels <- lapply(g, function(x) x$layout[grepl("panel", x$layout$name), ])

# Get the position of right most panel
r.panel  = lapply(panels, function(x) max(x$r)) # position of right most panel

# Get the number of columns to the right of the panels
n.cols = lapply(g.widths, function(x) length(x)) # right most column

# Get the widths of these columns to the right of the panels
r.widths  <- mapply(function(x,y,z) x[(y+1):z], g.widths, r.panel, n.cols)

# Get the sum of these widths
sum.r.widths <- lapply(r.widths, sum)

# Get the maximum of these widths
r.width = do.call(unit.pmax, sum.r.widths)

# Add a column to the right of each gtable of width 
# equal to the difference between the maximum
# and the width of each gtable's columns to the right of the panel. 
for(i in 1:length(plots)) g[[i]] = gtable_add_cols(g[[i]], r.width - sum.r.widths[[i]], -1)

# Draw it
do.call(grid.arrange, c(g, ncol = 1))

在此輸入圖像描述

取下這兩條線並保留其余部分,它工作得很好。

g.iris$widths <- max.widths # assign max. widths to iris gtable
g.mpg$widths <- max.widths # assign max widths to mpg gtable

在此輸入圖像描述

可能它限制了它們的寬度。

這很難看,但是如果你在時間壓力下這個黑客會起作用(不可推廣並且取決於繪圖窗口大小)。 基本上在頂部繪制2列,右邊是空白圖,並猜測寬度。

grid.arrange(
    grid.arrange(plot.iris, ggplot() + theme_minimal(),ncol=2, widths = c(.9, .1)),
    plot.mpg, 
    ncol=1
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM