使用ggplot畫出帶圓圈的tsne聚類圖

這是是生信技能樹生信爆款入門課程R語言ggplot部分曾老師布置的一個(gè)課外作業(yè)。
為鍛煉搜索解決問題能力,現(xiàn)在開始嘗試完成這個(gè)任務(wù)。

任務(wù)描述:

根據(jù)所給數(shù)據(jù)使用ggplot畫出如下帶圈的tsne圖


微信圖片_20210112103454.jpg

首先加載并查看數(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)他最終繪制得到的圖形是這樣,


image.png

和我們的目標(biāo)圖相比,相似性不是很高。先擱置,看下一篇。

2ggplot2畫圖一些不常用但是很重要的畫圖參數(shù)

看到通過下面代碼可以畫出類似的圖。

 stat_ellipse(aes(x = X, y = Y, fill = group), geom = "polygon", alpha = 1/2, levels = 0.95) ###這里的參數(shù)需要注意的是參數(shù)設(shè)置的問題
大家可以嘗試
image.png

然而他并沒有詳細(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))
image.png

這篇雖然可以參考畫出圖,然而還是不知道如何加圈。再往下看。

4. pca , nmds , pcoa 圖添加分組的橢圓

最終定位到這一篇解決了問題。

原文代碼示例:

ggplot(faithful, aes(waiting, eruptions, color = eruptions > 3)) +
  geom_point() +
  stat_ellipse(level = 0.8) +
  stat_ellipse(level = 0.9)

效果圖如下:


image.png

這個(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é)果:


image.png

然后再根據(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())
image.png

這樣就和原圖基本一致了。

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())

image.png

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


image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 第一章使用ggplot2進(jìn)行數(shù)據(jù)可視化 library(tidyverse) 查看運(yùn)行環(huán)境 devtools::s...
    PhageNanoenzyme閱讀 1,131評(píng)論 0 0
  • 作者:黃寶臣 數(shù)據(jù)科學(xué)/科學(xué)哲學(xué)碩士/本科生物狗知乎原文:https://www.zhihu.com/ques...
    iColors閱讀 1,158評(píng)論 0 2
  • ggplot2繪圖理念 圖形系統(tǒng)的核心理念是把繪圖與數(shù)據(jù)分離,把數(shù)據(jù)相關(guān)的繪圖與數(shù)據(jù)無關(guān)的繪圖分離,按圖層作圖。g...
    毛線東東a閱讀 2,525評(píng)論 0 12
  • 本筆記內(nèi)容:最近工作中遇到的分析需求:按照要求的分組畫boxplot和PcoA的散點(diǎn)圖。對(duì)畫各種圖的實(shí)現(xiàn)方法,一些...
    GPZ_Lab閱讀 14,567評(píng)論 0 18
  • ggplot2包的目的是提供一個(gè)全面的、基于語法的、連貫一致的圖形生成系統(tǒng),允許用戶創(chuàng)建新穎的、有創(chuàng)新性的數(shù)據(jù)可視...
    井底蛙蛙呱呱呱閱讀 3,508評(píng)論 0 11

友情鏈接更多精彩內(nèi)容