摘要:本笔记展示了如何绘制各种各样的热图。
通过 geom_tile() 函数创建热图:
输入数据必须是长格式,每行提供一个观测值。每个观测值至少需要 3
个变量:x:在 X 轴上的位置y:在 Y 轴上的位置fill:将映射为颜色的数值
# 基本热图
# Library | |
library(ggplot2) | |
# Dummy data | |
x <- LETTERS[1:20] | |
y <- paste0("var", seq(1,20)) | |
data <- expand.grid(X=x, Y=y) | |
data$Z <- runif(400, 0, 5) | |
# Heatmap | |
ggplot(data, aes(X, Y, fill= Z)) + | |
geom_tile() |
引入库:
r options(warn=-1)
library(ggplot2)
这行代码加载了 **
ggplot2库。ggplot2** 是 R 语言中非常流行的数据可视化包,用于创建多种图形和可视化图表。生成虚拟数据:
r x <- LETTERS[1:20]
y <- paste0("var", seq(1,20))
data <- expand.grid(X=x, Y=y)
data$Z <- runif(400, 0, 5)
x <- LETTERS[1:20]:这行代码创建了一个从 A 到 T 的字母序列,存储在变量 **x** 中。y <- paste0("var", seq(1,20)):这里生成了一个字符串向量,内容是”var1”、“var2”、...、“var20”。data <- expand.grid(X=x, Y=y):使用 **expand.grid函数创建了一个数据框data,其中包含了x和y** 所有可能的组合,每种组合对应数据框的一行。data$Z <- runif(400, 0, 5):给数据框添加了一个新列 **Z**,它包含了 400 个从 0 到 5 之间的均匀分布的随机数。
绘制热图:
r ggplot(data, aes(X, Y, fill= Z)) +
geom_tile()
![]()
这部分代码使用 **
ggplot函数创建了一个热图。aes(X, Y, fill= Z)设置了图形的美学映射,其中X和Y被映射到热图的 x 轴和 y 轴,而Z** 值(颜色填充)表示热图的颜色强度。**
geom_tile()是ggplot2中用于创建热图的几何对象。它根据X和Y的值创建了一个格子图,格子的颜色由Z** 值决定,从而生成热图。
总的来说,这段代码生成了一个 20x20 的热图,其中 x 轴由字母 A 到 T
标识,y 轴由”var1” 到”var20” 标识,每个格子的颜色强度由对应的 ** Z ** 值决定。
# 调色
颜色调色板可以像在任何 ggplot2
图表中一样进行更改。以下是使用不同方法的 3 个示例:
scale_fill_gradient()用于提供调色板的极端颜色。scale_fill_distiller()用于提供ColorBrewer调色板。scale_fill_viridis()用于使用Viridis。对于连续变量,不要忘记设置 **discrete=FALSE**。
# Library | |
library(ggplot2) | |
library(hrbrthemes) |
NOTE: Either Arial Narrow or Roboto Condensed fonts are required to use these themes.
Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and
if Arial Narrow is not on your system, please see https://bit.ly/arialnarrow
# Dummy data | |
x <- LETTERS[1:20] | |
y <- paste0("var", seq(1,20)) | |
data <- expand.grid(X=x, Y=y) | |
data$Z <- runif(400, 0, 5) | |
# Give extreme colors: | |
ggplot(data, aes(X, Y, fill= Z)) + | |
geom_tile() + | |
scale_fill_gradient(low="white", high="blue") + | |
theme_ipsum() |

# Color Brewer palette | |
ggplot(data, aes(X, Y, fill= Z)) + | |
geom_tile() + | |
scale_fill_distiller(palette = "RdPu") + | |
theme_ipsum() |

# Color Brewer palette | |
library(viridis) |
Loading required package: viridisLite
ggplot(data, aes(X, Y, fill= Z)) + | |
geom_tile() + | |
scale_fill_viridis(discrete=FALSE) + | |
theme_ipsum() |

