Position geom_text in the middle of each bar segment in a geom_col stacked barchart [duplicate]

好久不见. 提交于 2019-12-02 23:56:38

You need to have a variable mapped to an aesthetic to represent the groups in geom_text. For you, this is your "sector" variable. You can use it with the group aesthetic in geom_text.

Then use position_stack with vjust to center the labels.

ggplot(data = dta) +
    geom_col(aes(x = group, y = value, fill = sector)) +
    geom_text(aes(x = group, y = value, label = value, group = sector),
                  position = position_stack(vjust = .5))

You could save some typing by setting your aesthetics globally. Then fill would be used as the grouping variable for geom_text and you can skip group.

ggplot(data = dta, aes(x = group, y = value, fill = sector)) +
    geom_col() +
    geom_text(aes(label = value),
              position = position_stack(vjust = .5))

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!