I would like to remove the redundancy of strip labels when using facet_wrap() and faceting with two variables and both scales free.
For example, this
I am not sure you can do this by just using facet_wrap, so probably your attempt is the way to go. But IMO it needs an improvement. Presently, you are missing actual y-lab (sales) and it kinda misguides what is plotted in y- axis
You could improve what you are doing by adding another plot title row by using gtable and grid.
p1 <- ggplot(dt[dt$year == 2000,], aes(median, sales)) +
geom_point() +
facet_wrap("month", scales = "free") +
theme(axis.title.x = element_blank())
p2 <- ggplot(dt[dt$year == 2001,], aes(median, sales)) +
geom_point() +
facet_wrap("month", scales = "free") +
theme(axis.title.x = element_blank())
p3 <- ggplot(dt[dt$year == 2002,], aes(median, sales)) +
geom_point() +
facet_wrap("month", scales = "free")
Note that the labs are removed from the above plots.
if ( !require(grid) ) { install.packages("grid"); library(grid) }
if ( !require(gtable ) ) { install.packages("gtable"); library(gtable) }
z1 <- ggplotGrob(p1) # Generate a ggplot2 plot grob
z1 <- gtable_add_rows(z1, unit(0.6, 'cm'), 2) # add new rows in specified position
z1 <- gtable_add_grob(z1,
list(rectGrob(gp = gpar(col = NA, fill = gray(0.7))),
textGrob("2000", gp = gpar(col = "black",cex=0.9))),
t=2, l=4, b=3, r=13, name = paste(runif(2))) #add grobs into the table
Note that in step 3, getting the exact values for t (top extent), l(left extent), b (bottom extent) and r(right extent) might need trial and error method
Now repeat the above steps for p2 and p3
z2 <- ggplotGrob(p2)
z2 <- gtable_add_rows(z2, unit(0.6, 'cm'), 2)
z2 <- gtable_add_grob(z2,
list(rectGrob(gp = gpar(col = NA, fill = gray(0.7))),
textGrob("2001", gp = gpar(col = "black",cex=0.9))),
t=2, l=4, b=3, r=13, name = paste(runif(2)))
z3 <- ggplotGrob(p3)
z3 <- gtable_add_rows(z3, unit(0.6, 'cm'), 2)
z3 <- gtable_add_grob(z3,
list(rectGrob(gp = gpar(col = NA, fill = gray(0.7))),
textGrob("2002", gp = gpar(col = "black",cex=0.9))),
t=2, l=4, b=3, r=13, name = paste(runif(2)))
finally, plotting
plot_grid(z1, z2, z3, nrow = 3)
You can also have the years indicated in the column like in facet_grid instead of row. In that case, you have to add a column by using gtable_add_cols. But make sure to (a) add the column at the correct position in step-2, and (b) get the correct values for t, l, b and r in step-3.