# 大规模数据
输入数据是大规模矩阵是一个常见问题,就像 volcano
数据集那样。在这种情况下,你需要使用 tidyr
包的 ** gather() ** 函数来整理这些数据,以便用 ggplot 进行可视化。
# Library | |
library(ggplot2) | |
library(tidyr) | |
library(tibble) | |
library(hrbrthemes) | |
library(dplyr) |
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
# Heatmap | |
volcano %>% | |
# Data wrangling | |
as_tibble() %>% | |
rowid_to_column(var="X") %>% | |
gather(key="Y", value="Z", -1) %>% | |
# Change Y to numeric | |
mutate(Y=as.numeric(gsub("V","",Y))) %>% | |
# Viz | |
ggplot(aes(X, Y, fill= Z)) + | |
geom_tile() + | |
theme_ipsum() + | |
theme(legend.position="none") |

volcano 数据集通过一系列的管道操作( %>% )进行处理和可视化。
as_tibble():将 volcano 数据转换为 tibble(一种现代的数据框)。rowid_to_column(var="X"):为数据添加行编号,作为新列”X”。gather(key="Y", value="Z", -1):将宽格式数据转换为长格式,为了在 ggplot 中使用。这里创建了两列:Y(列的键)和 **Z**(值),同时排除了第一列(即刚刚创建的”X” 列)。mutate(Y=as.numeric(gsub("V","",Y))):将”Y” 列的值从字符转换为数值,以便于作图。
接下来的代码用于绘制热图:
ggplot(aes(X, Y, fill= Z)) + geom_tile():创建了一个基于 **X、Y和Z** 值的热图。theme_ipsum()和theme(legend.position="none"):应用了 **hrbrthemes包的theme_ipsum** 样式,并去掉了图例,以便更干净、更专业的图形展示。
总结来说,这段代码首先将 volcano 数据集从宽格式转换为适合 ggplot2 使用的长格式,然后使用 ggplot2 创建了一个热图,同时应用了定制的主题以增强其视觉效果。
# 可交互热图
加载库:
r library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ── ✔ forcats 1.0.0 ✔ readr 2.1.4 ✔ lubridate 1.9.2 ✔ stringr 1.5.0 ✔ purrr 1.0.2 ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ── ✖ dplyr::filter() masks stats::filter() ✖ dplyr::lag() masks stats::lag() ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errorsr library(hrbrthemes)
library(viridis)
library(plotly)
Attaching package: 'plotly' The following object is masked from 'package:ggplot2': last_plot The following object is masked from 'package:stats': filter The following object is masked from 'package:graphics': layoutr library(d3heatmap)
====================== Welcome to d3heatmap version 0.9.0 Type citation('d3heatmap') for how to cite the package. Type ?d3heatmap for the main documentation. The github page is: https://github.com/talgalili/d3heatmap/ Please submit your suggestions and bug-reports at: https://github.com/talgalili/d3heatmap/issues You may ask questions at stackoverflow, use the r and d3heatmap tags: https://stackoverflow.com/questions/tagged/d3heatmap ====================== Attaching package: 'd3heatmap' The following objects are masked from 'package:base': print, save这些命令加载了需要的 R 包,包括数据处理(
tidyverse)、主题和颜色方案(hrbrthemes,viridis)、交互式图形(plotly)和热图创建(d3heatmap)。加载并处理数据:
r data <- read.table("D:/Book/Rlearn/Data.csv", header=T, sep=";")
colnames(data) <- gsub("\\.", " ", colnames(data))
head(data)
Country Pop Birth rate Mortality rate Life expectancy 1 Channel Islands 166 9.280 9.140 81.269 2 Denmark 5733 10.707 9.620 80.839 3 Estonia 1302 10.698 12.657 77.342 4 Finland 5556 10.672 9.865 81.576 5 Iceland 337 12.836 6.467 83.220 6 Ireland 4791 13.179 6.562 81.621 Infant mortality Children per woman Growth rate Population aged 65 Group 1 7.276 1.492 4.56 30 North 2 3.495 1.758 3.67 1132 North 3 2.843 1.657 -2.63 256 North 4 1.839 1.764 2.65 1212 North 5 1.752 1.893 7.83 50 North 6 2.385 1.998 8.84 682 North Continent 1 Europe 2 Europe 3 Europe 4 Europe 5 Europe 6 Europe然后,它选择了一些特定国家的数据,并对这些数据进行了进一步的处理:
r data <- data %>%
filter(Country %in% c("France", "Sweden", "Italy", "Spain", "England", "Portugal", "Greece", "Peru", "Chile", "Brazil", "Argentina", "Bolivia", "Venezuela", "Australia", "New Zealand", "Fiji", "China", "India", "Thailand", "Afghanistan", "Bangladesh", "United States of America", "Canada", "Burundi", "Angola", "Kenya", "Togo")) %>%
arrange(Country) %>%
mutate(Country = factor(Country, Country))
这里使用 **
filter来选取特定国家的数据,arrange** 对国家名称进行排序,并将国家名称转换为因子类型。将数据转换为矩阵格式:
r mat <- datarownames(mat) <- mat[,1]
mat <- mat %>% dplyr::select(-Country, -Group, -Continent)
mat <- as.matrix(mat)
这段代码先将数据集复制到变量 **
mat** 中,设置矩阵的行名为国家名称,然后移除不需要的列,并将剩余的数据转换为矩阵格式。创建热图:
r library(heatmaply)
====================== Welcome to heatmaply version 1.5.0 Type citation('heatmaply') for how to cite the package. Type ?heatmaply for the main documentation. The github page is: https://github.com/talgalili/heatmaply/ Please submit your suggestions and bug-reports at: https://github.com/talgalili/heatmaply/issues You may ask questions at stackoverflow, use the r and heatmaply tags: https://stackoverflow.com/questions/tagged/heatmaply ======================r p <- heatmaply(mat,
#dendrogram = "row",xlab = "", ylab = "",
main = "",
scale = "column",
margins = c(60,100,40,20),
grid_color = "white",
grid_width = 0.00001,
titleX = FALSE,
hide_colorbar = TRUE,
branches_lwd = 0.1,
label_names = c("Country", "Feature:", "Value"),
fontsize_row = 5, fontsize_col = 5,
labCol = colnames(mat),
labRow = rownames(mat),
heatmap_layers = theme(axis.line=element_blank())
)p
这部分代码利用 ** heatmaply ** 库来创建一个热图。它定义了热图的各种参数,包括缩放、标签、字体大小、颜色和主题等。
总的来说,这段代码从在线源加载了多变量数据集,进行了清洗和转换,然后使用 ** heatmaply ** 库创建了一个热图。这个热图展示了选定国家在不同特征上的数据,通过颜色的强弱来表示不同的数值。
# 工作环境
devtools::session_info() |
─ Session info ───────────────────────────────────────────────────────────────
setting value
version R version 4.3.1 (2023-06-16 ucrt)
os Windows 11 x64 (build 22621)
system x86_64, mingw32
ui RTerm
language (EN)
collate Chinese (Simplified)_China.utf8
ctype Chinese (Simplified)_China.utf8
tz Asia/Hong_Kong
date 2024-01-10
pandoc 3.1.9 @ C:/Users/HANWAN~1/AppData/Local/Pandoc/ (via rmarkdown)
─ Packages ───────────────────────────────────────────────────────────────────
package * version date (UTC) lib source
assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.3.2)
base64enc 0.1-3 2015-07-28 [1] CRAN (R 4.3.0)
ca 0.71.1 2020-01-24 [1] CRAN (R 4.3.2)
cachem 1.0.8 2023-05-01 [1] CRAN (R 4.3.1)
callr 3.7.3 2022-11-02 [1] CRAN (R 4.3.1)
cli 3.6.1 2023-03-23 [1] CRAN (R 4.3.1)
codetools 0.2-19 2023-02-01 [2] CRAN (R 4.3.1)
colorspace 2.1-0 2023-01-23 [1] CRAN (R 4.3.1)
crayon 1.5.2 2022-09-29 [1] CRAN (R 4.3.1)
crosstalk 1.2.0 2021-11-04 [1] CRAN (R 4.3.1)
crul 1.4.0 2023-05-17 [1] CRAN (R 4.3.2)
curl 5.0.1 2023-06-07 [1] CRAN (R 4.3.1)
d3heatmap * 0.9.0 2023-11-13 [1] Github (rstudio/d3heatmap@c8a3c64)
data.table 1.14.8 2023-02-17 [1] CRAN (R 4.3.2)
dendextend 1.17.1 2023-03-25 [1] CRAN (R 4.3.2)
devtools 2.4.5 2022-10-11 [1] CRAN (R 4.3.2)
digest 0.6.33 2023-07-07 [1] CRAN (R 4.3.1)
dplyr * 1.1.3 2023-09-03 [1] CRAN (R 4.3.2)
ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.3.1)
evaluate 0.21 2023-05-05 [1] CRAN (R 4.3.1)
extrafont 0.19 2023-01-18 [1] CRAN (R 4.3.1)
extrafontdb 1.0 2012-06-11 [1] CRAN (R 4.3.1)
fansi 1.0.4 2023-01-22 [1] CRAN (R 4.3.1)
farver 2.1.1 2022-07-06 [1] CRAN (R 4.3.1)
fastmap 1.1.1 2023-02-24 [1] CRAN (R 4.3.1)
fontBitstreamVera 0.1.1 2017-02-01 [1] CRAN (R 4.3.1)
fontLiberation 0.1.0 2016-10-15 [1] CRAN (R 4.3.1)
fontquiver 0.2.1 2017-02-01 [1] CRAN (R 4.3.2)
forcats * 1.0.0 2023-01-29 [1] CRAN (R 4.3.1)
foreach 1.5.2 2022-02-02 [1] CRAN (R 4.3.2)
fs 1.6.3 2023-07-20 [1] CRAN (R 4.3.1)
gdtools 0.3.4 2023-10-15 [1] CRAN (R 4.3.2)
generics 0.1.3 2022-07-05 [1] CRAN (R 4.3.1)
gfonts 0.2.0 2023-01-08 [1] CRAN (R 4.3.2)
ggplot2 * 3.4.4 2023-10-12 [1] CRAN (R 4.3.2)
glue 1.6.2 2022-02-24 [1] CRAN (R 4.3.1)
gridExtra 2.3 2017-09-09 [1] CRAN (R 4.3.1)
gtable 0.3.3 2023-03-21 [1] CRAN (R 4.3.1)
heatmaply * 1.5.0 2023-10-06 [1] CRAN (R 4.3.2)
hms 1.1.3 2023-03-21 [1] CRAN (R 4.3.1)
hrbrthemes * 0.8.0 2020-03-06 [1] CRAN (R 4.3.2)
htmltools 0.5.5 2023-03-23 [1] CRAN (R 4.3.1)
htmlwidgets 1.6.2 2023-03-17 [1] CRAN (R 4.3.1)
httpcode 0.3.0 2020-04-10 [1] CRAN (R 4.3.2)
httpuv 1.6.11 2023-05-11 [1] CRAN (R 4.3.1)
httr 1.4.6 2023-05-08 [1] CRAN (R 4.3.1)
iterators 1.0.14 2022-02-05 [1] CRAN (R 4.3.2)
jsonlite 1.8.7 2023-06-29 [1] CRAN (R 4.3.1)
knitr 1.45 2023-10-30 [1] CRAN (R 4.3.2)
labeling 0.4.2 2020-10-20 [1] CRAN (R 4.3.0)
later 1.3.1 2023-05-02 [1] CRAN (R 4.3.1)
lazyeval 0.2.2 2019-03-15 [1] CRAN (R 4.3.1)
lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.3.1)
lubridate * 1.9.2 2023-02-10 [1] CRAN (R 4.3.1)
magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.3.1)
memoise 2.0.1 2021-11-26 [1] CRAN (R 4.3.1)
mime 0.12 2021-09-28 [1] CRAN (R 4.3.0)
miniUI 0.1.1.1 2018-05-18 [1] CRAN (R 4.3.1)
munsell 0.5.0 2018-06-12 [1] CRAN (R 4.3.1)
pillar 1.9.0 2023-03-22 [1] CRAN (R 4.3.1)
pkgbuild 1.4.2 2023-06-26 [1] CRAN (R 4.3.1)
pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.3.1)
pkgload 1.3.2.1 2023-07-08 [1] CRAN (R 4.3.1)
plotly * 4.10.2 2023-06-03 [1] CRAN (R 4.3.1)
plyr 1.8.8 2022-11-11 [1] CRAN (R 4.3.1)
png 0.1-8 2022-11-29 [1] CRAN (R 4.3.0)
prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.3.1)
processx 3.8.2 2023-06-30 [1] CRAN (R 4.3.1)
profvis 0.3.8 2023-05-02 [1] CRAN (R 4.3.1)
promises 1.2.0.1 2021-02-11 [1] CRAN (R 4.3.1)
ps 1.7.5 2023-04-18 [1] CRAN (R 4.3.1)
purrr * 1.0.2 2023-08-10 [1] CRAN (R 4.3.2)
R6 2.5.1 2021-08-19 [1] CRAN (R 4.3.1)
RColorBrewer 1.1-3 2022-04-03 [1] CRAN (R 4.3.0)
Rcpp 1.0.11 2023-07-06 [1] CRAN (R 4.3.1)
readr * 2.1.4 2023-02-10 [1] CRAN (R 4.3.1)
registry 0.5-1 2019-03-05 [1] CRAN (R 4.3.1)
remotes 2.4.2.1 2023-07-18 [1] CRAN (R 4.3.1)
reshape2 1.4.4 2020-04-09 [1] CRAN (R 4.3.2)
rlang 1.1.1 2023-04-28 [1] CRAN (R 4.3.1)
rmarkdown 2.23 2023-07-01 [1] CRAN (R 4.3.1)
rstudioapi 0.15.0 2023-07-07 [1] CRAN (R 4.3.1)
Rttf2pt1 1.3.12 2023-01-22 [1] CRAN (R 4.3.1)
scales 1.2.1 2022-08-20 [1] CRAN (R 4.3.1)
seriation 1.5.1 2023-07-20 [1] CRAN (R 4.3.2)
sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.3.1)
shiny 1.7.4.1 2023-07-06 [1] CRAN (R 4.3.1)
stringi 1.7.12 2023-01-11 [1] CRAN (R 4.3.0)
stringr * 1.5.0 2022-12-02 [1] CRAN (R 4.3.1)
systemfonts 1.0.4 2022-02-11 [1] CRAN (R 4.3.1)
tibble * 3.2.1 2023-03-20 [1] CRAN (R 4.3.1)
tidyr * 1.3.0 2023-01-24 [1] CRAN (R 4.3.1)
tidyselect 1.2.0 2022-10-10 [1] CRAN (R 4.3.1)
tidyverse * 2.0.0 2023-02-22 [1] CRAN (R 4.3.1)
timechange 0.2.0 2023-01-11 [1] CRAN (R 4.3.1)
TSP 1.2-4 2023-04-04 [1] CRAN (R 4.3.2)
tzdb 0.4.0 2023-05-12 [1] CRAN (R 4.3.1)
urlchecker 1.0.1 2021-11-30 [1] CRAN (R 4.3.1)
usethis 2.2.2 2023-07-06 [1] CRAN (R 4.3.1)
utf8 1.2.3 2023-01-31 [1] CRAN (R 4.3.1)
vctrs 0.6.3 2023-06-14 [1] CRAN (R 4.3.1)
viridis * 0.6.4 2023-07-22 [1] CRAN (R 4.3.2)
viridisLite * 0.4.2 2023-05-02 [1] CRAN (R 4.3.1)
webshot 0.5.5 2023-06-26 [1] CRAN (R 4.3.2)
withr 2.5.0 2022-03-03 [1] CRAN (R 4.3.1)
xfun 0.39 2023-04-20 [1] CRAN (R 4.3.1)
xtable 1.8-4 2019-04-21 [1] CRAN (R 4.3.1)
yaml 2.3.7 2023-01-23 [1] CRAN (R 4.3.0)
[1] C:/Users/Han Wang/AppData/Local/R/win-library/4.3
[2] C:/Program Files/R/R-4.3.1/library
────────────────────────────────────────────────────────────────────────────── 