热图、环形图

 表型相关热图 

 

library(ComplexHeatmap)
pheatmap(data,scale = "row")

 

 

 

 

##表达矩阵2个rho和p,需要转为长数据
library(ggcor)
dap <- cor_tbl(p,cluster = T)
darho <- cor_tbl(rho,cluster = T)


pvalue <- quickcor(dap,circular = T,cluster = T,
         open = 45, # 缺口大小
         # 内圈外圈比例
         outer = 0.5,inner = 1.5) +
  # 单元格边框线颜色
  geom_colour(colour = 'white') +
  # 自定义填充颜色
  scale_fill_gradient2(low = 'blue',mid = 'white',high = 'red') +
  # 基因名
  set_p_yaxis() + ##bcols = rand_color(10)+颜色
  # 样本名
  set_p_xaxis() +
  # 更改图例名称
  guides(fill = guide_colorbar(title = 'P value')) +
  anno_hc_bar(k = 2,fill = rand_color(2),pos = 'top',height = 0.3) +
  anno_hc_bar(k = 3,fill = rand_color(3),pos = 'top',height = 0.5) +
  anno_hc_bar(k = 4,fill = rand_color(5),pos = 'top',height = 0.2) +
  # 添加聚类树
  anno_col_tree(bcols = rand_color(5),height = 0.15) +
  anno_hc_bar(k = 15,fill = rand_color(15),pos = 'left',width = 0.5) +
  anno_hc_bar(k = 10,fill = rand_color(10),pos = 'left',width = 0.5) +
  anno_row_tree(bcols = rand_color(8))
# 添加热图
p9 <- quickcor(da,circular = T,cluster = T,grid.colour = 'white',
               open = 45, # 缺口大小
               # 内圈外圈比例
               outer = 2,inner = 1) +
  # 单元格边框线颜色
  geom_colour(colour = 'white') +
  # 自定义填充颜色
  scale_fill_gradient2(low = 'blue',mid = 'white',high = 'red',name = 'log2FC') +
  # 更改图例名称
  # guides(fill = guide_colorbar(title = 'log2FC')) +
  # 列注释
  anno_hc_bar(k = 2,fill = rand_color(2),pos = 'top',height = 0.3) +
  anno_hc_bar(k = 3,fill = rand_color(3),pos = 'top',height = 0.5) +
  anno_hc_bar(k = 4,fill = rand_color(5),pos = 'top',height = 0.2) +
  # 添加聚类树
  anno_col_tree(bcols = rand_color(5),height = 0.15) +
  anno_hc_bar(fill = rand_color(15),pos = 'left',width = 0.5) +
  anno_hc_bar(k = 10,fill = rand_color(10),pos = 'left',width = 0.5) +
  anno_row_tree(bcols = rand_color(8)) +

  # 样本名
  set_p_xaxis(bcols = rand_color(5))

p9 + anno_row_heat(da,aes(fill = r),width = 0.8,
                   geom = 'tile') +
  scale_fill_gradient2(low = 'green',mid = 'white',high = 'red',name = 'expression') +
  # 基因名
  set_p_yaxis()
# 添加点
p9 + anno_row_heat(da,aes(color = r),width = 0.8,
                   geom = 'point') +
  scale_color_gradient2(low = '#F9B208',mid = 'white',high = '#F55C47',name = 'expression') +
  # 基因名
  set_p_yaxis()

library(OmicCircos)

该包中有两个会用到的函数,一个是segAnglePo(), 该函数能够将基因组片段位置转换成环形弧度,返回一个数据框,用来说明环的大小、片段数量和片段长度。
另一个函数是circos(),用来画不同形状的环,如散点图环、线状图环、热力图环等等。

library(OmicCircos)

2、制作底图信息数据框
首先要有一个数据框(如下,my_contig)来说明你要画的环形图的基本信息,比如基因片段名称、大小等。如下格式
 scale_fill_gradient2(low="#445393", high="#EE2627", mid="white")
library(tidyverse)
library('ComplexHeatmap')
library('circlize')
library(dendextend)
library(dendsort)

mycol=colorRamp2(c(-1.7, 0.3, 2.3),c( "blue", "white", "red"))##设置颜色scale

circos.clear()
circos.par(gap.after=c(50))  ##circos.par调整圆环首尾间的距离,数值越大,距离越宽

