如何使用R快速绘制所有变量

时间:2019-01-21 14:47:38

标签: r

我的数据显示在下图中。我正在使用ggplot2绘制点图:

  ggplot(dr, aes(x = tSNE1, y = tSNE2, color = HLA_DR)) +
    geom_point(size = 0.1) +
    theme_bw() +
    scale_color_gradientn("TCRgd",
                          colours = 
                          colorRampPalette(rev(
                          brewer.pal(n = 11, name = "RdYlBu")))(50))+
    theme(panel.background = element_rect(fill = "transparent",colour = NA), 
          panel.grid.minor = element_blank(), 
          panel.grid.major = element_blank(),
          plot.background = element_rect(fill = "transparent",colour = NA))

如何像构面函数一样同时绘制所有其他列(例如CD14,CD16 ...)。

enter image description here

1 个答案:

答案 0 :(得分:0)

要执行构面,您需要调整数据的形状,使一列包含构面因子,另一列包含构面值。使用例如,这很容易{tidyr}软件包:

dr_long = dr %>% gather(Receptor, Expression, -tSNE1, -tSNE2)

这为您提供了一个如下表:

    tSNE1 tSNE2 Receptor Expression
    <dbl> <dbl> <chr>         <dbl>
 1 0.103  0.466 HLA_DR       -0.475
 2 0.799  0.322 HLA_DR        1.63
 3 0.262  0.591 HLA_DR       -0.100
 4 0.562  0.756 HLA_DR       -1.11
 5 0.566  0.898 HLA_DR        1.22
 6 0.149  0.742 HLA_DR        0.109
…

现在您可以绘制它:

ggplot(dr_long) +
    aes(tSNE1, tSNE2, color = Expression) +
    geom_point() +
    facet_wrap(~ Receptor)

由于正在绘制tSNE数据,因此您可能还希望将coord_fixed()添加到绘图命令中。

相关问题