這是是生信技能樹生信爆款入門課程R語言ggplot部分曾老師布置的一個(gè)課外作業(yè)。
為鍛煉搜索解決問題能力,現(xiàn)在開始嘗試完成這個(gè)任務(wù)。
任務(wù)描述:
根據(jù)所給數(shù)據(jù)使用ggplot畫出如下帶圈的tsne圖

首先加載并查看數(shù)據(jù)
> load('for_tSNE.pos.Rdata')
> dim(dat)
[1] 619 4
> head(dat)
tSNE_1 tSNE_2 cell cluster
6A-11 -1.910859 -26.09210 6A-11 2
6A-13 -3.498666 -27.66961 6A-13 2
6A-14 -7.646899 -12.26195 6A-14 2
6A-15 -2.986069 -27.00602 6A-15 2
6A-16 -7.633320 -12.21226 6A-16 2
6A-17 -4.207616 -25.12467 6A-17 2
> class(dat)
[1] "data.frame"
> dat[1:4,1:4]
tSNE_1 tSNE_2 cell cluster
6A-11 -1.910859 -26.09210 6A-11 2
6A-13 -3.498666 -27.66961 6A-13 2
6A-14 -7.646899 -12.26195 6A-14 2
6A-15 -2.986069 -27.00602 6A-15 2
> str(dat)
'data.frame': 619 obs. of 4 variables:
$ tSNE_1 : num -1.91 -3.5 -7.65 -2.99 -7.63 ...
$ tSNE_2 : num -26.1 -27.7 -12.3 -27 -12.2 ...
$ cell : chr "6A-11" "6A-13" "6A-14" "6A-15" ...
$ cluster: Factor w/ 5 levels "0","1","2","3",..: 3 3 3 3 3 3 3 3 3 3 ...
看出是一個(gè)619行4列數(shù)據(jù)框
根據(jù)任務(wù)描述,使用‘ggplot’畫出‘帶圓圈’的tsne聚類圖
看出有兩個(gè)關(guān)鍵詞 搜索ggplot,圓圈
通過初步篩選找到幾篇相關(guān)的帖子進(jìn)行精讀
首先看了這一篇
1 R語言給PCA加個(gè)小圈圈
發(fā)現(xiàn)他最終繪制得到的圖形是這樣,

和我們的目標(biāo)圖相比,相似性不是很高。先擱置,看下一篇。
2ggplot2畫圖一些不常用但是很重要的畫圖參數(shù)
看到通過下面代碼可以畫出類似的圖。
stat_ellipse(aes(x = X, y = Y, fill = group), geom = "polygon", alpha = 1/2, levels = 0.95) ###這里的參數(shù)需要注意的是參數(shù)設(shè)置的問題
大家可以嘗試

然而他并沒有詳細(xì)講過程。先放著,再看下一篇
3.如何將徒手畫的紅色圓圈添加到ggplot2圖形中?(How can I add freehand red circles to a ggplot2 graph?)
library(Rtsne)
iris_unique <- unique(iris) # Remove duplicates
iris_matrix <- as.matrix(iris_unique[,1:4])
set.seed(42) # Set a seed if you want reproducible results
tsne_out <- Rtsne(iris_matrix) # Run TSNE
# Show the objects in the 2D tsne representation
library(ggplot2)
tsne_plot <- data.frame(x = tsne_out$Y[,1], y = tsne_out$Y[,2], col = iris_unique$Species)
ggplot(tsne_plot) + geom_point(aes(x=x, y=y, color=col))

這篇雖然可以參考畫出圖,然而還是不知道如何加圈。再往下看。
4. pca , nmds , pcoa 圖添加分組的橢圓
最終定位到這一篇解決了問題。
原文代碼示例:
ggplot(faithful, aes(waiting, eruptions, color = eruptions > 3)) +
geom_point() +
stat_ellipse(level = 0.8) +
stat_ellipse(level = 0.9)
效果圖如下:

這個(gè)圖和目標(biāo)圖片非常相似。雖然他是兩個(gè)圈,但是我們?nèi)サ粢粋€(gè)就可以了呀。重點(diǎn)查看他的繪圖用數(shù)據(jù)格式。
> dim(faithful)
[1] 272 2
> head(faithful)
eruptions waiting
1 3.600 79
2 1.800 54
3 3.333 74
4 2.283 62
5 4.533 85
6 2.883 55
數(shù)據(jù)只有2列可以對(duì)應(yīng)上我們數(shù)據(jù)的tSNE_1,tSNE_2,填充列沒有,所以作者根據(jù)前兩列數(shù)據(jù)中是否大于3劃分了兩組。
我們是有填充列的,cluster,
根據(jù)小潔老師講的填充有
fill 內(nèi)填充
col 邊緣填充,
所以我們?cè)偌觾蓚€(gè)參數(shù)
fill = cluster,
col= cluster
這樣就得到了健明老師給的示例圖形。最終代碼如下:
rm(list = li())
stringsAsFactors = FALSE
load('for_tSNE.pos.Rdata')
head(dat)
class(dat)
dat[1:4,1:4]
str(dat)
ggplot(dat, aes(tSNE_1, tSNE_2,fill = cluster,col= cluster)) +
geom_point() +
stat_ellipse(level = 0.9)
得到結(jié)果:

然后再根據(jù)小潔老師講的添加theme系列代碼去掉灰色背景和網(wǎng)格。
ggplot(dat, aes(tSNE_1, tSNE_2,fill = cluster,col= cluster)) +
geom_point() +
stat_ellipse(level = 0.9)+
theme_bw()+
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank())

這樣就和原圖基本一致了。
2021.5.26補(bǔ)充
剛才小潔大佬來指點(diǎn)了下,添加填充色,添加兩句代碼即可。如下
geom = "polygon",
alpha = 1/2
ggplot(dat, aes(tSNE_1, tSNE_2,fill = cluster,col=cluster ))+
geom_point() +
stat_ellipse(level = 0.9,
geom = "polygon",
alpha = 1/5)+
theme_bw()+
theme(panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank())

不加 alpha = 1/5會(huì)是什么樣呢?沒有透明度看不到點(diǎn)了,
