标签不会与水平堆叠条居中(geom_col和position_fill)

时间:2019-08-20 13:32:52

标签: r ggplot2 label geom-col

我想用geom_col创建水平堆积的条形图。我几乎可以正确地做到,标签没有居中。我在geom_text函数上尝试了不同的aes参数组合,但是到目前为止,还没有成功。这是我最接近的。有人知道出什么问题了吗?

df <- data.frame(year=c("2014", "2015", "2014", "2015", "2014", "2015", "2014", "2015"),
                 city=c("Berlin", "Berlin", "Paris", "Paris", "New York", "New York", 
                        "Frankfurt", "Frankfurt"),
                 number=c(501,2285,558, 4001, 600, 3000, 305, 35000))

fig <- ggplot(df, aes(x = year, y = number)) +  
  geom_col(aes(fill = city), position = position_fill()) +
  geom_text(aes(y = number, label = number, group = year), 
            position=position_fill(vjust = 0.5), colour = "white") +
  coord_flip() +
  theme_bw() +
  theme(legend.position = "none") +
  scale_y_continuous(breaks = c(0,0.2,0.4,0.6,0.8,1.0), 
                     labels = c("0%","20%","40%","60%","80%","100%"))
fig

0 个答案:

没有答案