Heatmap Plot with ggplot2 【R】

前言

热图,作为生命科学领域常用的数据展示方式,R中已经有很多专门画热图的工具,比如heatmapComplexheatmap等等,但是,有些特殊的需求,还是要用ggplot2实现,最近有这需求,正好整理一下😑

构建数据

library(reshape)
# Data 
set.seed(8)
m <- matrix(round(rnorm(200), 2), 10, 10)
colnames(m) <- paste("Col", 1:10)
rownames(m) <- paste("Row", 1:10)

# Transform the matrix in long format
df <- melt(m)
colnames(df) <- c("x", "y", "value")
head(df)
XYValue
Row 1Col 1-0.08
Row 2Col 10.84
Row 3Col 1-0.46
Row 4Col 1-0.55
Row 5Col 10.74

使用 geom_tile绘制热图

使用geom_tile就可以绘制热图,颜色用value填充

# install.packages("ggplot2")
library(ggplot2)

ggplot(df, aes(x = x, y = y, fill = value)) +
  geom_tile()

在这里插入图片描述

变方

但是,会发现上边的图画出来,每个格子不是方方正正的,所以调整一下,使用:cood_fixed

ggplot(df, aes(x = x, y = y, fill = value)) +
  geom_tile() +
  coord_fixed() 

在这里插入图片描述

给格子加个边框

如果喜欢,可以给每个格子加上特定颜色的边框,比如白色的

ggplot(df, aes(x = x, y = y, fill = value)) +
  geom_tile(color = "white",
            lwd = 1.5,
            linetype = 1) +
  coord_fixed() 

在这里插入图片描述

显示数据

可以像使用其他工具那样,把颜色代表的数值显示出来,使用:geom_text

ggplot(df, aes(x = x, y = y, fill = value)) +
  geom_tile(color = "black") +
  geom_text(aes(label = value), color = "white", size = 4) +
  coord_fixed() 

在这里插入图片描述

颜色

更改颜色,大致有三种途径:scale_fill_gradientscale_fill_gradient2scale_fill_gradientn,下面

使用scale_fill_gradient

通过设置低值和高值对应的颜色来更改热图的颜色:

ggplot(df, aes(x = x, y = y, fill = value)) +
  geom_tile(color = "black") +
  scale_fill_gradient(low = "white", high = "red") +
  coord_fixed() 

在这里插入图片描述

使用scale_fill_gradient2

这个2吧,就是加了一个中值的设置:

ggplot(df, aes(x = x, y = y, fill = value)) +
  geom_tile(color = "black") +
  scale_fill_gradient2(low = "#075AFF",
                       mid = "#FFFFCC",
                       high = "#FF0000") +
  coord_fixed() 

在这里插入图片描述

使用scale_fill_gradientn

这个吧,就得需要自己选颜色了,可以选N多个,下面的例子就是用了调色板的20个颜色:

ggplot(df, aes(x = x, y = y, fill = value)) +
  geom_tile(color = "black") +
  scale_fill_gradientn(colors = hcl.colors(20, "RdYlGn")) +
  coord_fixed() 

在这里插入图片描述

图例

关于图里的修改,其实和画其他的图一样一样的…直接上代码了

长宽

ggplot(df, aes(x = x, y = y, fill = value)) +
  geom_tile(color = "black") +
  coord_fixed() +
  guides(fill = guide_colourbar(barwidth = 0.5,
                                barheight = 20)) 

在这里插入图片描述

Title

ggplot(df, aes(x = x, y = y, fill = value)) +
  geom_tile(color = "black") +
  coord_fixed() +
  guides(fill = guide_colourbar(title = "Title")) 

在这里插入图片描述)

标签

ggplot(df, aes(x = x, y = y, fill = value)) +
  geom_tile(color = "black") +
  coord_fixed() +
  guides(fill = guide_colourbar(label = FALSE,
                                ticks = FALSE)) 

在这里插入图片描述

移除

ggplot(df, aes(x = x, y = y, fill = value)) +
  geom_tile(color = "black") +
  coord_fixed() +
  theme(legend.position = "none") 

就不放图了,就是图例没有了😑

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值