2

这些问题是对之前帖子的后续处理:

我刚开始学习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)

带轴的多面图

我有两个问题:

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

0 回答 0