我想在ggplot的堆积条旁边绘制带有标签的人口金字塔。我使用position_stack(vjust=1.3)
下面的代码来使标签显示在条的“顶部”旁边,但是我不理解该命令如何缩放标签的位置。
library(tidyverse)
data.frame(
sex = c("F", "F", "F", "F", "F", "F", "F", "F", "M", "M", "M", "M", "M", "M", "M", "M"),
ag = c("0-9", "10-19", "20-29", "30-39", "40-49", "50-59", "60-69", "70+"),
n = c(-0.21, -0.12, -0.09, -0.03, -0.04, -0.01, 0, 0, 0.22, 0.11, 0.06, 0.04, 0.02, 0.03, 0.01, 0),
stringsAsFactors = F
) %>%
ggplot(aes(x=ag, y = n, fill=sex)) +
geom_col() +
scale_fill_brewer("",labels = c("Women", "Men"), palette = "Set1") +
coord_flip() +
geom_text(data = . %>% dplyr::filter(sex == "M"),
aes(label = n),
position=position_stack(vjust=1.3)) +
geom_text(data = . %>% dplyr::filter(sex == "F"),
aes(label = n),
position=position_stack(vjust=-0.3))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句