ggplot2中的Heatmap教程及实例

1,172 阅读3分钟

简介

在这篇文章中,我们将带你了解使用ggplot2的Heatmap教程,ggplot2是一个常用的包,用于在R中创建漂亮的可视化。我们将首先了解语法,然后看到不同类型的使用ggplot2的Heatmap例子。

ggplot2中热图的语法

在ggplot2中生成热图的最小语法是

ggplot(, mapping = aes())+ geom_tile()

我们还可以通过添加更多的图层来定制热图,如主题、实验室等。

R语言中使用ggplot2绘制热图的例子

准备数据集

在ggplot2中的所有热图例子中,我们将使用 "heart.csv "数据集。

转换成一个叫做df的数据框

In[2]:

df <- read.table("heart.csv",header=TRUE,sep=',') 
head(df)

输出[2]:

ï...年龄性别cptrestbpscholfbs补钙タルチャー准确老峰坡度ca峰值目标
63131452331015002.30011
37121302500118703.50021
41011302040017201.42021
56111202360117800.82021
57001203540116310.62021
57101401920114800.41011

我们使用cor()函数来计算两个向量之间的相关性,这又为我们生成了一个数据集的相关矩阵

In[3]:

cormat <- round(cor(df),2)
head(cormat)

Out[3]:

ï...年龄性别cptrestbpscholfbs补钙ǞǞǞ老峰坡度cathal目标
ï...年龄1.00-0.10-0.070.280.210.12-0.12-0.400.100.21-0.170.280.07-0.23
性别-0.101.00-0.05-0.06-0.200.05-0.06-0.040.140.10-0.030.120.21-0.28
cp-0.07-0.051.000.05-0.080.090.040.30-0.39-0.150.12-0.18-0.160.43
栈桥0.28-0.060.051.000.120.18-0.11-0.050.070.19-0.120.100.06-0.14
胆汁0.21-0.20-0.080.121.000.01-0.15-0.010.070.050.000.070.10-0.09
fbs0.120.050.090.180.011.00-0.08-0.010.030.01-0.060.14-0.03-0.03

使用reshape2库,我们随后可以使用melt函数将我们的数据框转换成长格式,并拉伸数据框。这个转换后的数据集将在随后的所有例子中使用。

In[3]:

library(reshape2)
melted_cormat <- melt(cormat)
head(melted_cormat)

Out[3]:

Var1Var2价值
ï...年龄ï...年龄1.00
性别ï...年龄-0.10
cpï...年龄-0.07
trestbpsï...年龄0.28
cholï...年龄0.21
脂肪细胞ï...年龄0.12

加载ggplot2

首先,让我们加载ggplot2库,如下图所示:

在[4]中。

library(ggplot2)

例1:ggplot2中的基本热图

ggplot2中的基本热力图是通过使用geom_tile()层创建的,如下面的例子所示。

在[5]中。

ggplot(data = melted_cormat, aes(x=Var1, y=Var2, fill=value)) + 
  geom_tile()

Out[5]:

ggplot2 heatmap example

例2:热力图中的梯度填充(Scale_fill_gradient)功能

Scale_fill_gradient用于在热力图中添加颜色梯度。它有两个参数,即高和低,用于指定梯度比例。

In[6]:

ggplot(data=melted_cormat,aes(x = Var1, y = Var2, fill = value))+
geom_tile()+scale_fill_gradient(high = "red", low = "white")

输出[6]:

ggplot2 heatmap example

例3:给热力图添加标题

让我们给我们的热力图添加一个标题,给它更多的描述。我们可以通过使用ggtitle()来做到这一点。

在[7]中:

ggplot(data=melted_cormat,aes(x = Var1, y = Var2, fill = value))+
geom_tile()+scale_fill_gradient(high = "green", low = "white") +
ggtitle("Heart dataset")+ theme(plot.title = element_text(size = 20, face = "bold"))

Out[7]:

ggplot2 heatmap example

例4:用热力图可视化相关关系

这里的每个单元格都描述了相交变量之间的关系,比如说线性相关。这个图可以帮助分析人员了解变量之间的关系,同时描述性或预测性的统计模型。

In[8]:

ggplot(data=melted_cormat,aes(x = Var1, y = Var2, fill = value))+
geom_tile()+scale_fill_gradient(high = "green", low = "red")+
ggtitle("Heart dataset")+ 
theme(plot.title = element_text(size = 20, face = "bold"))+
geom_text(aes(label = round(value, 3)))+
scale_fill_continuous(low = "red", high = "lightgreen")

Out[8]:

例5:六边形图

添加六边形绑定 函数geom_hex()产生一个带有六边形绑定的散点图。hexbin R包是进行六边形划分的必要条件。如果你没有这个包,可以使用下面的R代码,用require(hexbin)来安装。

在[9]:

# Default plot 
ggplot(df,aes(chol,thalach))+ geom_hex()
# Change the number of bins
ggplot(df,aes(chol,thalach)) + geom_hex(bins = 10)

输出[9]:

例6:添加2d bin计数的热图

函数geom_bin2d()产生一个带有矩形仓的散点图。每个bin中的观测值被计算出来,并以热图的形式显示。

In[10]:

# Default plot 
ggplot(df,aes(chol,thalach)) + geom_bin2d()
# Change the number of bins
ggplot(df,aes(chol,thalach)) + geom_bin2d(bins = 15)

Out[10]: