gzh原文點我,歡迎同時關注
在數據分析過程中,我們經常碰到想要直觀獲取兩個或多個變量間相互關系的情況。本期就介紹5種常用于展示變量間相互關系的圖形,并給出常用示例圖形及對應的完整可復現代碼
1、散點圖
散點圖用于表示2個數字變量之間的關系。散點圖中每個數據點,其第一個變量的值在X軸上表示,第二個在Y軸上表示
常見問題&處理方法
- 數據重疊(過度繪制)
- 縮小散點大小
- 調整散點透明度
- 對數據取樣(less is more)
- 分組
- 高亮其中某些分組
- 分面(faceting):比如有3個組,則分成3副圖,每幅圖高亮其中一個組
- 抖動處理(Jittering):如果數據的其中一個軸是定量(比如確定x=1,則可對這些數據點在一定范圍內增加或減少一個隨機值,從而避免數據重疊在一起。
示例:
散點圖包含多種分類,此處簡單舉例

library(ggplot2)
d1 <- data.frame(x=seq(1,100), y=rnorm(100), name="No trend")
d2 <- d1 %>% mutate(y=x*10 + rnorm(100,sd=60)) %>% mutate(name="Linear relationship")
d3 <- d1 %>% mutate(y=x^2 + rnorm(100,sd=140)) %>% mutate(name="Square")
d4 <- data.frame( x=seq(1,10,0.1), y=sin(seq(1,10,0.1)) + rnorm(91,sd=0.6)) %>% mutate(name="Sin")
don <- do.call(rbind, list(d1, d2, d3, d4))
p1 = don %>%
ggplot(aes(x=x, y=y)) +
geom_point(color="#69b3a2", alpha=0.8) +
theme_ipsum() +
geom_smooth(,method=loess , color="red", fill="#69b3a2", se=TRUE,size=0.5) +
facet_wrap(~name, scale="free")
散點圖還可參考:
一起學畫圖:散點圖(1)— 基礎散點圖 — R-ggplot2復現Nature文章散點圖
3、散點連接圖
在散點圖的基礎上,增加了數據點之間的連線;或者說,在折線圖上,增加了數據點,能夠補充數據點的的信息
常見問題&處理方法
- 應避免使用雙軸(dual-axis)
- 數據重疊(過度繪制)——見1
示例:

library(ggplot2)
library(dplyr)
library(babynames)
library(ggrepel)
library(tidyr)
# 數據
data <- babynames %>%
filter(name %in% c("Ashley", "Amanda")) %>%
filter(sex=="F") %>%
filter(year>1970) %>%
select(year, name, n) %>%
spread(key = name, value=n, -1)
# 篩選被標注的數據
tmp_date <- data %>% sample_frac(0.3)
csp = data %>%
ggplot(aes(x=Amanda, y=Ashley, label=year)) +
geom_point(color="#69b3a2") +
geom_text_repel(data=tmp_date) +
geom_segment(color="#69b3a2",
aes(xend=c(tail(Amanda, n=-1), NA),
yend=c(tail(Ashley, n=-1), NA)),
arrow=arrow(length=unit(0.3,"cm"))
) +
theme_ipsum()
csp
3、熱力圖
熱力圖通過圖形來表示數據,通過圖形的不同顏色及顏色深淺反映兩個變量之間的相關數據的大小,熱力圖非常適合展現數據的總體視圖。
常見問題&處理方法
- 通常需要對數據進行歸一化處理(normalize)
- 推薦使用聚類分析,將聚類情況相近的數據統(tǒng)一放置,以獲得更好的圖形效果
- 注意配色,否則圖表信息無法充分展示
示例:
R中有很多可以制作熱力圖的包和軟件,如R本身提供的heatmap()函數,ggplot2中的geom_tile(),lattice中的levelplot(),pheatmap、ggcor、 ComplexHeastmap等

#BiocManager::install("ComplexHeatmap")
library(ComplexHeatmap)
test = matrix(rnorm(200), 20, 10)
test[1:10, seq(1, 10, 2)] = test[1:10, seq(1, 10, 2)] + 3
test[11:20, seq(2, 10, 2)] = test[11:20, seq(2, 10, 2)] + 2
test[15:20, seq(2, 10, 2)] = test[15:20, seq(2, 10, 2)] + 4
colnames(test) = paste("Test", 1:10, sep = "")
rownames(test) = paste("Gene", 1:20, sep = "")
annotation_col = data.frame(
CellType = factor(rep(c("CT1", "CT2"), 5)),
Time = 1:5
)
rownames(annotation_col) = paste("Test", 1:10, sep = "")
annotation_row = data.frame(
GeneClass = factor(rep(c("Path1", "Path2", "Path3"), c(10, 4, 6)))
)
rownames(annotation_row) = paste("Gene", 1:20, sep = "")
ann_colors = list(
Time = c("white", "firebrick"),
CellType = c(CT1 = "#1B9E77", CT2 = "#D95F02"),
GeneClass = c(Path1 = "#7570B3", Path2 = "#E7298A", Path3 = "#66A61E")
)
chp = pheatmap(test, annotation_col = annotation_col, annotation_row = annotation_row,
annotation_colors = ann_colors)
chp

該圖代碼、數據請見:ComplexHeatmap熱圖繪制
4、氣泡圖
氣泡圖在散點圖基礎上增加了第三個維度,該維度通常是一個額外的數字變量,其值通過氣泡的大小來表示
常見問題&處理方法
使用的是氣泡的面積大小作為度量標準,而非氣泡直徑長度
如果樣本量太多,同樣會出現數據重疊(過度繪制)問題——見1
應該設置氣泡面積大小的圖例
示例:

該圖代碼、數據請見:一起學畫圖:氣泡圖—常用于富集分析
5、相關圖
相關圖(相關矩陣)用來分析矩陣中每一對數字變量之間的關系。每對變量之間的相關關系通過散點圖或代表相關關系的符號(如氣泡、直線、數字等)進行可視化。對角線通常使用直方圖或密度圖表示每個變量的分布
常見問題&處理方法
- 控制變量個數,變量太多會降低可讀性
示例:
可以實現相關圖的包,包括GGally的ggpairs,corrgram,ellipse,以及car包中的scatterplotMatrix() 以及原生plot()都可以實現類似的效果ggpairs官方文檔:https://ggobi.github.io/ggally/articles/ggpairs.html

library(GGally)
library(ggplot2)
data(flea)
ggpairs(flea, columns = 2:4, ggplot2::aes(colour=species))
data(tips, package = "reshape")
rla = ggpairs(
tips[, c(1, 3, 4, 2)],
upper = list(continuous = "density", combo = "box_no_facet"),
lower = list(continuous = "points", combo = "dot_no_facet"),
aes(colour=sex)
) +
theme_update()
rla

該圖代碼、數據請見:ggcor相關性熱圖繪制

#install.packages("ellipse")
library(ellipse)
library(RColorBrewer)
data <- cor(mtcars)
my_colors <- brewer.pal(5, "Spectral")
my_colors <- colorRampPalette(my_colors)(100)
ord <- order(data[1, ])
data_ord <- data[ord, ord]
plotcorr(data_ord , col=my_colors[data_ord*50+50] , mar=c(1,1,1,1)

往期推薦
一起學畫圖:棒棒糖圖 / 啞鈴圖—基本示例與優(yōu)化
一起學畫圖:散點圖+邊緣分布統(tǒng)計圖形
一起學畫圖:氣泡圖—常用于富集分析
一起學畫圖:散點圖(1)— 基礎散點圖 — R-ggplot2復現Nature文章散點圖
關注微信公眾號 生信小書生,查看更多精彩內容