我有一个表1,其中每一行对应于特定患者的基因特征向量。患者ID位于第一列(标签),基因索引位于第二列(geneIndex)。其余的列具有不同维度的特征值(总共128个)。
我能够将这些数据进行tsne缩减到2D,并根据患者ID标记群集。代码如下:
library(Rtsne)
experiment<- read.table("test.txt", header=TRUE, sep= "\t")
metadata <- data.frame(sample_id = rownames(experiment),
colour = experiment$label)
data <- as.matrix(experiment[,2:129])
set.seed(1)
tsne <- Rtsne(data)
df <- data.frame(x = tsne$Y[,1],
y = tsne$Y[,2],
colour = metadata$colour)
library(ggplot2)
ggplot(df, aes(x, y, colour = colour)) +
geom_point()然而,我的目标是可视化与geneIndex相关的特征向量。例如,我想将geneIndex "3“精确定位为红色,而图中其余的点将为灰色。
如果您有任何建议,我将不胜感激!
谢谢!
发布于 2020-10-22 06:04:48
看一下数据,似乎没有太多的3,所以如果你只是和其他人一起绘制,得到一个透明的灰色和选中的有红色..我认为这很难理解:
df$geneIndex = experiment$geneIndex
plotIndex = function(data,selectedGene){
data$Gene = ifelse(data$geneIndex == selectedGene,selectedGene,"others")
ggplot(data, aes(x, y, colour = Gene))+
geom_point(alpha=0.3,size=1)+
scale_color_manual(values=c("#FF0000E6","#BEBEBE1A"))+
theme_bw()
}
plotIndex(df,3)

也许可以尝试通过再绘制一次图,结合新的图例来圈出这些图:
library(ggnewscale)
plotIndex = function(data,selectedGene){
subdf = subset(data,geneIndex == selectedGene)
ggplot(data, aes(x, y, colour = colour)) +
geom_point(alpha=0.3,size=2,shape=20)+
new_scale_color()+
geom_point(data=subdf,
aes(col=factor(geneIndex)),
shape=1,stroke=0.8,size=2.1)+
scale_color_manual("geneIndex",values="red")+
theme_bw()
}
plotIndex(df,3)

如果你不需要图例,你可以忘记ggnewscale库。这个package也许也能做上面的事情。你需要检查一下。
https://stackoverflow.com/questions/64471319
复制相似问题