-
Couldn't load subscription status.
- Fork 2.1k
Description
In plots with nested facet categories, ggplot2 repeats the facet label for the "outer" category, rather than having a single spanning facet across all the sub-categories. I have some code that I've been using to cover the outer facets with a single spanning facet strip using gtable_add_grob from the gtable package. However this code no longer works with ggplot2 2.2.0 (see below for details on what goes wrong). Below is a reproducible example (based on my answer to this SO question). I wanted to see if this is a bug and if there's a way around the problem (other than reinstalling ggplot2 2.1.0 when I want to create spanning facet strips).
# Data
densityAGRLKA = structure(list(location = structure(c(1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L), .Label = c("SF", "SS"), class = "factor"), species = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("AGR", "LKA"), class = "factor"),
position = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L), .Label = c("top", "bottom"), class = "factor"), density = c(0.41,
0.41, 0.43, 0.33, 0.35, 0.43, 0.34, 0.46, 0.32, 0.32, 0.4,
0.4, 0.45, 0.34, 0.39, 0.39, 0.31, 0.38, 0.48, 0.3, 0.42,
0.34, 0.35, 0.4, 0.38, 0.42, 0.36, 0.34, 0.46, 0.38, 0.36,
0.39, 0.38, 0.39, 0.39, 0.39, 0.36, 0.39, 0.51, 0.38)), .Names = c("location",
"species", "position", "density"), row.names = c(NA, -40L), class = "data.frame")
# Install ggplot2 2.1.0 from local file
# install.packages("ggplot2_2.1.0.tar.gz", repos=NULL, type="source")
library(ggplot2)
library(grid)
library(gtable)
# Dodge width
pd = position_dodge(0.7)
## Initital ggplot2 plot
p=ggplot(densityAGRLKA, aes("", density)) +
geom_boxplot(width=0.7, position=pd) +
theme_bw() +
facet_grid(. ~ species + location + position) +
theme(panel.margin=unit(0,"lines"),
strip.background=element_rect(color="grey30", fill="grey90"),
panel.border=element_rect(color="grey90"),
axis.ticks.x=element_blank()) +
labs(x="")
Here's what the original plot looks like:
Now, let's place spanning strips over the original facet strips.
pg = ggplotGrob(p)
# Add spanning strip labels for species
pos = c(4,11)
for (i in 1:2) {
pg <- gtable_add_grob(pg,
list(rectGrob(gp=gpar(col="grey50", fill="grey90")),
textGrob(unique(densityAGRLKA$species)[i],
gp=gpar(cex=0.8))), 3,pos[i],3,pos[i]+7,
name=c("a","b"))
}
# Add spanning strip labels for location
pos=c(4,7,11,15)
for (i in 1:4) {
pg = gtable_add_grob(pg,
list(rectGrob(gp = gpar(col="grey50", fill="grey90")),
textGrob(rep(unique(densityAGRLKA$location),2)[i],
gp=gpar(cex=0.8))), 4,pos[i],4,pos[i]+3,
name = c("c", "d"))
}
# Draw the plot
grid.draw(pg)
Here's what the plot looks like after adding spanning facet strips with ggplot2 2.1.0. This represents the original (desired) behavior of the code above:
# Now detach ggplot2 2.1.0 and install ggplot2 2.2.0
#detach(package:ggplot2, unload=TRUE)
#install.packages("ggplot2")
#library(ggplot2)
After installing and loading ggplot2 2.2.0 I ran the code above again. Below is what the plot looks like. Actually, I made one small change to the plot code before creating the graph below: I added clip="off" in both calls to gtable_add_grob. Without that change, the plot below would look exactly the same as the original, unaltered plot.
I'm not sure why, but gtable_add_grob seems to be placing the labels correctly in the horizontal dimension while ignoring the vertical placement arguments. Also, the rectGrob isn't showing up at all. Is this a bug, or does ggplot2 2.2.0 require a different workflow to make changes like this?


