将ggplot图设置为在点图行之间具有相同的x轴宽度和相同的空间

时间:2014-05-26 05:35:54

标签: r ggplot2 gridextra

更新了问题,以纳入已在SO

上回答的部分解决方案

我正在使用ggplot2创建多个绘图和gridExtra以将绘图组合成一个带有多个面板的图形,所有这些都在一列中。我的问题是,我不能让点图行之间的空间在两个图中保持一致。

enter image description here

library(ggplot2)
# data
  dat1 <- data.frame(VARIABLES=c("Item 1", "Item 2 is a little longer"),
                     est=c(.3, .5),
                     min=c(.2, .4),
                     max=c(.4, .7))
  dat2 <- data.frame(VARIABLES=c("Item 3", 
                                 "Item 4 is even longer if you can believe it",
                                 "And there is a third item",
                                 "And a fourth item"),
                     est=c(.3, .5, .3, .5),
                     min=c(.2, .4, .2, .4),
                     max=c(.4, .7, .4, .7))
  dat <- c("dat1", "dat2")
  labs <- c("Plot 1", "Plot2")
# create plots
  count <- 1
  for (i in dat) {
    p <- ggplot(get(i), aes(x=reorder(as.character(VARIABLES), est), 
                              y=est)) +
    geom_pointrange(aes(ymin=min,
                        ymax=max),
                    linetype="dashed") +
    geom_point(size=3) +
    ylim(-1,1) +
    theme_bw() +
    labs(title = labs[count]) +
    theme(legend.position="none") +
    coord_flip()
    assign(paste(i, "plot", sep="."), p)
    count <- count+1
  }
# combine plots
  library(gridExtra)
  # approach suggested by @baptise
  # http://stackoverflow.com/questions/13294952/left-align-two-graph-edges-ggplot
  gA <- ggplotGrob(dat1.plot)
  gB <- ggplotGrob(dat2.plot)
  maxWidth = grid::unit.pmax(gA$widths[2:5], gB$widths[2:5])
  gA$widths[2:5] <- as.list(maxWidth)
  gB$widths[2:5] <- as.list(maxWidth)
  grid.arrange(gA, gB, ncol=1)

3 个答案:

答案 0 :(得分:8)

library(gridExtra)
library(grid)

gb1 <- ggplot_build(dat1.plot)
gb2 <- ggplot_build(dat2.plot)

# work out how many y breaks for each plot
n1 <- length(gb1$layout$panel_params[[1]]$y.labels)
n2 <- length(gb2$layout$panel_params[[1]]$y.labels)

gA <- ggplot_gtable(gb1)
gB <- ggplot_gtable(gb2)

g <- rbind(gA, gB)

# locate the panels in the gtable layout
panels <- g$layout$t[grepl("panel", g$layout$name)]
# assign new (relative) heights to the panels, based on the number of breaks
g$heights[panels] <- unit(c(n1,n2),"null")

grid.newpage()
grid.draw(g)

enter image description here

答案 1 :(得分:1)

这是一种方法:为较短的标签填写额外的空格并使用等宽字体。

longest_name <- max(nchar(as.character(dat1$VARIABLES)), nchar(as.character(dat2$VARIABLES)))
fill_in_spaces <- function(v) paste0(paste0(rep(" ", longest_name - nchar(v)), collapse=""), v)
levels(dat1$VARIABLES) <- sapply(levels(dat1$VARIABLES), fill_in_spaces)
levels(dat2$VARIABLES) <- sapply(levels(dat2$VARIABLES), fill_in_spaces)

然后绘图程序几乎相同,只需添加

p <- p + theme(text=element_text(family="Courier", size=14))

enter image description here

存在一个小问题:级别被重新排序,因此第3项现在是最后一项,但是可以像所描述的那样容易地修复。 here

答案 2 :(得分:1)

dat1$Plot <- "Plot 1"
dat2$Plot <- "Plot 2"
dataset <- rbind(dat1, dat2)

ggplot(
  dataset, 
  aes(
    y = reorder(as.character(VARIABLES), est), 
    x = est,
    xmin = min,
    xmax = max
  )) +
  geom_errorbarh() + geom_point() + 
facet_wrap(~Plot, ncol = 1, scales = "free_y")

enter image description here