ggplot2:使用gtable将条带标签移动到facet_grid的面板顶部,并通过' class'将多个facet_grid图形创建到一个网格中

时间:2015-06-05 07:26:58

标签: r ggplot2 grid facet gtable

正如您在标题中看到的那样,我的问题实际上与此question相关。

这个question的解决方案效果很好,但是现在我想拍摄这个情节并根据“'类”生成多个facet_grid图。 (仍将标签保持在顶部)成一个网格图布置。我正在使用for循环来生成多个facet_grid图。

这就是我到目前为止,代码中的注释描述了正在发生的事情(或者我认为至少发生了什么),但我的问题似乎是在代码的末尾。我试图将这些索引的facet_grid图表列表输出到一个网格排列,但它不起作用。

R代码:

library(ggplot2)
library(gtable)
library(grid)
library(gridExtra)

# looping to create multiple graphs by vehicle class.
for (j in 1:length(unique(mpg$class))){

# as it loops, mpg is subsetted by each class into mpg2.
mpg2 <- mpg[mpg$class==as.character(unique(mpg$class)[j]),]

#this line was put in, because indexing mt[[j]] won't work unless the object mt has been
#created first. Without this line I get this error:
# Error in mt[[j]] <- ggplot(mpg2, aes(x = cty, y = model)) + geom_point() +  : 
#   object 'mt' not found
mt <- ggplot(mpg2, aes(x = cty, y = model)) + geom_point()

#with the creation of the object 'mt', in the one line above, these ggplots can now be assigned to 'mt[[j]]'
mt[[j]] <- ggplot(mpg2, aes(x = cty, y = model)) + geom_point() +
  facet_grid(manufacturer ~ ., scales = 'free', space = 'free') +
  theme(panel.margin = unit(0.5, 'lines'), 
        strip.text.y = element_text(angle = 0))

# Get the gtable of each ggplot in mt[[j]]
gt <- ggplotGrob(mt[[j]])

# Get the position of the panels in the layout
panels <-c(subset(gt$layout, name=="panel"))

# Add a row above each panel
for(i in rev(panels$t-1)) gt = gtable_add_rows(gt, unit(.5, "lines"), i)

# Get the positions of the panels and the strips in the revised layout
panels <-c(subset(gt$layout, name=="panel", se=t:r))
strips <- c(subset(gt$layout, name=="strip-right", se=t:r))

# Get the strip grobs
stripText = gtable_filter(gt, "strip-right")

# Insert the strip grobs into the new rows
for(i in 1:length(strips$t)) gt = gtable_add_grob(gt, stripText$grobs[[i]],  t=panels$t[i]-1, l=4, r=4)

# Remove the old strips
gt = gt[,-5]

# For this plot - adjust the heights of the strips and the empty row above the strips
for(i in panels$t) {
  gt$heights[i-1] = list(unit(0.8, "lines"))
  gt$heights[i-2] = list(unit(0.2, "lines"))
}

# Draw it - this line of code, below, has been commented out, because grid.draw(gt) wouldn't assign to plots[[j]]
#plots[[j]] <- grid.draw(gt)

#this line was put in, because indexing plots[[j]] won't work unless the object 'plots' has been
#created first. Without this line I get this error:
# Error in plots[[j]] <- arrangeGrob(gt) : object 'plots' not found
plots <- arrangeGrob(gt)

# here i want each arrageGrob(gt) plot created in this loop to then be saved to plots[[j]]
# so i can then call the plots later in a list and output them using grid.arrange.
plots[[j]] <- arrangeGrob(gt)

}
# running the code up to here, works without any error, but the code below where I am
# trying to create the grid of plots, does not work.  


# here i am trying to place all the plots into a list.
allplots <- plots[j-j+1:j]

# I now want to gather all of the 'class' facet_grid plots into one grid.
graph <- do.call("grid.arrange", c(allplots, ncol=2))
graph

0 个答案:

没有答案