【问题标题】:feature visualization in tsne plottsne 图中的特征可视化
【发布时间】:2020-10-21 20:29:46
【问题描述】:

我有一个表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”,而绘图上的其余点将具有灰色。

如果有任何建议,我将不胜感激!

谢谢!

【问题讨论】:

    标签: r ggplot2 data-visualization


    【解决方案1】:

    查看数据,似乎没有很多 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 可能也能做到以上几点.. 你需要检查一下。

    【讨论】:

    • 谢谢!我想知道是否有可能保留集群的分布(在我的初始代码中),而不是突出显示不同颜色的集群 - 仅突出显示对应于geneIndex 3的点。
    • 不是很清楚.. 我猜你想要颜色,但最重要的是,突出显示。见编辑
    猜你喜欢
    • 2022-07-06
    • 2018-07-11
    • 2020-05-25
    • 2020-05-05
    • 1970-01-01
    • 2016-03-29
    • 2015-10-20
    • 2020-11-10
    • 2020-05-13
    相关资源
    最近更新 更多