【SCI绘图】【热力图系列2 R】多特征相关性分析热力图指定聚类 R

SCI,CCF,EI及核心期刊绘图宝典,爆款持续更新,助力科研!

本期分享:

【SCI绘图】【热力图系列2 R】多特征相关性分析热力图指定聚类 R

1.环境准备

R 复制代码
library(gplots)
library(RColorBrewer)

2.数据读取

R 复制代码
#########################################################
### reading in data and transform it to matrix format
#########################################################

data <- read.csv("dataset.csv", comment.char="#")
rnames <- data[,1]                            # assign labels in column 1 to "rnames"
mat_data <- data.matrix(data[,2:ncol(data)])  # transform column 2-5 into a matrix
rownames(mat_data) <- rnames                  # assign row names

3.绘图展示

R 复制代码
#########################################################
### customizing and plotting heatmap
#########################################################

# creates a own color palette from red to green
my_palette <- colorRampPalette(c("red", "yellow", "green"))(n = 299)

# creates a 5 x 5 inch image
png("h3_categorizing.png",
  width = 5*300,        # 5 x 300 pixels
  height = 5*300,
  res = 300,            # 300 pixels per inch
  pointsize = 8)        # smaller font size

heatmap.2(mat_data,
  cellnote = mat_data,  # same data set for cell labels
  RowSideColors = c(    # grouping row-variables into different
     rep("gray", 3),   # categories, Measurement 1-3: green
     rep("blue", 3),    # Measurement 4-6: blue
     rep("black", 4)),    # Measurement 7-10: red
  main = "Categories",  # heat map title
  notecol="black",      # change font color of cell labels to black
  density.info="none",  # turns off density plot inside color legend
  trace="none",         # turns off trace lines inside the heat map
  margins =c(12,9),     # widens margins around plot
  col=my_palette,       # use on color palette defined earlier 
  dendrogram="row",     # only draw a row dendrogram
  Colv="NA")            # turn off column clustering

# adding a color legend for the categories
par(lend = 1)           # square line ends  for the color legend
legend("topright",      # location of the legend on the heatmap plot
    legend = c("category1", "category2", "category3"), # category labels
    col = c("gray", "blue", "black"),  # color key
    lty = 1,            # line style
    lwd = 10,           # line width
   )

dev.off()
相关推荐
霁月的小屋4 分钟前
从Vue3与Vite的区别切入:详解Props校验与组件实例
开发语言·前端·javascript·vue.js
趣知岛12 分钟前
初识DeepSeek
开发语言·人工智能·deepseek
superman超哥14 分钟前
仓颉编译器优化揭秘:尾递归优化的原理与实践艺术
开发语言·后端·仓颉编程语言·仓颉·仓颉语言·尾递归·仓颉编译器
lkbhua莱克瓦2417 分钟前
基础-SQL-DML
开发语言·数据库·笔记·sql·mysql
独自破碎E19 分钟前
说一下消息队列有哪些模型
java·开发语言
saber_andlibert21 分钟前
【C++转GO】初阶知识
开发语言·c++·golang
小笔学长26 分钟前
Mixin 模式:灵活组合对象功能
开发语言·javascript·项目实战·前端开发·mixin模式
我是人机不吃鸭梨27 分钟前
Flutter 桌面端开发终极指南(2025版):构建跨平台企业级应用的完整解决方案
开发语言·javascript·人工智能·flutter·架构
夏幻灵33 分钟前
[从零开始学JAVA|第一篇 ] 分清关键字 方法名 字面量 标识符
java·开发语言
小徐Chao努力35 分钟前
【Langchain4j-Java AI开发】03-提示词与模板
java·开发语言·人工智能