您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關如何利用ggplot2繪制密度圖的內容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
利用ggplot2繪制密度圖,并直接在密度圖上疊加另一組數據的密度曲線
library('ggplot2')library('reshape2')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_density=ggplot(dat,aes(x=Num))+ geom_density(aes(fill=as.character(dat$Sample),color=as.character(dat$Sample)),alpha = 0.5,size=1,linetype="solid")+ theme(plot.title = element_text(size = 25,face = "bold", vjust = 0.5, hjust = 0.5), legend.title = element_blank(), legend.text = element_text(size = 15, face = "bold"), legend.position = 'right', legend.key.size=unit(0.5,'cm'), axis.line=element_line(size = 1,color="black"), axis.ticks.x=element_blank(), axis.text.x=element_text(size = 15,face = "bold", vjust = 0.5, hjust = 0.5), axis.text.y=element_text(size = 15,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 = NA), panel.grid.minor = element_blank(), panel.grid.major = element_blank(), plot.background = element_rect(fill = "transparent",colour = NA)) print(P_density)
兩組數據直接疊加密度圖
數據dat1
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) dat1 = data.frame(A,B,C,D,E) names(dat1)[1] = c("type") names(dat1)[2:5]=F dat1= melt(dat1,variable.name="Sample",value.name = "Num") head(dat1)
數據dat2
A =rep( c("A","B","C","D"),each=2) B = c(9.944277,9.245216,8.741771,8.573114,7.953372,10.756460,7.904934,8.971346) C = c(8.248881,9.238328,9.789772,9.800562,8.698050,9.083611,9.076143,9.650690) D = c(9.884433,9.863561,10.756525,9.520756,8.363614,9.184047,10.004748,9.019348) E = c(9.821923,9.430095,9.431069,8.589512,7.755056,9.935671,7.219894,9.492607) F = rep(c("sample3","sample4"),len=4) dat2 = data.frame(A,B,C,D,E) names(dat2)[1] = c("type") names(dat2)[2:5]=F dat2 = melt(dat2,variable.name="Sample",value.name = "Num") head(dat2)
繪圖
P_density=ggplot(data=NULL)+ geom_density(aes(x=dat1$Num,fill=as.character(dat1$Sample),color=as.character(dat1$Sample)),alpha = 0.3,size=1,linetype="solid")+ geom_density(aes(x=dat2$Num,fill=as.character(dat2$Sample),color=as.character(dat2$Sample)),alpha = 0.3,size=1,linetype="solid")+ labs(x="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 = 15, face = "bold"), legend.position = 'right', legend.key.size=unit(0.5,'cm'), axis.line=element_line(size = 1,color="black"), axis.ticks.x=element_blank(), axis.text.x=element_text(size = 15,face = "bold", vjust = 0.5, hjust = 0.5), axis.text.y=element_text(size = 15,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 = NA), panel.grid.minor = element_blank(), panel.grid.major = element_blank(), plot.background = element_rect(fill = "transparent",colour = NA)) print(P_density)
感謝各位的閱讀!關于“如何利用ggplot2繪制密度圖”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,讓大家可以學到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。