【发布时间】:2017-07-20 08:25:03
【问题描述】:
我正在尝试生成一个plotlyheatmap,我希望通过离散比例指定颜色。
这就是我的意思:
生成具有 2 个聚类的数据并分层聚类:
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]
将mat 中的值划分为间隔并为每个间隔设置颜色:
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)
使用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()+
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")
这是我尝试使用plotly 生成它:
plot_ly(z=mat,x=colnames(mat),y=rownames(mat),type="heatmap",colors=interval.cols)
给出:
这些数字并不相同。在ggplot2 图中,与plotly 图相比,集群更加明显。
有什么方法可以对plotly 命令进行参数化以提供更类似于ggplot2 图的内容?
另外,是否可以使 plotly 图例离散 - 类似于 ggplot2 图中的图例?
现在假设我想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,by=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,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")
因此这些簇被panel.spacing 分隔,看起来更加明显。有什么办法可以通过plotly 实现这种刻面?
【问题讨论】:
-
用
ggplotly怎么样? -
对于这些维度,将 ggplot 转换为使用 ggplotly 进行绘图需要不合理的时间(很多很多分钟) - 不太实用