ggplot:对齐多个多面图 - 各个不同尺寸的面

时间:2018-05-31 20:10:00

标签: r ggplot2 data-visualization gridextra cowplot

我正在尝试将多个图表与facet对齐。我的问题有些微不足道但是很顺利:我可以制作一个绘图,使绘图区域对齐,刻面本身对齐,但刻面条的宽度不一样。如果小平面的标签长度​​不同,则小平面条的大小应使文本可以适合小平面。到目前为止,我无法找到一种方法,在对齐多个图时使所有小平面条的宽度相同。

以下是我想要对齐的图表类型以及我们对齐它们的努力的示例:

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 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

小平面条包裹在另一张桌子内,您需要调整其宽度。以下似乎有效。

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

答案 2 :(得分:-1)

更改此部分

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

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