千家信息网

如何利用ggplot2绘制密度图

发表于:2025-01-19 作者:千家信息网编辑
千家信息网最后更新 2025年01月19日,这篇文章给大家分享的是有关如何利用ggplot2绘制密度图的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。利用ggplot2绘制密度图,并直接在密度图上叠加另一组数据的密度曲
千家信息网最后更新 2025年01月19日如何利用ggplot2绘制密度图

这篇文章给大家分享的是有关如何利用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]=Fdat = 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]=Fdat1= 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]=Fdat2 = 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绘制密度图"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

0