我想用水平条形图显示数据并通过分组变量对其进行分面。因为我想要一个带有分面的水平图,所以我将使用geom_barh
来自ggstance
包裹。我有一个数据集,其中我的观察结果被分为几种不同的类型和计数。像这样的事情:
library(tidyverse)
data <- tibble(observations = c(1:17),
type = c("a", "a", "a", "a",
"b", "b", "b", "b", "b", "b",
"c", "c", "c", "c", "c", "c", "c"),
n = c(30:46))
这是我的问题。当我使用facet_wrap
,我的条形有不同的宽度:
library(ggstance)
ggplot(data, aes(x = n, y = reorder(observations, n))) +
geom_barh(stat = "identity") +
facet_wrap(~ type, ncol = 1, scales = "free_y")
但是当我使用facet_grid
,我无法将条带移至顶部,因为没有strip.position
争论:
ggplot(data, aes(x = n, y = reorder(observations, n))) +
geom_barh(stat = "identity") +
facet_grid(type ~ . , scales = "free_y", space = "free_y")
这只是 ggplot 的怪癖之一还是有办法操纵它?