2

These questions follow up on the previous posts:

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)

Faceted figure with axes

I have two questions:

  1. How do I remove the labels from the x-axis in the top two rows, so only the axis line and ticks show?
  2. How do I prevent the y-axis title from disappearing?
Community
  • 1
  • 1
CAT
  • 73
  • 1
  • 5
  • you do realize that you don't need to do `pkg::function` when you `library(pkg)`, right? – hrbrmstr May 06 '16 at 01:33
  • Aren't you just asking for the default behavior of `facet_grid`? I don't see a need to build it row by row here, but you'd just need to adjust the code for each row respective to its location (no x-axis for the top two; y label for the middle one, etc.). – alistaire May 06 '16 at 03:55
  • @hrbrmster: Thanks for pointing out that error, I just copied it over from the OP and will fix it. – CAT May 06 '16 at 17:00
  • @alistaire: Can you expand on your comment? If I had left the panel grids on the figure I can see what you are saying, but I changed the theme to format how I want it for publication. Thanks. – CAT May 06 '16 at 17:02
  • Without `gtable`, `facet_grid` prints axes only on the outside, and will give you a y-axis label, so why are you using `gtable`? – alistaire May 06 '16 at 17:04
  • Yes, I know that. However, I was requested to produce a figure as described above, which has x-axes on each panel. I think that I can do that by making 6 different figures and using grid.arrange, however, I was wondering if there was a method in which I could do it all as one figure. – CAT May 06 '16 at 17:53

0 Answers0