gpt4 book ai didi

r - 面对一个 plotly 热图

转载 作者:行者123 更新时间:2023-12-01 01:53:10 25 4
gpt4 key购买 nike

我希望能够处理 R plotly heatmap .

这就是我的意思:
我有一个分层聚类的基因表达数据集:

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]

然后我将其离散化为特定的表达范围,因为这恰好有助于解决我的情况的颜色。我还在创建其他结构来帮助我绘制 colorbar我希望它的方式:
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)
require(reshape2)
interval.df <- reshape2::melt(interval.mat,varnames=c("gene","sample"),value.name="expr")
interval.cols2 <- rep(interval.cols, each=ncol(mat))
color.df <- data.frame(range=c(0:(2*length(interval.cols)-1)),colors=c(0:(2*length(interval.cols)-1)))
color.df <- setNames(data.frame(color.df$range,color.df$colors),NULL)
for (i in 1:(2*length(interval.cols))) {
color.df[[2]][[i]] <- interval.cols[[(i + 1) / 2]]
color.df[[1]][[i]] <- i/(2*length(interval.cols))-(i %% 2)/(2*length(interval.cols))
}

我以他们的方式生成数据,我知道样本 1-500 是一个集群,样本 501:1000 是另一个集群,所以我标记它们:
  interval.df$cluster <- NA
interval.df$cluster[which(interval.df$sample %in% paste("s",1:500,sep="."))] <- "A"
interval.df$cluster[which(interval.df$sample %in% paste("s",501:1000,sep="."))] <- "B"

我认为添加一个没有颜色和间隔的样本会在 heatmap 中创建一个白色的列。看起来像一个刻面边界的图:
divider.df <- data.frame(gene=unique(interval.df$gene),sample=NA,expr=NA,cluster=NA)
interval.df <- rbind(dplyr::filter(interval.df,cluster == "A"),divider.df,dplyr::filter(interval.df,cluster == "B"))

现在我尝试绘图:
#add ticks for each cluster
tick.vals <- c("s.158","s.617")
tick.text <- c("A","B")

require(plotly)
plot_ly(z=c(interval.df$expr),x=interval.df$sample,y=interval.df$gene,colors=interval.cols2,type="heatmap",colorscale=color.df,
colorbar=list(title="score",tickmode="array",tickvals=c(1:6),ticktext=names(interval.cols),len=0.2,outlinecolor="white",bordercolor="white",borderwidth=5,bgcolor="white")) %>%
layout(xaxis = list(title = 'Cluster',tickmode = 'array',tickvals = tick.vals,ticktext = tick.text))

但我没有看到集群之间有任何分离:
enter image description here

知道如何在两个集群之间实现这样的分面边界吗?

最佳答案

您的示例非常复杂,因此我将其缩减为一个最小示例,以专注于您在热图的象限中寻找的差距。

修改自 plotly 站点上的示例,here .

library(plotly)

m <- matrix(rnorm(9), nrow = 3, ncol = 3)

p <- plot_ly(
x = c("a", "b", "c"), y = c("d", "e", "f"),
z = m, type = "heatmap"
)

subplot(p, p, p, p, shareX = TRUE, shareY = TRUE, nrows = 2)

如果您为每个象限创建一个绘图对象,然后使用 subplot ,您将得到类似于以下内容的结果:

enter image description here

N.B. I have cropped out the legend because it was duplicated for the facets, you could merge these into one.

关于r - 面对一个 plotly 热图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42617446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com