前情提要

并且基于pbmc3k的數(shù)據(jù),使用Dotplot進行可視化和簡單調整。這期一起來了解一下,使用Dotplot參數(shù)調整美化結果,以及基于ggplot2進行可視化

Dotplot可視化及美化
示例數(shù)據(jù)為pbmc-3k的注釋分群后的數(shù)據(jù),使用FindAllMarkers
查找并獲取top5的Marker基因進行可視化
#top5 marker基因獲取
pbmc.markers <- FindAllMarkers(pbmc, only.pos = TRUE, min.pct = 0.25, logfc.threshold = 0.25, verbose = FALSE)
top5 = pbmc.markers %>% group_by(cluster) %>% top_n(n = 5, wt = avg_log2FC)
g = unique(top5$gene)
默認參數(shù)繪圖及調整美化
如果直接使用默認參數(shù),不調節(jié)參數(shù)展示的情況
DotPlot(pbmc, features = g)

可以看到橫坐標中features展示列全部擠在了一起,不便于閱讀。因為Dotplot繪圖是基于ggplot2的語法結構的,所以可以調用ggplot2包里面的函數(shù)去傾斜展示基因,或者將基因和細胞亞群調換位置的方式進行調整
1. 調節(jié)features的排列角度
使用RotatedAxis()
函數(shù)將坐標軸標簽旋轉,讓features列展示的基因,由最開始的平鋪變?yōu)閮A斜,方便閱讀
DotPlot(pbmc, features = g) + RotatedAxis()

不過RotatedAxis()
函數(shù)里面角度是確定了的,如果需要根據(jù)自己的需求調整角度可以使用theme()函數(shù)
來調整x軸的具體傾斜角度

DotPlot(pbmc, features = g) +
theme(axis.text.x=element_text(angle=70,hjust = 1))

2.將features和identity調換位置
- 使用
coord_flip()函數(shù)
翻轉坐標軸,使得圖形的布局更加合理。 - 使用
RotatedAxis()函數(shù)
旋轉坐標軸標簽,以便更好地展示和閱讀。
DotPlot(pbmc, features = g) + coord_flip()+ RotatedAxis()

3. 分組展示Marker基因
- 使用
split函數(shù)
按照基因列表以及分類列表進行分組,Marker基因將根據(jù)它們所屬的群組(cluster)被分組顯示。 - 使用
RotatedAxis函數(shù)
將x軸標簽旋轉 - 基于theme函數(shù)去調整坐標軸,設置文本顏色和大小、添加邊框、調整間距等
DotPlot(pbmc,
features = split(top5$gene, top5$cluster),
cols = c("#ffffff", "firebrick3")
) +
RotatedAxis() +
theme(
strip.text.x = element_text(size = 8),
axis.text.x = element_text(color="black",size=10),
panel.border = element_rect(color = "black"),
panel.spacing = unit(1, "mm"),
axis.title = element_blank(),
axis.text.y = element_blank(),
)

使用ggplot2繪圖及美化
已經(jīng)有很多推文基于ggplot2去可視化及美化點圖:
那這邊小謝就偷個懶,基于前輩們整理好的代碼來使用ggplot2繪制marker基因的點圖。
1. 提取數(shù)據(jù)并整理細胞亞群排序
#獲取需要的數(shù)據(jù)
p <- DotPlot(pbmc,
features = split(top5$gene, top5$cluster),
cols = c("#ffffff", "firebrick3"))
#重新整理細胞亞群的排列,倒序排列
p$data$feature.groups2 <- factor(p$data$feature.groups,
levels = c("Platelet","DC","NK",
"FCGR3A+ Mono","CD8 T","B",
"Memory CD4 T","CD14+ Mono","Naive CD4 T"))
p
p$data2. 使用ggplot2繪圖
ggplot2繪圖相關的內容是需要反復練習的,如果沒有R語言基礎想要系統(tǒng)學習的話,可以了解一下生物信息學馬拉松授課,最近一期在廣州線下,歡迎大家來廣州相聚!
## 加載R包定義圖例顏色----
library(ggh4x)
library(RColorBrewer)
strip <- strip_themed(
background_x = elem_list_rect(fill = brewer.pal(9, "Paired")))
## 可視化----
p1 <- p$data %>%
ggplot(aes(x = features.plot,
y = id)) +
geom_point(aes(size = pct.exp,
color = avg.exp.scaled)) +
facet_wrap2(~feature.groups2,
scales = "free_x",
strip = strip,
nrow = 1) +
theme_classic() +
RotatedAxis()+
theme(strip.text.x = element_text(size = 8),
axis.text.x = element_text(color="black",size=10),
axis.title = element_blank(),
strip.background = element_rect(color = "white"),
axis.text.y = element_blank()) +
scale_color_gradient(low = "#ffffff",
high = "firebrick3",
name = "avg.exp")
p1
這樣就得到了帶有背景顏色圖例的結果,為了簡潔將左邊的細胞亞群標簽信息使用axis.text.y = element_blank()
不進行展示,當然我們也可以調整之后,將y軸的圖例加上

3. 整理y軸標簽
## 圖例信息----
library(ggplot2)
df <- data.frame(x = 0, y = levels(pbmc), stringsAsFactors = F )
df$y <- factor(df$y, levels = df$y )
#通過shape選擇自己想要的圖例形狀
p2 <- ggplot(df, aes(x, y, color = factor(y))) +
geom_point(size = 6, shape = 18, show.legend = F) +
scale_color_manual(values = rev(brewer.pal(9, "Paired"))) +
theme_classic() +
scale_x_continuous(expand = c(0,0)) +
theme(
plot.margin = margin(r=0),
axis.title = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_text(size = 9),
axis.ticks = element_blank(),
axis.line = element_blank()
)
p2
選擇了shape=18的實心菱形
然后將整理好的圖例和點圖拼接到一起即可
library(cowplot)
plot_grid(p2, p1, align = "h", axis="bt", rel_widths = c(1.5, 9))

小結
這期基于ggplot2在Dotplot結果的基礎上進行調整,以及提取Dotplot的結果數(shù)據(jù),使用ggplot2進行美化和可視化