circos.heatmap(data,
    col=mycol,
    dend.side= "inside", ##聚类树方向,也可outside
    track.height = 0.38,##轨道的高度,数值越大圆环越粗
    rownames.side= "outside", rownames.col= "black", rownames.cex=0.2, rownames.font=1,##矩阵行名方向
    
    cluster=F, ##是否聚类
    dend.track.height=0.18, ##行聚类树高度
    dend.callback=function(dend,m,si) { #聚类树回调,dend:当前扇区的树状图;m:当前扇区对应的子矩阵;si:当前扇区的名称
    color_branches(dend,k=15,col=1:15) ##聚类树颜色
    })
lg=Legend(title= "Exp",col_fun=mycol,direction = c( "vertical"))##图例
grid.draw(lg)

#circos.heatmap内只能是一个矩阵,但如果矩阵数据存在分组,可以用split参数来指定分类变量
split= sample(letters[1:2], 40, replace = TRUE)
split= factor(split, levels = letters[1:2])
circos.heatmap(data,split=split)

#假设有两个热图的矩阵数据
data2=data2
circos.heatmap(data2, col = mycol,rownames.side= "inside",rownames.cex=0.8)

library(gridBase)

lg_Exp1=Legend(title= "Exp1",col_fun=mycol2,direction = c( "vertical"))

lg_Exp2=Legend(title= "Exp2",col_fun=mycol,direction = c( "vertical"))

circle_size= unit(0.07, "snpc")

h= dev.size

lgd_list= packLegend(lg_Exp1,lg_Exp2, max_height = unit(2*h, "inch"))

draw(lgd_list, x = circle_size, just = "left")

ggplot热图

colnames(data) <- c("Cancer",'Gene','Pearson','pvalue')
data$P[which(data$pvalue >0.05)] = '>0.05'
data$P[which(data$pvalue <0.05)] = '<0.05'
data$P=factor(data$P,ordered=T,levels=c('>0.05','<0.05'))
ggplot(data=data,aes(x=Gene,y=Cancer))+
  geom_point(aes(size=P,fill=Pearson),shape=22,color="grey80")+
  scale_fill_gradient2("Pearson' rho",low="#445393", high="#EE2627", mid="white")+
  theme_bw()+
  theme(panel.grid.major= element_blank(),
        panel.grid.minor= element_blank(),
        plot.title = element_text(hjust = 0.5, size = 12),
        axis.text.y =element_text(size = 12, color = "black"),
        axis.text.x.top=element_text(angle=90,hjust = 0,vjust = 0.1,
                                     color = "black",size = 10),
        axis.ticks = element_blank(),
        legend.key.size = unit(0.15, "inches"))+
  labs(x="",y=NULL)+
  scale_size_discrete("p value",range=c(2,5))+
  scale_x_discrete(position = "top")

 

计算坐标与角度

作图

​
ggplot() +
##热图1
geom_tile(data = data[which(data$group == 'treat'),],
            aes(x = 1:nrow(data),y=1, height = 0.1,fill = logp),
            color = 'white') +scale_fill_gradient2(midpoint = 2.97593,low="#445393", high="#EE2627", mid="white", name = 'P value', breaks <- c(0.1, 2.97593, 6.8),labels=c('NS', 'cutoff', 'S')) + new_scale("fill")+
##热图2
geom_tile(data = data[which(data$group == 'treat'),],
            aes(x = 1:nrow(data), y=0.89, height = 0.1,fill = rho),
            color = 'white') + scale_fill_viridis_c('Pearson’s rho', breaks = c(-0.33, 0, 0.33,0.68), labels = c(-0.33, 0, 0.33,0.68)) +
scale_y_continuous(expand = expansion(add = c(0.75,0.05)))+  ###中心扩圆
 coord_polar(theta = 'x') + 
# 添加基因注释1
new_scale("fill")+
geom_rect(data = name, aes(xmin = xmin,xmax=xmax,ymin = 1.17,ymax=1.27,fill =cancer)) + 
scale_fill_manual(name = 'Cancer types',values = cb_palette) + 
geom_text(data = name,   ##标注行注释文本
            aes(x = x,
                y =1.3,
                label = cancer, angle = ang, hjust = hjust), size = 3)+ guides(fill = "none")+
 new_scale("fill")+ 
 # 添加基因注释2
  geom_rect(data = data,aes(xmin = xmin,xmax=xmax,ymin =1.06,ymax=1.16,fill= gene), color = NA) +
  scale_fill_manual(name = 'Gene',
                    values = descolor1)+
