Force x-axis labels on facet_grid ggplot: drop lables

萝らか妹 提交于 2020-01-04 08:28:04

问题


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:

  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?

来源:https://stackoverflow.com/questions/37062783/force-x-axis-labels-on-facet-grid-ggplot-drop-lables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!