用的数据集还是mtcars,相关性数据在上一篇介绍,这里不再重复运行。
附上一篇链接:R语言绘制相关性图和矩阵https://blog.csdn.net/weixin_46714992/article/details/133636212?spm=1001.2014.3001.5501
本篇任务:绘制热点图,移除聚类数图dendrogram,并自己调整颜色盘。
1、首先加载需要用到的包,用来调颜色和绘图。
# Task 3: heatmap
# use the mtcars data for this task.
# the task consists of the following steps:
# a) use a heatmap to visualize the data
# b) normalize the heatmap by column, and remove the dendrogram
# c) customize coloring by colorRampPalette. select your own color palette.
install.packages('RColorBrewer')
install.packages('gplots')
library(RColorBrewer)
library(gplots)
2、设置颜色,调整每个颜色对应的相关性区间,如下面设置的是pink对应-1到0,yellow对应0.01到0.8,可自行调整。
my_palette <- colorRampPalette(c("pink", "yellow", "dodgerblue"))(n = 299)
col_breaks = c(seq(-1,0,length=100), # for pink
seq(0.01,0.8,length=100), # for yellow
seq(0.81,1,length=100)) # for blue
3、绘制热点图,并移除聚类数图dendrogram,想保留dendrogram的话把none那一块去掉就好。
# we'll use heatmap.2 from gplots library for an enhanced view:
heatmap.2(res,
cellnote = round(res,2), # same data set for cell labels, round to 2 decimal points
main = "Correlation", # 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(5,5), # define margins around plot
col=my_palette, # use on color palette defined earlier
breaks=col_breaks, # enable color transition at specified limits
dendrogram = "none") # remove dendrogram
4、输出结果图。