您好,登錄后才能下訂單哦!
小編給大家分享一下如何使用ggplot2繪制堆疊柱狀圖,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
繪制堆疊柱狀圖,重點重點在于geom_bar中postion="stack",
dat type Sample Num 1 A sample1 90 2 B sample1 34 3 C sample1 56 4 D sample1 99 5 E sample1 15 6 A sample2 50 7 B sample2 20 8 C sample2 24 9 D sample2 70 10 E sample2 14
繪圖部分將geom_bar(position="stack")設置成堆疊類型可以生成堆疊的柱狀圖,默認fill的順序是”Sample“因子的順序,如果不是有序因子一般是字母排序的順序。
p = ggplot(dat, aes(x = type,y = Num,fill = Sample))+ geom_bar(stat ="identity",width = 0.6,position ="stack")+ scale_fill_manual(values = c("red","blue"))+ labs(x = "",y = "", title = "test")+ geom_text(aes(label = dat$Num),position=position_stack(vjust =0.5),size = 5)+ guides(fill = guide_legend(reverse = F))+ theme(plot.title = element_text(size = 25,face = "bold", vjust = 0.5, hjust = 0.5), legend.title = element_blank(), legend.text = element_text(size = 18, face = "bold"), legend.position = 'right', legend.key.size=unit(0.8,'cm')) print(p)
反向堆疊操作position_stack(reverse=T)
p = ggplot(dat, aes(x = type,y = Num,fill = Sample))+ geom_bar(stat ="identity",width = 0.6,position =position_stack(reverse = T))+ scale_fill_manual(values = c("red","blue"))+ labs(x = "",y = "", title = "test")+ geom_text(aes(label = dat$Num),position=position_stack(reverse =T,vjust =0.5),size = 5)+ guides(fill = guide_legend(reverse = F))+ theme(plot.title = element_text(size = 25,face = "bold", vjust = 0.5, hjust = 0.5), legend.title = element_blank(), legend.text = element_text(size = 18, face = "bold"), legend.position = 'right', legend.key.size=unit(0.8,'cm')) print(p)
以上是“如何使用ggplot2繪制堆疊柱狀圖”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。