当前位置:   article > 正文

【SCI绘图】【热力图系列1 R】多特征相关性分析热力图R语言实现_特征与标签之间的相关性分析,并以热力图的形式展示出来

特征与标签之间的相关性分析,并以热力图的形式展示出来

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

本期分享: 

SCI绘图】【热力图系列1 R】多特征相关性分析热力图R语言实现

1.环境准备

  1. library(gplots)
  2. library(RColorBrewer)

2.数据示例

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

3.绘图展示

  1. #########################################################
  2. ### customizing and plotting heatmap
  3. #########################################################
  4. # creates a own color palette from red to green
  5. my_palette <- colorRampPalette(c("red", "yellow", "green"))(n = 299)
  6. # (optional) defines the color breaks manually for a "skewed" color transition
  7. col_breaks = c(seq(-1,0,length=100), # for red
  8. seq(0.01,0.7,length=100), # for yellow
  9. seq(0.71,1,length=100)) # for green
  10. # creates a 5 x 5 inch image
  11. png("h1_simple.png",
  12. width = 5*300, # 5 x 300 pixels
  13. height = 5*300,
  14. res = 300, # 300 pixels per inch
  15. pointsize = 8) # smaller font size
  16. heatmap.2(mat_data,
  17. cellnote = mat_data, # same data set for cell labels
  18. main = "Correlation", # heat map title
  19. notecol="black", # change font color of cell labels to black
  20. density.info="none", # turns off density plot inside color legend
  21. trace="none", # turns off trace lines inside the heat map
  22. margins =c(12,9), # widens margins around plot
  23. col=my_palette, # use on color palette defined earlier
  24. breaks=col_breaks, # enable color transition at specified limits
  25. dendrogram="row", # only draw a row dendrogram
  26. Colv="NA") # turn off column clustering
  27. ##############################################################################
  28. # NOTE
  29. ##############################################################################
  30. # The color breaks above will yield a warning
  31. # "...unsorted 'breaks' will be sorted before use" since they contain
  32. # (due to the negative numbers). To avoid this warning, you can change the
  33. # manual breaks to:
  34. #
  35. # col_breaks = c(seq(0,1,length=100), # for red
  36. # seq(1.01,1.7,length=100), # for yellow
  37. # seq(1.71,2,length=100)) # for green
  38. #
  39. # However, the problem is then that our heatmap contains negative values
  40. # which will then not be colored correctly. Remember that you don't need to
  41. # provide manual color breaks at all, this is entirely optional.
  42. ##############################################################################
  43. dev.off()

完整代码:

  1. library(gplots)
  2. library(RColorBrewer)
  3. #########################################################
  4. ### reading in data and transform it to matrix format
  5. #########################################################
  6. data <- read.csv("dataset.csv", comment.char="#")
  7. rnames <- data[,1] # assign labels in column 1 to "rnames"
  8. mat_data <- data.matrix(data[,2:ncol(data)]) # transform column 2-5 into a matrix
  9. rownames(mat_data) <- rnames # assign row names
  10. #########################################################
  11. ### customizing and plotting heatmap
  12. #########################################################
  13. # creates a own color palette from red to green
  14. my_palette <- colorRampPalette(c("red", "yellow", "green"))(n = 299)
  15. # (optional) defines the color breaks manually for a "skewed" color transition
  16. col_breaks = c(seq(-1,0,length=100), # for red
  17. seq(0.01,0.7,length=100), # for yellow
  18. seq(0.71,1,length=100)) # for green
  19. # creates a 5 x 5 inch image
  20. png("h1_simple.png",
  21. width = 5*300, # 5 x 300 pixels
  22. height = 5*300,
  23. res = 300, # 300 pixels per inch
  24. pointsize = 8) # smaller font size
  25. heatmap.2(mat_data,
  26. cellnote = mat_data, # same data set for cell labels
  27. main = "Correlation", # heat map title
  28. notecol="black", # change font color of cell labels to black
  29. density.info="none", # turns off density plot inside color legend
  30. trace="none", # turns off trace lines inside the heat map
  31. margins =c(12,9), # widens margins around plot
  32. col=my_palette, # use on color palette defined earlier
  33. breaks=col_breaks, # enable color transition at specified limits
  34. dendrogram="row", # only draw a row dendrogram
  35. Colv="NA") # turn off column clustering
  36. ##############################################################################
  37. # NOTE
  38. ##############################################################################
  39. # The color breaks above will yield a warning
  40. # "...unsorted 'breaks' will be sorted before use" since they contain
  41. # (due to the negative numbers). To avoid this warning, you can change the
  42. # manual breaks to:
  43. #
  44. # col_breaks = c(seq(0,1,length=100), # for red
  45. # seq(1.01,1.7,length=100), # for yellow
  46. # seq(1.71,2,length=100)) # for green
  47. #
  48. # However, the problem is then that our heatmap contains negative values
  49. # which will then not be colored correctly. Remember that you don't need to
  50. # provide manual color breaks at all, this is entirely optional.
  51. ##############################################################################
  52. dev.off()

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/675357
推荐阅读
相关标签
  

闽ICP备14008679号