我希望能够在 R 中的分组条形图上的组之间添加空格。通常我会使用
geom_bar(position = position_dodge(width = ))
但我不能使用 position =
,因为它已经被用作 position = "stack"
。
有办法解决这个问题吗?
我希望能够增加组之间的空间,因此增加大写字母 A 和 B、B 和 C 等之间的空间。
我还希望能够更改我通常使用的堆叠条的颜色和图案
scale_fill_manual
和 geom_col_pattern
但这些不起作用,因为它们会将我的绘图重新变成一组。
set.seed(687532) # Create example data frame
data <- data.frame(facet = rep(LETTERS[1:5], each = 6),
group = c("x", "y"),
stack = letters[1:3],
value = round(abs(rnorm(30)), 2))
data
ggplot(data, # Draw barplot with grouping & stacking
aes(x = group,
y = value,
fill = stack)) +
geom_bar(stat = "identity",
position = "stack", width = 0.6) +
theme_classic() + theme(panel.border = element_rect(colour = NA, fill=NA, size=0),
text = element_text(size = 8, family = "sans"),
) + scale_y_continuous(expand = c(0,0)) + labs(x = "", y = expression(paste("Relative Abundance(%)"))) +
facet_wrap(~facet, strip.position = "bottom", scales = "free_x") +
theme(panel.background = element_blank(),
panel.spacing = unit(0, "line"),
strip.background = element_blank(),
strip.placement = "outside") + theme(plot.title.position = 'plot',
plot.title = element_text(hjust = 0.5)) + theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
由于您的组实际上是面,因此只需
expand
x 轴。
ggplot(data, aes(x = group, y = value, fill = stack)) +
geom_col(position = "stack", width = 0.6) +
scale_y_continuous("Relative Abundance(%)", expand = c(0, 0)) +
scale_x_discrete(NULL, expand = c(0.5, 0.5)) +
facet_wrap(~facet, strip.position = "bottom", scales = "free_x") +
theme_classic() +
theme(panel.border = element_blank(),
panel.background = element_blank(),
panel.spacing = unit(0, "line"),
strip.background = element_blank(),
strip.placement = "outside",
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))