ggplot2: Использование gtable для перемещения надписей полос на верхнюю часть панели для facet_grid И создание нескольких графиков facet_grid по классам в одной сетке
Как видно из названия, мой вопрос на самом деле связан с этим вопросом.
Решение этого вопроса прекрасно работает, но теперь я хотел бы взять этот график и создать несколько графиков 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