r - ggplot2:从图中删除未使用的因子水平组合的方面 (facet_grid)

标签 r ggplot2 facet-grid

我想从多面 ggplot2 图中有选择地删除不必要的方面。我看过这个问题,但不知道怎么做(也许那里的建议现在已经过时了):

adding empty graphs to facet_wrap in ggplot2

这是一个最小的例子。我想删除右下角的空刻面 (b, 2)。

library('ggplot2')
d <- data.frame('factor_1' = factor(c('a', 'a', 'b')),
                'factor_2' =    factor(c('1', '2', '1')),
                x = 1:3, y = 1:3)

ggplot(data = d, mapping = aes(x = x, y = y)) +
  geom_point() +
  facet_grid(facets = factor_1 ~ factor_2, drop = TRUE)

enter image description here

显然drop = TRUE在这里没有影响,因为没有未使用的因子水平,只有未使用的组合。

最佳答案

在 ggplot2 2.2.0 中,绘图中的 grob 名称已更改。

library(ggplot2)
library(grid)
d <- data.frame('factor_1' = factor(c('a', 'a', 'b')),
                'factor_2' =    factor(c('1', '2', '1')),
                x = 1:3, y = 1:3)

p = ggplot(data = d, mapping = aes(x = x, y = y)) +
  geom_point() +
  facet_grid(facets = factor_1 ~ factor_2, drop = TRUE)

# Get ggplot grob
g = ggplotGrob(p)

# Get the layout dataframe. 
# Note the names.
# You want to remove "panel-2-2"
g$layout

# gtable::gtable_show_layout(g) # Might also be useful

# Remove the grobs
# The grob needs to be remove,
#  and the relevant row in the layout data frame needs to be removed
pos <- grepl(pattern = "panel-2-2", g$layout$name)
g$grobs <- g$grobs[!pos]
g$layout <- g$layout[!pos, ]


# Alternatively, replace the grobs with the nullGrob
g = ggplotGrob(p)
pos <- grep(pattern = "panel-2-2", g$layout$name)
g$grobs[[pos]] <- nullGrob()

# If you want, move the axis
# g$layout[g$layout$name == "axis-b-2", c("t", "b")] = c(8, 8)

# Draw the plot
grid.newpage()
grid.draw(g)

enter image description here

您链接中的答案需要修改如下:
n <- 1000
df <- data.frame(x = runif(n), y=rnorm(n), label = sample(letters[1:7], 
                 size = n, replace = TRUE), stringsAsFactors=TRUE)
df$label.new <- factor(df$label, levels=sort(c(""," ",levels(df$label))))


p <- ggplot(df, aes(x=x, y=y)) + geom_point() + 
         facet_wrap(~ label.new, ncol=3,drop=FALSE)

g = ggplotGrob(p)

g$layout # Note the names and their positions (t, b, l, r)
# gtable::gtable_show_layout(g) # Might also be useful

pos <- g$layout$name %in% c("panel-1-1", "panel-1-2", "strip-t-1-1", "strip-t-2-1")
g$grobs <- g$grobs[!pos]
g$layout <- g$layout[!pos, ]

# Or replace the grobs with the nullGrob
g = ggplotGrob(p)
pos <- g$layout$name %in% c("panel-1-1", "panel-1-2", "strip-t-1-1", "strip-t-2-1")
g$grobs[pos] <- list(nullGrob())

# Move the axis
g$layout[g$layout$name == "axis-l-1-1", c("l", "r")] = c(10,10)

grid.newpage()
grid.draw(g)

关于r - ggplot2:从图中删除未使用的因子水平组合的方面 (facet_grid),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40807252/

相关文章:

r - ggplot2 中高级 fiddle 图的中位数和四分位数

r - geom_col 以错误的方式绘制数据

r - 将ggplot对象从R中的循环内存储在列表中

r - 将带有 facet_grid 的 ggplot2 对象的标题移到中间

java - 在 java 代码中运行 R 脚本并将 r 结果输出到文本文件

r - 使用 R 将列名插入其值

r - 重新排序因子后正确绘制 xyplot 线

r - geom_col with facet_grid with margins=TRUE 不堆叠?

r - 如何在 R 中为 ggplot 的每个方面添加 R2?

r - 使用 dplyr 添加多列并根据条件填充单元格