R入门函数与脚本

R Pheatmap包 画热图,自定义颜色条,保存PDF

2023-08-13  本文已影响0人  火卫控

R Pheatmap包 画热图,自定义颜色条,保存PDF

采用colorRampPalette 定义颜色条
自定义函数save_pheatmap_pdf 来 定制化保存PDF

结果如下:
[图片上传失败...(image-949b6-1692000081567)]

代码如下:

library(pheatmap)
#test1 <-read.csv("C:/Users/Administrator/Desktop/YsJ/sz/zzwkP1.csv",header=TRUE)
#test1 <-read.csv("D:\\12345.csv",header=TRUE,row.names = 1)
#"D:\GZlab_W_AQY\实验记录\原始数据\2023.4.17-假病毒\20230417\20230147.csv"
#"D:\Coding\R_gzlab_docu\protease_test\20230420jia2nd\20230420.csv"
test1 <-read.csv("D:\\Coding\\R_gzlab_docu\\protease_test\\20230807-all\\protesea-all.csv",row.names = 1,header = T)

test1 <- scale(test1)

#breaks
#bk <- c(seq(-30,99,by=5),seq(100,230,by=5))
bk <- c(seq(0,99,by=10),seq(100,500,by=10))

p <- pheatmap(test1,
              cluster_rows = FALSE,  
              cluster_cols = F,
              border_color = "white",
              display_numbers = T,
              fontsize = 5,
              cellwidth = 25, cellheight = 5,
              
              
              #display_numbers = matrix(ifelse(test > 5, "*", "")),
              #legend_breaks = -1:4, legend_labels = c("0", "1e-4", "1e-3", "1e-2", "1e-1", "1"),
              
              
              color = c(colorRampPalette(colors = c("blue","white"))(length(bk)/5),colorRampPalette(colors = c("white","red"))(length(bk)/1.25)),
              legend_breaks=seq(0,500,20),
              breaks=bk,
              #color = colorRampPalette(colors = c("blue","pink","red")(100))
              #color = colorRampPalette(colors = c("blue","white","red"))(100)
)


save_pheatmap_pdf <- function(x, filename, width=7, height=7) {
  stopifnot(!missing(x))
  stopifnot(!missing(filename))
  pdf(filename, width=width, height=height)
  grid::grid.newpage()
  grid::grid.draw(x$gtable)
  dev.off()
}

save_pheatmap_pdf(p, "20230807testall.pdf",8,25)
上一篇 下一篇

猜你喜欢

热点阅读