基因组数据绘图ggplot2绘图生信分析过程中的一些小trick

使用ggplot画出带圆圈的tsne聚类图

2021-01-12  本文已影响0人  Seurat_Satija

这是是生信技能树生信爆款入门课程R语言ggplot部分曾老师布置的一个课外作业。
为锻炼搜索解决问题能力,现在开始尝试完成这个任务。

任务描述:

根据所给数据使用ggplot画出如下带圈的tsne图


微信图片_20210112103454.jpg

首先加载并查看数据

> 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 ...

看出是一个619行4列数据框

根据任务描述,使用‘ggplot’画出‘带圆圈’的tsne聚类图
看出有两个关键词 搜索ggplot,圆圈

通过初步筛选找到几篇相关的帖子进行精读
首先看了这一篇

1 R语言给PCA加个小圈圈

发现他最终绘制得到的图形是这样,


image.png

和我们的目标图相比,相似性不是很高。先搁置,看下一篇。

2ggplot2画图一些不常用但是很重要的画图参数

看到通过下面代码可以画出类似的图。

 stat_ellipse(aes(x = X, y = Y, fill = group), geom = "polygon", alpha = 1/2, levels = 0.95) ###这里的参数需要注意的是参数设置的问题
大家可以尝试
image.png

然而他并没有详细讲过程。先放着,再看下一篇

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

这个图和目标图片非常相似。虽然他是两个圈,但是我们去掉一个就可以了呀。重点查看他的绘图用数据格式。

> 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

数据只有2列可以对应上我们数据的tSNE_1,tSNE_2,填充列没有,所以作者根据前两列数据中是否大于3划分了两组。
我们是有填充列的,cluster,

根据小洁老师讲的填充有

fill 内填充

col 边缘填充,

所以我们再加两个参数
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)

得到结果:


image.png

然后再根据小洁老师讲的添加theme系列代码去掉灰色背景和网格。

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

这样就和原图基本一致了。

上一篇 下一篇

猜你喜欢

热点阅读