'Labels on top' 与 facet_grid,或 'space option' 与 facet_wrap

'Labels on top' with facet_grid, or 'space option' with facet_wrap

facet_grid 允许我根据 y 轴上的项目数调整每个面的宽度(space 参数):

df <- data.frame(label = c("Variable one", rep("Variable two", 2), rep("Variable three", 3)), item = c("A", "B", "C", "D", "E", "F"), value = rnorm(6))
ggplot(df, aes(x = value, y = item)) + 
geom_point() + 
facet_grid(label ~ ., scales = "free_y", space = "free_y") + 
ylab("") + 
theme(strip.text.y = element_text(angle=0))

但我希望分面标签在顶部,所以我切换到 facet_wrap,并丢失了 space 参数(分面具有相同的宽度):

ggplot(df, aes(x = value, y = item)) + 
geom_point() + 
facet_wrap(~ label, scales = "free_y", ncol = 1) + 
ylab("")

是否可以两全其美?

预先感谢您的帮助。

我不知道如何在 ggplot 中做到这一点,但您可以使用 latticeExtra:

实现具有 ggplotoid 样式的类似布局
library(latticeExtra)
df$label <- factor(df$label, levels = unique(df$label))

dotplot(item ~ value | label, data = df,
        layout = c(1, 3),
        as.table = TRUE,
        scales = list(y = list(relation = "free")),
        par.settings = ggplot2like())
resizePanels()

可以手动完成。所需图中三个面板的高度之比大致为1:3:2。可以通过改变grobs来调整三个面板的高度:

library(ggplot2)
library(grid)
df <- data.frame(label = c("Variable one", rep("Variable two", 2), rep("Variable three", 3)), item = c("A", "B", "C", "D", "E", "F"), value = rnorm(6))

p1 = ggplot(df, aes(x = value, y = item)) + 
geom_point() + 
facet_wrap(~ label, scales = "free_y", ncol = 1) + 
ylab("")

g1 = ggplotGrob(p1) 

g1$heights[[7]] = unit(1, "null")
g1$heights[[12]] = unit(3, "null")
g1$heights[[17]] = unit(2, "null")

grid.newpage()
grid.draw(g1)

或者,高度可以设置为与原图中的高度相同:

p2 = ggplot(df, aes(x = value, y = item)) + 
geom_point() + 
facet_grid(label ~ ., scales = "free_y", space = "free_y") + 
ylab("") + 
theme(strip.text.y = element_text(angle=0))

g2 = ggplotGrob(p2) 

g1$heights[[7]] = g2$heights[[6]]
g1$heights[[12]] = g2$heights[[8]]
g1$heights[[17]] = g2$heights[[10]]

grid.newpage()
grid.draw(g1)

或者,可以不参考原图设置高度。可以根据df中每个labelitems个数来设置。并从 @baptiste's answer here 借用一些代码到 select 与面板对应的布局中的项目:

# From 'df', get the number of 'items' for each 'label'.
# That is, the number y-breaks in each panel.
library(plyr)
N = dlply(df, .(label), function(x) length(row.names(x)))

# Get the items in the g1 layout corresponding to the panels.
panels1 <- g1$layout$t[grepl("panel", g1$layout$name)]

# Replace the default panel heights with relative heights
g1$heights[panels1] <- unit(N, "null")

## Draw g1
grid.newpage()
grid.draw(g1)

我认为此功能不存在或计划在 ggplot2 (see here for the closed issue discussing this feature)

中实施

但是 ggforce 软件包支持您正在寻找的东西

library(ggplot2)
library(ggforce)
df <- data.frame(label = c("Variable one", rep("Variable two", 2), rep("Variable three", 3)), item = c("A", "B", "C", "D", "E", "F"), value = rnorm(6))
  
ggplot(df, aes(x = value, y = item)) + 
  geom_point() + 
  ggforce::facet_col(facets = vars(label), 
                     scales = "free_y", 
                     space = "free") +
  ylab("") + 
  theme(strip.text.y = element_text(angle=0))

reprex package (v0.3.0)

于 2020-06-28 创建