在使用堆積條形圖時候,新增一個百分比堆積條形圖,可以加深讀者印象。
封裝一個function函數后只需要在調用的數據上改一下pos=‘fill’的代碼即可。比較方便。
案例:
# 封裝函數 fun1<-function(data,xlab,fillc,pos,xname,yname){ ggplot(data,aes(x=xlab,fill=fillc))+ geom_bar(position = pos)+ labs(x=xname,y=yname)+ coord_flip()+ theme_minimal() } # 探索不同教育程度的學生的課程主題(pos=‘stack’指定使用堆積條形圖) p1<-fun1(student_data,student_data$Topic,fillc = student_data$StageID,pos='stack',xname='Topic',yname='student count') p2<- fun1(student_data,student_data$Topic,fillc = student_data$StageID,pos='fill',xname='Topic',yname='student count') multiplot(p1,p2)