在绘制热图中使用离散的自定义颜色 [英] Using discrete custom color in a plotly heatmap
问题描述
我试图生成绘图
heatmap
,其中我希望颜色由这是我的意思:
使用2个群集生成数据并对它们进行分层聚类:
require(permute)
set.seed(1)
mat <-rbind(cbind(matrix (rnorm(2500,2,1),nrow = 25,ncol = 500),矩阵(rnorm(2500,-2,1),nrow = 25,ncol = 500)),
cbind (2500,-2,1),nrow = 25,ncol = 500),矩阵(rnorm(2500,2,1),nrow = 25,ncol = 500)))
rownames(mat)<粘贴(g,1:50,sep =。)
colnames(mat)< - 粘贴(s,1:1000,sep =。)
hc.col < - hclust(dist(t(mat)))
dd.col< - as.dendrogram(hc.col)
col.order< - order.dendrogram(dd.col)
hc.row< - hclust(dist(mat))
dd.row< - as.dendrogram(hc.row)
row.order< - order.dendrogram(dd。行)
mat < - mat [row.order,col.order]
Brake mat
to中的值为每个区间设置一个颜色:
require(RColorBrewer)
mat.intervals< - cut(mat ,break = 6)
interval.mat< - 矩阵(mat.intervals,nrow = 50,ncol = 1000,dimnames = list(rownames(mat),colnames(mat)))
interval。 cols< - brewer.pal(6,Set2)
名称(interval.cols)< - 等级(mat.intervals)
使用 ggplot2
我通过这种方式绘制这个 heatmap
legend
指定离散的颜色和各自的范围):
require(reshape2 )
interval.df< - reshape2 :: melt(interval.mat,varnames = c(gene,sample),value.name =expr)
require(ggplot2)
ggplot(interval.df,aes(x = sample,y = gene,fill = expr))+
geom_tile(color = NA)+ theme_bw()+
主题(strip.text。 X = element_text(角度= 90,vjust = 1,hjust = 0.5,大小= 6),panel.spacing =单位(0.025, CM),legend.key = element_blank(),plot.margin =单元(c( 1,1,1,1), CM),legend.key.size =部(0.25, CM),窗格l.border = element_blank(),strip.background = element_blank(),axis.ticks.y = element_line(size = 0.25))+
scale_color_manual(drop = FALSE,values = interval.cols,labels = names( (例如:interval.cols),name =expr)+
scale_fill_manual(drop = FALSE,values = interval.cols,labels = names(interval.cols),name =expr)
给出:
这是我试图用 plotli
生成它:
plot_ly(z = mat,x = colnames(mat),y = rownames(mat),type =heatmap,colors = interval.cols)
给出:
这些数字并不相同。在 ggplot2
图中,与绘图
图相比,集群更加明显。
是否有任何方式参数化 ggplot2
figure?
另外,是否有可能将绘制为
图例离散 - 类似于 ggplot2
figure?
现在假设我想要 facet
群集。在 ggplot2
情况下,我会这样做:
require(dplyr)
facet.df< - data.frame(sample = c(paste(s,1:500,sep =。),paste(s,501:1000,sep =。) )),facet = c(rep(f1,500),rep(f2,500)),stringsAsFactors = F)
interval.df< - left_join(interval.df,facet.df,通过= c(sample=sample))
interval.df $ facet < - factor(interval.df $ facet,levels = c(f1,f2))
然后绘制:
ggplot(interval.df,aes(x = sample,y = gene,fill = expr))+ facet_grid(〜facet,scales =free,space =free,switch =both)+
geom_tile(color = NA)+ labs(x =facet,y =gene)+ theme_bw()+
theme(strip.text.x = element_text(angle = 90,vjust = 1 ,hjust = 0.5,大小= 6),panel.spacing =部(0.05, CM),plot.margin =单元(c(1,1,1,1), CM),legend.key.size =单元(0.25,cm),panel.border = element_blank(),strip.background = element_blank(),axis.ticks.y = element_line(size = 0.25))+
scale_color_manual(drop = FALSE,值= interval.cols,标签=名称(interval.cols)中,n ame =expr)+
scale_fill_manual(drop = FALSE,values = interval.cols,labels = names(interval.cols),name =expr)
给出:
所以这些集群由 panel.spacing
分隔,看起来更加明显。有没有办法通过 plotli
?
最初考虑的是同样的事情,即对渐变进行下采样,但相反,强迫更严格的过渡似乎至少可以使颜色更加明显。
interval.cols2 < - rep(interval.cols,each = 1000)
plot_ly(z = mat,x = colnames(mat),y = rownames(mat),type =heatmap,colors = interval.cols2)
I'm trying to generate a plotly
heatmap
, where I'd like the colors to be specified by a discrete scale.
Here's what I mean:
Generate data with 2 clusters and hierarchically cluster them:
require(permute)
set.seed(1)
mat <- rbind(cbind(matrix(rnorm(2500,2,1),nrow=25,ncol=500),matrix(rnorm(2500,-2,1),nrow=25,ncol=500)),
cbind(matrix(rnorm(2500,-2,1),nrow=25,ncol=500),matrix(rnorm(2500,2,1),nrow=25,ncol=500)))
rownames(mat) <- paste("g",1:50,sep=".")
colnames(mat) <- paste("s",1:1000,sep=".")
hc.col <- hclust(dist(t(mat)))
dd.col <- as.dendrogram(hc.col)
col.order <- order.dendrogram(dd.col)
hc.row <- hclust(dist(mat))
dd.row <- as.dendrogram(hc.row)
row.order <- order.dendrogram(dd.row)
mat <- mat[row.order,col.order]
Brake the values in mat
to intervals and set a color for each interval:
require(RColorBrewer)
mat.intervals <- cut(mat,breaks=6)
interval.mat <- matrix(mat.intervals,nrow=50,ncol=1000,dimnames=list(rownames(mat),colnames(mat)))
interval.cols <- brewer.pal(6,"Set2")
names(interval.cols) <- levels(mat.intervals)
Using ggplot2
I draw this heatmap
this way (also having the legend
specify the discrete colors and respective ranges):
require(reshape2)
interval.df <- reshape2::melt(interval.mat,varnames=c("gene","sample"),value.name="expr")
require(ggplot2)
ggplot(interval.df,aes(x=sample,y=gene,fill=expr))+
geom_tile(color=NA)+theme_bw()+
theme(strip.text.x=element_text(angle=90,vjust=1,hjust=0.5,size=6),panel.spacing=unit(0.025,"cm"),legend.key=element_blank(),plot.margin=unit(c(1,1,1,1),"cm"),legend.key.size=unit(0.25,"cm"),panel.border=element_blank(),strip.background=element_blank(),axis.ticks.y=element_line(size=0.25))+
scale_color_manual(drop=FALSE,values=interval.cols,labels=names(interval.cols),name="expr")+
scale_fill_manual(drop=FALSE,values=interval.cols,labels=names(interval.cols),name="expr")
which gives:
This is my attempt to generate it with plotly
:
plot_ly(z=mat,x=colnames(mat),y=rownames(mat),type="heatmap",colors=interval.cols)
which gives:
The figures are not identical. In the ggplot2
figure the clusters are much more pronounced in contrast to the plotly
figure.
Is there any way to parametrize the plotly
command to give something more similar to the ggplot2
figure?
Also, is it possible to make the plotly
legend discrete - similar to that in the ggplot2
figure?
Now suppose I want to facet
the clusters. In the ggplot2
case I'd do:
require(dplyr)
facet.df <- data.frame(sample=c(paste("s",1:500,sep="."),paste("s",501:1000,sep=".")),facet=c(rep("f1",500),rep("f2",500)),stringsAsFactors=F)
interval.df <- left_join(interval.df,facet.df,by=c("sample"="sample"))
interval.df$facet <- factor(interval.df$facet,levels=c("f1","f2"))
And then plot:
ggplot(interval.df,aes(x=sample,y=gene,fill=expr))+facet_grid(~facet,scales="free",space="free",switch="both")+
geom_tile(color=NA)+labs(x="facet",y="gene")+theme_bw()+
theme(strip.text.x=element_text(angle=90,vjust=1,hjust=0.5,size=6),panel.spacing=unit(0.05,"cm"),plot.margin=unit(c(1,1,1,1),"cm"),legend.key.size=unit(0.25,"cm"),panel.border=element_blank(),strip.background=element_blank(),axis.ticks.y=element_line(size=0.25))+
scale_color_manual(drop=FALSE,values=interval.cols,labels=names(interval.cols),name="expr")+
scale_fill_manual(drop=FALSE,values=interval.cols,labels=names(interval.cols),name="expr")
Which gives:
So the clusters are separated by the panel.spacing
and look even more pronounced. Is there any way to achieve this faceting with plotly
?
I was thinking initially the same thing, which is to down-sample the gradient, but instead forcing harsher transitions seems to do the trick at least to make the colors more pronounced.
interval.cols2 <- rep(interval.cols, each=1000)
plot_ly(z=mat,x=colnames(mat),y=rownames(mat),type="heatmap",colors=interval.cols2)
这篇关于在绘制热图中使用离散的自定义颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!