如何使用 ggplot2 在 r 中创建热图


本教程介绍如何使用 ggplot2 在 R 中创建热图。

示例:在 R 中创建热图

为了创建热图,我们将使用内置的 R 数据集mtcars

 #view first six rows of mtcars
head(mtcars)

# mpg cyl disp hp drat wt qsec vs am gear carb
#Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
#Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
#Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
#Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
#Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
#Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1

目前mtcars是宽格式,但我们需要将其混合成长格式才能创建热图。

 #load reshape2 package to use melt() function
library(reshape2)

#melt mtcars into long format
melt_mtcars <- melt(mtcars)

#add column for car name
melt_mtcars$car <- rep(row.names(mtcars), 11)

#view first six rows of melt_mtcars
head(melt_mtcars)

# variable value char
#1 mpg 21.0 Mazda RX4
#2 mpg 21.0 Mazda RX4 Wag
#3 mpg 22.8 Datsun 710
#4 mpg 21.4 Hornet 4 Drive
#5 mpg 18.7 Hornet Sportabout
#6 mpg 18.1 Valiant

我们可以使用以下代码在 ggplot2 中创建热图:

 library(ggplot2)

ggplot(melt_mtcars, aes(variable, char)) +
  geom_tile(aes(fill = value), color = "white") +
  scale_fill_gradient(low = "white", high = "red")

不幸的是,由于disp的值远大于数据框中所有其他变量的值,因此很难看到其他变量的颜色变化。

解决这个问题的一种方法是使用scales()包中的rescale()函数和plyr()包中的ddply()函数将每个变量的值从0重新缩放到1:

 #load libraries
library(plyr)
library(scales)

#rescale values for all variables in melted data frame
melt_mtcars <- ddply(melt_mtcars, .(variable), transform, rescale = rescale(value))

#create heatmap using rescaled values
ggplot(melt_mtcars, aes(variable, char)) +
  geom_tile(aes(fill = rescale), color = "white") +
  scale_fill_gradient(low = "white", high = "red")

我们还可以通过更改scale_fill_gradient()参数中使用的颜色来更改热图颜色:

 #create heatmap using blue color scale
ggplot(melt_mtcars, aes(variable, char)) +
  geom_tile(aes(fill = rescale), color = "white") +
  scale_fill_gradient(low = "white", high = "steelblue")

请注意,热图当前按汽车名称分类。我们可以使用以下代码根据mpg等变量之一的值对热图进行排序:

 #define car name as a new column, then order by mpg descending
mtcars$car <- row.names(mtcars)
mtcars$car <- with(mtcars, reorder(car, mpg))

#melt mtcars into long format
melt_mtcars <- melt(mtcars)

#rescale values for all variables in melted data frame
melt_mtcars <- ddply(melt_mtcars, .(variable), transform, rescale = rescale(value))

#create heatmap using rescaled values
ggplot(melt_mtcars, aes(variable, char)) +
  geom_tile(aes(fill = rescale), color = "white") +
  scale_fill_gradient(low = "white", high = "steelblue")

要通过增加mpg对热图进行排序,只需在 reorder() 参数中使用-mpg

 #define car name as a new column, then order by mpg descending
mtcars$car <- row.names(mtcars)
mtcars$car <- with(mtcars, reorder(car, -mpg ))

#melt mtcars into long format
melt_mtcars <- melt(mtcars)

#rescale values for all variables in melted data frame
melt_mtcars <- ddply(melt_mtcars, .(variable), transform, rescale = rescale(value))

#create heatmap using rescaled values
ggplot(melt_mtcars, aes(variable, char)) +
  geom_tile(aes(fill = rescale), color = "white") +
  scale_fill_gradient(low = "white", high = "steelblue")

最后,如果我们不喜欢使用 labs() 和 theme() 参数的外观,我们可以删除 x 和 y 轴标签以及图例:

 #create heatmap with no axis labels or legend
ggplot(melt_mtcars, aes(variable, char)) +
  geom_tile(aes(fill = rescale), color = "white") +
  scale_fill_gradient(low = "white", high = "steelblue") +
  labs(x = "", y = "") +
  theme(legend.position = "none")

添加评论

您的电子邮箱地址不会被公开。 必填项已用*标注