从数据框中绘制饼图

时间:2019-04-29 08:02:47

标签: r

我有两个变量的数据框;出勤4 5 6 7 2 5 7 8以及另一种接受治疗的A B B A A B B AA。如何制作饼图,比较rstudio中A和B的总百分比?

1 个答案:

答案 0 :(得分:0)

使用dplyrpie函数,我们首先将treatment分组,然后得出每组的总和。

a = data.frame(attendance=c(4,5,6,7,2,5,7,8),
                             treatment=c("A","B","B","A","A","B","B","A"),
                             stringsAsFactors = FALSE)

A = a%>%group_by(treatment)%>%summarise(tot=sum(attendance))

pie(A$tot/sum(A$tot),labels=paste(A$treatment,round(A$tot/sum(A$tot),2)),main="Pie")

enter image description here