0

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

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
4

0 回答 0