尝试以下操作:
library(dplyr)
library(ggplot2)
data <- data.frame(a=c("a1","a1","a2","a3","a1","a2","a3","a4","a2","a1","a5","a4","a3"),b=1:13)
data <- data %>%
group_by(a) %>%
count() %>%
ungroup() %>%
mutate(per=`n`/sum(`n`)) %>%
arrange(per)
data$label <- scales::percent(data$per)
ggplot(data=data)+
geom_bar(aes(x="", y=per, fill=a), stat="identity", width = 1)+
coord_polar("y", start=0)+
theme_void()+
geom_text(aes(x=1, y = cumsum(per) - per/2, label=label))
我还包括饼图的另一个版本,翻转圆形切片和标签的顺序(如果这是你的意思):
ggplot(data=data)+
geom_bar(aes(x="", y=per, fill=a), stat="identity", width = 1)+
coord_polar("y", start=0, direction = -1)+
theme_void()+
geom_text(aes(x=1, y = cumsum(per) - per/2, label=label))
您可以检查[这里](https://开头stackov erlot.com/questions/35682703/display-percentage-values-on-a-pie-chart) – akrun
我没有确切的值列,应该在geom_bar中使用aes(y = .. count ..)计算它,并且这个问题导致问题 – far