提问人:Dai 提问时间:9/30/2023 最后编辑:stefanDai 更新时间:10/6/2023 访问量:21
ggplot facet_nested删除子标题,只保留分组的标题
ggplot facet_nested remove sub-headers and only keep the grouped headers
问:
我有以下代码,可以创建带有子标题的绘图。
pdf <- data.frame(xx = as.factor(c(rep(0, 100), rep(1, 100))),
value = rnorm(200),
selected_site = c('y', rep('n', 98), 'y'),
name = as.factor(rep(1:5, each = 20)),
version = rep(c('A', 'B'), each = 100))
pdf %>%
ggplot(aes(x = value, fill = xx)) +
geom_histogram(alpha=0.6, position="identity") +
facet_nested(version*selected_site ~ name, scale = 'free', ) +
scale_fill_manual(name = '', values = c('red', 'blue')) +
facetted_pos_scales(
y = list(selected_site == TRUE ~ scale_y_continuous(limits = c(0,2), guide = "none"),
selected_site == FALSE ~ scale_y_continuous(limits = c(0,15), guide = "none"))) +
xlab('') + ylab('') +
theme_bw() + theme(legend.position = 'none')
我想删除子标题“n”和“y”,只保留分组标题“A”和“B”。有没有办法做到这一点?
答:
0赞
stefan
9/30/2023
#1
调整我在 facet_wrap2() 上的答案 由两个变量分组,但用一个变量标记,您可以通过 的参数删除条带背景和文本。然而,这并不完美,因为它保留了一些空白空间。不幸的是,但我也没有找到摆脱它的选项:strip
strip_nested
library(ggplot2)
library(ggh4x)
set.seed(123)
pdf |>
ggplot(aes(x = value, fill = xx)) +
geom_histogram(alpha = 0.6, position = "identity") +
facet_nested(version * selected_site ~ name,
scale = "free",
strip = strip_nested(
by_layer_y = TRUE,
background_y = list(
element_rect(),
element_blank()
),
text_y = list(
element_text(),
element_blank()
)
)
) +
scale_fill_manual(name = "", values = c("red", "blue")) +
facetted_pos_scales(
y = list(
selected_site == "TRUE" ~ scale_y_continuous(limits = c(0, 2), guide = "none"),
selected_site == "FALSE" ~ scale_y_continuous(limits = c(0, 15), guide = "none")
)
) +
xlab("") +
ylab("") +
theme_bw() +
theme(legend.position = "none")
评论