new_scale("fill")+
theme_void()

cancer=str_c("cancer",letters[1:5],sep=" ")
cancer=rep(cancer,8)
cancer=sort(cancer)
cancer=str_to_upper(cancer)

gene=rep(c("gene1","gene2","gene3","gene4"),10)

group=rep(c("treat","control"),each=4)
group=rep(group,5)

heat1=rnorm(40,0,1)
heat2=rnorm(40,10,5)

data=data.frame(cancer,gene,group,heat1,heat2)

#转换为长数据,且计算每层总格子数x

nrow=nrow(data)
x= 
[1]  1  2  3  4  1  2  3  4  5  6  7  8  5  6  7  8  9 10 11 12  9 10 11 12 13 14 15 16 13 14 15 16 17 18 19 20 17
[38] 18 19 20

data$x=x

##计算角度,标记文本时用,所以新建一个angle。总的来说为总数/环数,计算坐标位点
res=data[which(data$group=="treat"),]
res$ang=seq(from=(360/nrow(res))/1.5,
              to = (1.5* (360/nrow(res))) - 360,
              length.out = nrow(res)) + 80
res$hjust <- 0
res$hjust[which(res$ang < -90)] <- 1
res$ang[which(res$ang < -90)] <- (180+res$ang)[which(res$ang < -90)]
res$x=seq(1:20)
res2=data[which(data$group=="control"),]
res2$ang=res$ang
res2$hjust=res$hjust
res2$x=seq(1:20)
data=rbind(res,res2)

##添加分环信息
data$group=factor(data$group)##factor个数对应环数,此处为treat和control两组

##作图
p1=ggplot(data,aes(x = x,y = group)) +
  geom_tile(aes(fill = heat1),color = 'white') +
  scale_fill_gradient2(midpoint = 0,
                       low = '#3C8DAD',
                       mid = "white",
                       high = '#FF6767') +
  scale_y_discrete(expand = expansion(mult = c(2,0))) +
  scale_x_discrete(expand = expansion(mult = c(0,0.05))) +
  coord_polar(theta = 'x') + ##环形图
  theme_void() +
  geom_text(data = res,   ##标注行注释文本
            aes(x = x,
                y = 4.6,
                label = x, angle = ang, hjust = hjust), #x为id,可选择id的name
            size = 2)
p1

##添加注释 添加第一个分组
annotation1=as.data.frame(res$cancer)
colnames(annotation1)="cancer"
###预设颜色
library(ggsci)
descolor1=pal_npg(palette = c("nrc"), alpha = 1)(10)

ggplot() +
geom_bar(data = annotation1,stat = 'identity', # 添加注释一
           aes(x = 1:nrow(res),y = 0.25,fill = cancer),
           width = 1,
           color = NA) +
  scale_fill_manual(name = 'annotation1',values = descolor1)

##再把热图加上
p=ggplot() +
geom_bar(data = annotation1,stat = 'identity', # 添加注释一
           aes(x = 1:nrow(res),y = 0.25,fill = cancer), #y = 0.25 用来调整注释条与热图的相对位置 
           width = 1,
           color = NA) +
  scale_fill_manual(name = 'annotation1',values = descolor1)+
# 添加第一个环s1
# 新增图例
new_scale("fill") +
geom_tile(data = data[which(data$group == 'treat'),],
            aes(x = 1:nrow(res),y = 1,fill = heat1),
            color = 'white') +
  scale_fill_gradient2(midpoint = 0,
                       low = 'green',
                       mid = "black",
                       high = 'red',
                       name = 'ONE') +
  # 添加第一个环s2
  geom_tile(data = data[which(data$group == 'control'),],
            aes(x = 1:nrow(res),y = 2,fill = heat1),
            color = 'white') +
  ylim(-3,5) #ylim 参数,越负,环形热图中间就越空
p

library(circlize) ##rand_color函数
p + coord_polar(theta = 'x') +
  theme_void() +
  geom_text(data = res,
            aes(x = x, ##x的id这一列,代表环形位置的
                y = 4.6,
                label = x, angle = ang, hjust = hjust),
            # 给基因名添加颜色
            color = rand_color(200),
            size = 2)

 4、添加多层注释

