嘗試以下操作:
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