在facet_grid上强制x轴标签ggplot:drop lables

时间:2016-05-06 01:25:53

标签: r ggplot2 axes facet-grid

这些问题跟进了以前的帖子:

我刚开始学习gtable。我合并了上面两个帖子中的代码,以获得构面网格的每个面板上的x轴。

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

我有两个问题:

  1. 如何从前两行的x轴中删除标签,只显示轴线和刻度?
  2. 如何防止y轴标题消失?

0 个答案:

没有答案