annotation1=as.data.frame(res$cancer)
colnames(annotation1)="cancer"

annotation2=as.data.frame(res$gene)
colnames(annotation2)="gene"
p1 <- ggplot() +
  # 添加基因注释1
  geom_bar(data = annotation1,stat = 'identity',
           aes(x = 1:nrow(res),y = 0.25,fill = cancer),
           width = 1,
           color = NA) +
  # 添加基因注释2
  geom_bar(data = annotation2,stat = 'identity',
           aes(x = 1:nrow(res),y = -0.25,fill = gene),
           width = 1,
           color = NA) +
  scale_fill_manual(name = 'gene annotation',
                    values = descolor1)
p1

p2=p1+new_scale("fill") +
geom_tile(data = data[which(data$group == 'treat'),],
            aes(x = 1:nrow(res),y = 1,fill = heat1),
            color = 'white') +
  scale_fill_gradient2(midpoint = 0,
                       low = 'green',
                       mid = "black",
                       high = 'red',
                       name = 'ONE') +
  # 添加第一个环s2
  geom_tile(data = data[which(data$group == 'control'),],
            aes(x = 1:nrow(res),y = 2,fill = heat1),
            color = 'white') +
  ylim(-3,5)
p2

p3=p2 + coord_polar(theta = 'x') +
  theme_void() + xlim(0,21) + ##xlim很重要,决定展示圆环的多少
  geom_text(data = res,
            aes(x = x,
                y = 4.6,
                label = x, angle = ang, hjust = hjust),
            # 给基因名添加颜色
            color = rand_color(20),
            size = 2)
p3

有时候还需要对样本进行注释,同样的,添加条形图就完事

annotation1=as.data.frame(res$cancer)
colnames(annotation1)="cancer"

annotation2=as.data.frame(res$gene)
colnames(annotation2)="gene"

ringannotation1=as.data.frame(c("treat","control"))
colnames(ringannotation1)="group"

p3=p1+ 
    new_scale("fill") +
    geom_bar(data = ringannotation1,stat = 'identity',
           aes(x = -2,y = 1,fill =factor(group)),
           width = 5,
           color = NA) +
  scale_fill_manual(name = 'sample annotation',
                    values = c('control' = '#FA26A0','treat' = '#24A19C'))
p3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
环形热图是一种特殊的热图展示方式,相比于普通的热图,它沿着X轴弯曲展示基因数据,可以在更小的绘版面与空间中展示更多的基因数据。 在Python中,可以使用ggplot库来创建环形热图。首先,使用ggplot()函数创建一个基础的绘对象。然后,使用geom_tile()函数来添加热图的数据层,并设置fill参数来指定填充颜色。可以使用scale_fill_gradient2()函数来设置颜色渐变的范围和名称。 除了热图的数据层之外,还可以使用geom_bar()函数来添加注释条,并使用scale_fill_manual()函数来设置注释条的颜色。 为了将热图转化为环形热图,可以使用coord_polar()函数,并将theta参数设置为"x"。然后,使用theme_void()函数来去除背景和坐标轴的显示。可以使用xlim()函数来设置环形热图的展示范围。 最后,可以使用geom_text()函数来添加基因名称,并设置颜色、大小等属性。 具体代码如下: p = ggplot() # 添加热图数据层 p = p + geom_tile(data=data[which(data$group == 'treat'),], aes(x=1:nrow(res), y=1, fill=heat1), color='white') p = p + scale_fill_gradient2(midpoint=0, low='green', mid="black", high='red', name='ONE') # 添加注释条 p = p + geom_bar(data=annotation1, stat='identity', aes(x=1:nrow(res), y=0.25, fill=cancer), width=1, color=NA) p = p + scale_fill_manual(name='annotation1', values=descolor1) # 转化为环形热图 p = p + coord_polar(theta='x') p = p + theme_void() p = p + xlim(0, 21) # 添加基因名称 p = p + geom_text(data=res, aes(x=x, y=4.6, label=x, angle=ang, hjust=hjust), color=rand_color(20), size=2) 请注意,这只是一个示例的代码框架,具体的数据和参数设置需要根据实际情况进行调整。<span class="em">1</span><span class="em">2</span><span class="em">3</span><span class="em">4</span>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值