r语言绘制热点图

用的数据集还是mtcars,相关性数据在上一篇介绍,这里不再重复运行。

附上一篇链接:R语言绘制相关性图和矩阵icon-default.png?t=N7T8https://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、输出结果图。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蛋肠加蛋不加香菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值