ggplot:如何对齐多个大小不同的分面图?

5
我正在尝试将多个具有不同外观的图表进行对齐。我的问题有点小,但很烦人:我可以使得所有的绘图区域和各自的外观对齐,但是外观条却宽度不同。如果外观标签的长度不同,那么外观条的大小会根据文本适应外观大小。到目前为止我还没有找到一种方法来在对齐多个图表时使所有的外观条宽度相同。
这里有一个示例,这就是我想要排列的类型图表及我的努力去对齐它们:
library(data.table)
library(ggplot2)
library(foreach)
library(stringr)
library(cowplot)

# example data to show how aligning faceted plots is not quite right
plotvars = c(paste0("plot1_var", 1:7), paste0("plot2_var",1:5), paste0("plot3_var",1:10))
data = 
  foreach(p=plotvars,.combine = "rbind") %do% {
    d = data.table(plot = rep(str_extract(p,pattern = "plot[[:digit:]]"),2),
               plot_variables = rep(p,2),
               fill_categories = c("fill1","fill2"),
               number = sample(1:1000, size = 2))
    d[, facet_variables := ifelse(plot=="plot1", 
                                  rep(sample(paste0("facet",1:3),size=1),2),
                                  ifelse(plot=="plot2",
                                         rep(sample(paste0("facet_title",1:3),size=1),2),
                                         ifelse(plot=="plot3",
                                                rep(sample(paste0("facet_title_longer",1:3),size=1),2),
                                                NA)))]
    d
  }

# function to make stacked barplots with facets + coord_flip
make_plot = function(data, plot_var) {
  ggplot(data[plot==plot_var],
         aes(x=plot_variables,
             y=number,
             fill=fill_categories))+
    geom_bar(stat="identity")+
    coord_flip()+
    facet_grid(facet_variables ~ ., 
               space="free",
               scales="free")+
    theme(strip.text.y = element_text(angle=0),
          legend.position = "none")
}
p1 = make_plot(data=data,plot_var="plot1")
p1

p1

p2 = make_plot(data=data,plot_var="plot2")
p2

enter image description here

p3 = make_plot(data=data,plot_var = "plot3")
p3

enter image description here

# using 'cowplot::plot_grid' gives strange re-sizing of individual bars
cowplot::plot_grid(p1,p2,p3, ncol=1,nrow=3,align = "hv")

enter image description here

# try gtable_rbind version
g1=ggplotGrob(p1)
g2=ggplotGrob(p2)
g3=ggplotGrob(p3)

# this plot keeps the bar widths the correct size, but the facets are still incorrectly different widths.
ggdraw(gridExtra::gtable_rbind(g1,g2,g3))

enter image description here

如何使维度条在不同图表中宽度相同?

3个回答

2

您可以通过一个标签函数来实现这样的效果,该函数会插入第二行空白,其长度可以自由设置。使用 mtcars 数据集...

#define a function to add a second line of spaces after a given label 
#and a blank line before to maintain the centre vertical alignment
#you might need to play with the appropriate value to get the width right
widen <- function(x) paste(" \n", x, "\n", paste0(rep(" ", 20), collapse=""))

mtcars %>% ggplot(aes(x = mpg)) +
  geom_histogram() +
  facet_grid(cyl ~ ., labeller = labeller(cyl = widen)) +
  coord_flip() +
  theme(strip.text.y = element_text(angle = 0))

enter image description here


是的,这个解决方案应该可以工作,虽然不是我正在寻找的解决方案--没有办法使用gtable并更改条带的宽度吗?我无法像通常那样弄清楚它。如果没有基于修改grobs的解决方案提出,我将把它标记为答案。谢谢! - Reilstein
@Tung,我实际上尝试了与你链接的帖子中类似的解决方案-手动设置gtable宽度-但它并没有改变facet strip的宽度,只改变了facet strip所在列的宽度。我无法弄清楚为什么改变gtable宽度实际上并没有改变strip背景颜色区域的宽度。 - Reilstein

1

这些facet strips被包裹在另一个表格中,你需要调整那里的宽度。以下方法似乎有效。

g1 <- ggplotGrob(p1)
g2 <- ggplotGrob(p2)
g3 <- ggplotGrob(p3)

# g3 has the widest strips, so get the width from there and copy over
# to the other plots
stripwidth <- g3$grobs[[13]]$widths 
g1$grobs[[13]]$widths <- stripwidth
g1$grobs[[14]]$widths <- stripwidth
g1$grobs[[15]]$widths <- stripwidth

g2$grobs[[13]]$widths <- stripwidth
g2$grobs[[14]]$widths <- stripwidth
g2$grobs[[15]]$widths <- stripwidth

ggdraw(gridExtra::gtable_rbind(g1,g2,g3))

enter image description here


这就是我一直在找的,谢谢!我在修改ggplot的组件方面还是一个新手,所以我没有意识到有时候需要修改grobs而不是覆盖的gtable。 - Reilstein

-1

更改此部分

facet_grid(facet_variables ~ ., 
           space="free",
           scales="free")+

facet_grid(facet_variables ~ ., 
           space="fixed",    # This is the difference
           scales="free")+

这个不起作用。也许我的问题表述不够清晰,但我想要在构建多个独立图表并将它们对齐后,facet标签区域的宽度保持一致。 - Reilstein

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接