问题描述
我想使用 pheatmap 制作一个热图,如果有任何东西在 -1 到 -0.5 之间,它应该是深绿色,在 -0.5 到 -0.10 之间的任何东西都应该是浅绿色,而在 -0.10 到 0 之间的任何东西都应该是白色。同样,0 到 0.10 为白色,0.10 到 0.5 为浅紫色,0.5 到 1 之间的任何为紫色。我也不想扩展我的数据并且没有集群。到目前为止,我在 R 中有这段代码:
0.85 0.63 0.61 0.60 0.53 0.23 0.20 0.15 0.12 0.08 0.04 -0.05 -0.08 -0.19 -0.34 -0.56 -0.78
0.75 0.54 0.51 0.50 0.45 0.41 0.35 0.12 0.08 0.04 -0.01 -0.07 -0.15 -0.45 -0.51 -0.57 -0.68
df <- read.table("test.txt",header = FALSE,sep = "\t")
pheatmap(as.matrix(df),scale="none",cluster_rows = FALSE,cluster_cols = FALSE,annotation_names_col = FALSE,show_colnames= FALSE,color = colorRampPalette(colors = c("darkgreen","white","purple"))(200),main = "A3SS",border_color = NA,fontsize_row=10
)
如何使用 pheatmap 中的中断来实现我的目标?
解决方法
您必须正确设置中断并构建调色板 - 如果您真的想要 200 种颜色,这应该可以完成工作(如果没有看到下一部分):
library(pheatmap)
# dummy data
df <- data.table::fread("0.85 0.63 0.61 0.60 0.53 0.23 0.20 0.15 0.12 0.08 0.04 -0.05 -0.08 -0.19 -0.34 -0.56 -0.78
0.75 0.54 0.51 0.50 0.45 0.41 0.35 0.12 0.08 0.04 -0.01 -0.07 -0.15 -0.45 -0.51 -0.57 -0.68")
# make the color pallete
clrsp <- colorRampPalette(c("darkgreen","white","purple"))
clrs <- clrsp(200)
breaks1 <- seq(-1,1,length.out = 200)
pheatmap(as.matrix(df),scale="none",cluster_rows = FALSE,cluster_cols = FALSE,annotation_names_col = FALSE,show_colnames= FALSE,color = clrs,main = "A3SS",breaks = breaks1,fontsize_row=10)
如果您只想使用五种颜色并按照已知值进行精确切割,则可以选择从已制作的 RColorBrewer 包装托盘中采购:
breaks2 <- c(-1,-0.5,-0.1,0.1,0.5,1)
pheatmap(as.matrix(df),color = rev(RColorBrewer::brewer.pal(5,name = "PiYG")),breaks = breaks2,fontsize_row=10)
或者,您可以在矢量中通知颜色,但文本中没有明确的浅紫色,无论如何您可以使用十六进制颜色代码更精确,然后只是“亮”/“暗”:
pheatmap(as.matrix(df),color = c("darkgreen","lightgreen","darkorchid1","purple"),fontsize_row=10)