如何在饼图中制作标签

时间:2019-07-16 17:04:49

标签: r ggplot2

我有这样的数据

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")

我只是希望共享的每个数字都出现在该标签的一部分上,但我无法弄清楚

2 个答案:

答案 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")

An example

答案 1 :(得分:0)

尝试一下-

> ggplot(df, aes(x="", y=share, fill=brand))+
  geom_bar(stat="identity", width=1)+ 
  coord_polar("y", start=0)+ 
  geom_text(aes(label = paste0(share)),position = position_stack(vjust = 0.5)) 

enter image description here