您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“如何利用ggplot2繪制箱線圖并進行分面處理”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“如何利用ggplot2繪制箱線圖并進行分面處理”這篇文章吧。
利用ggplot2繪制箱線圖,并進行分面處理。
加載相應的R包與數據設置
library('ggplot2')########################畫圖 library('reshape2')########################melt library('grid') ########################分面 A =rep( c("A","B","C","D"),each=2) B = c(6.332968,9.368328,6.674348,4.127901,5.192845,6.652865,7.829350,6.995062) C = c(5.367671,7.286253,5.217053,3.875520,6.679444,6.127819,5.091166,7.942029) D = c(5.171107,6.232718,5.320568,4.924498,7.140883,4.228142,5.793514,6.347785) E = c(5.533754,6.152393,6.113618,4.960935,5.959568,5.078903,4.871103,5.223206) F = rep(c("sample1","sample2"),len=4) dat = data.frame(A,B,C,D,E) names(dat)[1] = c("type") names(dat)[2:5]=F dat = melt(dat,variable.name="Sample",value.name = "Num") head(dat)
繪制基本的箱線圖
##################按照類型進行統計 P_box=ggplot(data=dat,aes(x=type,y=Num)) + geom_boxplot(fill="grey",width=0.4,notch=F)+ ##########填充顏色fill, 寬,中位線缺刻notch=T labs(x="type",y="Num")+ 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'), axis.ticks.x=element_blank(),###########取消x軸刻度線 axis.text.x=element_text(size = 10,face = "bold", vjust = 0.5, hjust = 0.5),############刻度標簽文字大小等設置 axis.text.y=element_text(size = 10,face = "bold", vjust = 0.5, hjust = 0.5), axis.title.x = element_text(size = 20,face = "bold", vjust = 0.5, hjust = 0.5), axis.title.y = element_text(size = 20,face = "bold", vjust = 0.5, hjust = 0.5), ######取消默認的背景顏色方框等 panel.background = element_rect(fill = "transparent",colour = "black"), panel.grid.minor = element_blank(), panel.grid.major = element_blank(), plot.background = element_rect(fill = "transparent",colour = "black")) print(P_box)
利用樣品進行分面
# 基于樣品進行分面 P_box=ggplot(data=dat,aes(x=type,y=Num)) + geom_boxplot(fill="grey",width=0.4,notch=F)+ labs(x="type",y="Num")+ 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'), axis.ticks.x=element_blank(), axis.text.x=element_text(size = 10,face = "bold", vjust = 0.5, hjust = 0.5), axis.text.y=element_text(size = 10,face = "bold", vjust = 0.5, hjust = 0.5), axis.title.x = element_text(size = 20,face = "bold", vjust = 0.5, hjust = 0.5), axis.title.y = element_text(size = 20,face = "bold", vjust = 0.5, hjust = 0.5), panel.background = element_rect(fill = "transparent",colour = "black"), panel.grid.minor = element_blank(), panel.grid.major = element_blank(), plot.background = element_rect(fill = "transparent",colour = "black"))+ facet_grid(.~Sample) #########分面 print(P_box)
以上是“如何利用ggplot2繪制箱線圖并進行分面處理”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。