我有这样的数据
df = data.frame("brand" = c("Samsung","Huawei","Apple","Xiaomi","OPPO","Other"),
"share" = c(2090,1580,1210,930,860,3320))
然后我要制作图表并将这些数字放在上面
ggplot(df, aes(x="", y=share, fill=brand))+
geom_bar(stat="identity", width=1)+
coord_polar("y", start=0)+
geom_text(aes(y = share, label = brand), color = "white")
我只是希望共享的每个数字都出现在该标签的一部分上,但我无法弄清楚
答案 0 :(得分:1)
这个作品。
library("dplyr")
df <- df %>% arrange(desc(brand)) %>% mutate(lab.ypos = cumsum(share) - 0.5*share)
ggplot(df, aes(x = "", y = share, fill = brand)) +
geom_bar(width = 1, stat = "identity", color = "white") + coord_polar("y", start = 0)+
geom_text(aes(y = lab.ypos, label = share), color = "white")
答案 1 :(得分:0)