본문 바로가기
R

(R)The way to make proportional stacked bar graph / plyr,ddply, dplyr, mutate, count, group_by

by jangpiano 2020. 8. 16.
반응형
<The way to make filled- stacked bar graph
 which shows the percentage of elements in each group>

I will introduce two ways to make the right graph which shows the percentage of elements in rem_rank in each group in "vore" with the samely based data with the left graph. 

 

 


<First way - mutate(), count(), group_by() in dplyr>

> mean(msleep$sleep_rem)
[1] 1.87541

> msleep<-msleep%>%filter(!is.na(sleep_rem))
> msleep2<-msleep%>%mutate(rem_rank=ifelse(sleep_rem<=1.87541,"little","much"))%>%group_by(vore,rem_rank)
> ggplot(data=msleep2,aes(x=vore,y=sleep_rem,fill=rem_rank))+geom_bar(stat="identity")


msleep<-msleep%>%filter(!is.na(sleep_rem))

> msleep2<-msleep%>%mutate(rem_rank=ifelse(sleep_rem<=1.87541,"little","much"))%>%count(vore ,rem_rank)
%>%group_by(vore)%>%mutate(per=n/sum(n)*100)

> ggplot(data=msleep2,aes(x=vore,y=per,fill=rem_rank))+geom_bar(stat="identity")



<second way - ddply() in plyr>

> ?ddply

ddply is used to split data frame and apply function then combine results into a data frame. 


I will use ddply() in plyr package make a new variable "percent_vore" by applying a function <x/sum(x)*100> which is needed to calculating each percentage of sleep rem in each vore.


> mean(msleep$sleep_rem)
[1] 1.87541
> msleep<-msleep%>%filter(!is.na(sleep_rem))

> msleep2<-msleep%>%mutate(rem_rank=ifelse(sleep_rem<=1.87541,"little","much"))%>%group_by(vore,rem_rank)
> ggplot(data=msleep2,aes(x=vore,y=sleep_rem,fill=rem_rank))+geom_bar(stat="identity")



>library(plyr)


> rate_graph<-ddply(msleep2,"vore", transform, percent_vore=sleep_rem/sum(sleep_rem)*100)

> ggplot(rate_graph,aes(x=vore,y=percent_vore,fill=rem_rank))+geom_bar(stat="identity")




반응형