我正在尝试在堆叠条形图中使用facet_wrap,并且希望在条上显示每个部分的值标签。
以钻石数据集为例:
当只有一个图形时,我的geom_text代码可以正常工作,尽管对于较短的条形图来说可能有点拥挤:
以钻石数据集为例:
当只有一个图形时,我的geom_text代码可以正常工作,尽管对于较短的条形图来说可能有点拥挤:
diamonds %>%
ggplot(aes(x = cut, fill = clarity)) +
geom_bar() +
geom_text(data = . %>%
group_by(cut, clarity) %>%
tally() %>%
ungroup() %>%
group_by(cut) %>%
ungroup(),
aes(y = n, label = n),
position = position_stack(0.5),
show.legend = FALSE)
然而,当我添加分面时,所有标签都显示在各个单独的分面中:
diamonds %>%
ggplot(aes(x = cut, fill = clarity)) +
geom_bar() +
facet_wrap(~ color) +
geom_text(data = . %>%
group_by(cut, clarity) %>%
tally() %>%
ungroup() %>%
group_by(cut) %>%
ungroup(),
aes(y = n, label = n),
position = position_stack(0.5),
show.legend = FALSE)
如何使标签仅出现在相关的条形上?
谢谢!