学个R包,复现一张CNS级别美图(附代码)学一个R包给文章加一张CNS级别的美图(四)学一个R包给文章加一张CNS级别的美图(五)学一个R包给文章加一张CNS级别的美图(六)
加载包
我们还是构建一个跟前面推文一致的数据:#加载R包library(ComplxHatmap)library(circliz)#构建一个跟之前一样的矩阵st.sd(1)nr1=4;nr=8;nr=6;nr=nr1+nr+nrnc1=6;nc=8;nc=10;nc=nc1+nc+ncmat-cbind(rbind(matrix(rnorm(nr1*nc1,man=1,sd=0.5),nr=nr1),matrix(rnorm(nr*nc1,man=0,sd=0.5),nr=nr),matrix(rnorm(nr*nc1,man=0,sd=0.5),nr=nr)),rbind(matrix(rnorm(nr1*nc,man=0,sd=0.5),nr=nr1),matrix(rnorm(nr*nc,man=1,sd=0.5),nr=nr),matrix(rnorm(nr*nc,man=0,sd=0.5),nr=nr)),rbind(matrix(rnorm(nr1*nc,man=0.5,sd=0.5),nr=nr1),matrix(rnorm(nr*nc,man=0.5,sd=0.5),nr=nr),matrix(rnorm(nr*nc,man=1,sd=0.5),nr=nr)))mat-mat[sampl(nr,nr),sampl(nc,nc)]rownams(mat)=past0("gn",sq_ln(nr))colnams(mat)=past0("Sampl",sq_ln(nc))had(mat)##Sampl1SamplSamplSampl4Sampl5Sampl6##gn10.-0.....##gn0......##gn0......##gn40...-0.-0..##gn5-0......##gn60.-0...-0.-0.##Sampl7Sampl8Sampl9Sampl10Sampl11Sampl1##gn1-0..-0...-0.##gn1..-0....##gn-0.-0..-0..-0.##gn40.-0.....##gn5..-0....8##gn6-0...76900..-0.##Sampl1Sampl14Sampl15Sampl16Sampl17Sampl18##gn1-0.-0...-0.-0.##gn1.-0...-0..##gn-0.-0.-0.0451.-0..##gn40......##gn51.-0..-0.-1..##gn6-0..-0....##Sampl19Sampl0Sampl1SamplSamplSampl4##gn11..-0.-0...##gn0...-0...##gn1......##gn4-0......##gn50..-0.-0...##gn60......
图片注释
所谓图片注释就是把热图中的信息用特定图片进行反应,比如使用箱式图、密度图等等,就像是使用ggplot绘制相关性图片时在边缘叠加其他类型图片,我们分别来看看有哪些可以进行图文注释吧!Barplot#普通条形图注释,计算每一列的平均值并用柱状图进行注释ha1=HatmapAnnotation(Man=anno_barplot(colMans(mat),gp=gpar(fill=1:4)))Hatmap(mat,nam="mat",top_annotation=ha1)
#叠加条形图注释,随机生成一份数据进行叠加图注释m=matrix(runif(10*4),nc=10)m=t(apply(m,1,function(x)x/sum(x)))ha=HatmapAnnotation(Immun_Cll=anno_barplot(m,gp=gpar(fill=:11),bar_width=1,hight=unit(,"cm")))Hatmap(mat,nam="mat",top_annotation=ha)
#叠加柱状图的注释是不是和我们常见的Cibrsorts计算免疫浸润细胞的结果图片类似?所以我们以后可以将基因表达矩阵热图和免疫浸润细胞结果合在一起进行可视化Histogram#能用柱状图那肯定也能用直方图啦ha4=rowAnnotation(Man=anno_histogram(mat,gp=gpar(fill=:5)))Hatmap(mat,nam="mat",right_annotation=ha4)Dnsityplot
#同样使用mat矩阵进行密度图映射ha5=rowAnnotation(Man=anno_dnsity(mat,joyplot_scal=,gp=gpar(fill=:4)))Hatmap(mat,nam="mat",right_annotation=ha5)
#将密度图变为小提琴图ha6=rowAnnotation(Man=anno_dnsity(mat,typ="violin",gp=gpar(fill=:4)))Hatmap(mat,nam="mat",right_annotation=ha6)
#将密度图变为热图ha7=rowAnnotation(Man=anno_dnsity(mat,typ="hatmap",width=unit(,"cm")))Hatmap(mat,nam="mat",right_annotation=ha7)
文本注释
相对于图片注释的花里胡哨来说,文本注释可以在我们的日常绘图中反而更加常用:#使用月份名称替代基因注释,实际应用过程中可替代为各种表型名称ha8=rowAnnotation(foo=anno_txt(month.nam,location=1,rot=0,#旋转0度just="right",#右对齐gp=gpar(fontsiz=1:1+4)))Hatmap(mat[1:1,1:1],nam="mat",right_annotation=ha8)
#也可以和我们之前讲过的其他参数结合起来ha9=rowAnnotation(foo=anno_txt(month.nam,location=0.5,just="cntr",gp=gpar(fill=rp(:5,ach=),col="gray40",bordr="rd"),width=max_txt_width(month.nam)*1.))Hatmap(mat[1:1,1:4],nam="mat",right_annotation=ha9
有时候我们只需要标注特定的文本,就像上次我们进行单细胞绘图注释一样,那么:ha=rowAnnotation(foo=anno_mark(at=c(1:4,1:18),#用at参数需要标注文本的行数labls=month.nam[1:11]))#labls对应at的个数Hatmap(mat,nam="mat",clustr_rows=FALSE,right_annotation=ha)
综合注释
接下来我们看一个综合注释的例子,这个例子包含一个整体热图,然后从整体热图中提取出两列特定的热图,接着再对这两列热图进行注释,映射相应特征:st.sd()m=matrix(rnorm(50*10),nrow=50)ht_list=Hatmap(m,nam="Immun_Cll")#热图主体ha=HatmapAnnotation(summary=anno_summary(hight=unit(,"cm")))v=sampl(lttrs[1:],50,rplac=TRUE)ht_list=ht_list+Hatmap(v,nam="IC_Man",top_annotation=ha,width=unit(1,"cm"))#映射的特定列1ha=HatmapAnnotation(summary=anno_summary(gp=gpar(fill=:),hight=unit(,"cm")))v=rnorm(50)ht_list=ht_list+Hatmap(v,nam="TMB_Scor",top_annotation=ha,width=unit(1,"cm"))#映射的特定列split=sampl(lttrs[1:],50,rplac=TRUE)#设置聚类切割的样本类型lgd_boxplot=Lgnd(labls=c("groupa","groupb"),titl="group",lgnd_gp=gpar(fill=c("rd","blu")))#设置lgnddraw(ht_list,row_split=split,ht_gap=unit(5,"mm"),hatmap_lgnd_list=list(lgd_boxplot))#绘制热这种注释适用于我们要对热图主体的某一特征进行描述的时候,想象一下,热图主体是免疫细胞含量,mat1是每一行的平均值,mat是TMB值,这样我们就可以直观地在一个图片中观察到免疫细胞含量、免疫细胞含量平均值、TMB三者之间的关系,这样的图片反应的信息更加丰富,自然也就显得比别人技高一筹啦!当然我们有时候还想在热图主体的右边加一些其他个性化的注释,比如我们还是利用前面构建的mat函数绘图:
subgroup=sampl(lttrs[1:],18,rplac=TRUE,prob=c(1,5,10))rg=rang(mat)panl_fun=function(indx,nm){pushViwport(viwport(xscal=rg,yscal=c(0,)))grid.rct()grid.xaxis(gp=gpar(fontsiz=8))grid.boxplot(m[indx,],pos=1,dirction="horizontal")popViwport()}#自定义函数绘制boxplotanno=anno_zoom(align_to=subgroup,which="row",panl_fun=panl_fun,siz=unit(,"cm"),gap=unit(1,"cm"),width=unit(4,"cm"))Hatmap(mat,nam="mat",right_annotation=rowAnnotation(foo=anno),row_split=subgroup)
热图修饰
一个“花里胡哨”的热图总是会在别人意想不到的地方添加更多的信息,而这些操作在ComplxHatmap中无非就是多加几行参数罢了,现在我们通过前面的学习,已经基本把ComplxHatmap的参数都学了,接下来就让我们把这些参数串起来,看看出来的结果可以有多花哨吧!ha1=HatmapAnnotation(pt=anno_points(1:10,gp=gpar(col=rp(:,ach=5)),hight=unit(,"cm")),show_annotation_nam=FALSE)#绘制第一个热图的图形注释ha=HatmapAnnotation(ln=anno_lins(cbind(1:10,10:1),gp=gpar(col=:,lty=1:),hight=unit(,"cm")),show_annotation_nam=FALSE)#绘制第二个热图的图形注释m=matrix(rnorm(),10)ht_list=Hatmap(m,nam="mat1",top_annotation=ha1)+Hatmap(m,nam="mat",top_annotation=ha)+Hatmap(m[,1],nam="mat",top_annotation=HatmapAnnotation(summary=anno_summary(gp=gpar(fill=:))),width=unit(1,"cm"))draw(ht_list,ht_gap=unit(7,"mm"))
#添加lgnd显示更多信息lgd_list=list(Lgnd(labls=c("rd","grn"),titl="pt",typ="points",pch=16,lgnd_gp=gpar(col=:)),Lgnd(labls=c("rd","grn"),titl="ln",typ="lins",lgnd_gp=gpar(col=:,lty=1:)),Lgnd(labls=c("group1","group"),titl="km",typ="boxplot",lgnd_gp=gpar(fill=:)))draw(ht_list,ht_gap=unit(7,"mm"),row_km=,annotation_lgnd_list=lgd_list)还可以再复杂一点:
st.sd(1)mat=matrix(rnorm(80,),8,10)mat=rbind(mat,matrix(rnorm(40,-),4,10))rownams(mat)=past0("R",1:1)colnams(mat)=past0("C",1:10)ha_column1=HatmapAnnotation(points=anno_points(rnorm(10)),annotation_nam_sid="lft")ht1=Hatmap(mat,nam="ht1",km=,column_titl="Hatmap1",top_annotation=ha_column1,row_nams_sid="lft")ha_column=HatmapAnnotation(typ=c(rp("a",5),rp("b",5)),col=list(typ=c("a"="rd","b"="blu")))ht=Hatmap(mat,nam="ht",row_titl="Hatmap",column_titl="Hatmap",bottom_annotation=ha_column,column_km=)ht_list=ht1+ht+rowAnnotation(bar=anno_barplot(rowMans(mat),width=unit(,"cm")))draw(ht_list,row_titl="Hatmaplist",column_titl="Hatmaplist")好了,今天的内容又结束啦。到今天为止,我们把ComplxHatmap的常用参数基本学完,再一次感谢R包的作者顾博士,曾经我以为可视化也不过如此,关键还是在于数据清洗,知道我学习了ComplxHatmap之后,才发现原来可视化可以有这么多的玩法。并且!你以为ComplxHatmap就只能用来画热图?TooYoungTooNaiv!—END—预览时标签不可点收录于话题#个上一篇下一篇