密度图--ggplot2

利用ggplot2绘制密度图,并直接在密度图上叠加另一组数据的密度曲线
library('ggplot2')########################画图
library('reshape2')########################melt
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")+
##fill填充颜色,color 线条的颜色,apha 透明度,linetype线形,实线
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"),###显示x,y轴
axis.ticks.x=element_blank(), ###取消x轴刻度线
###刻度标签设置,以及坐标轴titile
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)

attachments-2018-05-2ylINOUg5b079870a806d.png

两组数据直接叠加密度图

数据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)+  ##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")+
##fill填充颜色,color 线条的颜色,apha 透明度,linetype线形,实线
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"),###显示x,y轴
axis.ticks.x=element_blank(), ###取消x轴刻度线
###刻度标签设置,以及坐标轴titile
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)


attachments-2018-05-h9EQes5B5b0798a5161a8.png


如果想提升自己的绘图技能,我们推荐:R语言绘图基础(ggplot2)


更多生物信息课程:

1. 文章越来越难发?是你没发现新思路,基因家族分析发2-4分文章简单快速,学习链接:基因家族分析实操课程基因家族文献思路解读

2. 转录组数据理解不深入?图表看不懂?点击链接学习深入解读数据结果文件,学习链接:转录组(有参)结果解读转录组(无参)结果解读

3. 转录组数据深入挖掘技能-WGCNA,提升你的文章档次,学习链接:WGCNA-加权基因共表达网络分析

4. 转录组数据怎么挖掘?学习链接:转录组标准分析后的数据挖掘转录组文献解读

5. 微生物16S/ITS/18S分析原理及结果解读OTU网络图绘制cytoscape与网络图绘制课程

6. 生物信息入门到精通必修基础课:linux系统使用perl入门到精通perl语言高级R语言入门R语言画图

7. 医学相关数据挖掘课程,不用做实验也能发文章:TCGA-差异基因分析GEO芯片数据挖掘GEO芯片数据标准化GSEA富集分析课程TCGA临床数据生存分析TCGA-转录因子分析TCGA-ceRNA调控网络分析

8.其他,二代测序转录组数据自主分析NCBI数据上传二代测序数据解读


  • 发表于 2018-05-25 13:01
  • 阅读 ( 7306 )
  • 分类:R

0 条评论

请先 登录 后评论
Daitoue
Daitoue

167 篇文章

作家榜 »

  1. omicsgene 698 文章
  2. 安生水 347 文章
  3. Daitoue 167 文章
  4. 生物女学霸 120 文章
  5. xun 82 文章
  6. 红橙子 78 文章
  7. rzx 74 文章
  8. CORNERSTONE 72 文章