These questions follow up on the previous posts:
- Force X axis text on for all facets of a facet_grid plot
- Force x-axis labels on facet_grid ggplot: x-axis labels differ per row
I'm just starting to learn gtable
. I merged the code from the above two posts to get the x-axes on each panel of the facet grid.
library(ggplot2)
diamondSub <-subset(diamonds, (cut=="Ideal" | cut=="Premium" | cut == "Very Good") &
(color=="E" | color=="I"))
p <- ggplot(diamondSub, aes(x=clarity, y=price)) +
geom_blank() +
geom_boxplot() +
facet_grid(cut~color, scales="free_x") +
theme(plot.background=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.line.x = element_line(color = 'black',size=1),
axis.line.y = element_line(color = 'black',size=1))
p
library(gtable)
g <- ggplotGrob(p)
# locate the panels
panels <- grep("panel", g$layout$name)
top <- unique(g$layout$t[panels])
# Construct each row separately
top.row <- rbind_gtable(g[seq.int(min(top)), ],
g[max(top)+1,], "first")
middle.row <- rbind_gtable(g[c(top[2]-1,top[2]), ],
g[max(top)+1,], "first")
bottom.row <- g[(max(top)-1):nrow(g), ]
all <- rbind_gtable(rbind_gtable(top.row, middle.row, "first"), bottom.row, "first")
# Draw it
grid.newpage()
grid.draw(all)
I have two questions:
- How do I remove the labels from the x-axis in the top two rows, so only the axis line and ticks show?
- How do I prevent the y-axis title from disappearing?