ggplot facet_wrap 选定的列 data.frame?

ggplot facet_wrap selected columns of data.frame?

我有一个 data.frame X,其中包含 point/sample 坐标 X1X2

> head(X)
                               X1        X2 Cluster Timepoint Transcripts       MEF       ESC
Drop_6_6A_0_TACCTAATCTAC 169.3437  20.18623       2     Day 0       49688 0.4366071 0.3260743
Drop_6_6A_0_TCAGCTTGTCAC 155.8880 -16.69927       3     Day 0       47365 0.4554254 0.3350818
Drop_6_6A_0_TCGCAATAAGAT 168.4270  36.50967       2     Day 0       44881 0.4114934 0.2595030
Drop_6_6A_0_AATCTACCAATC 164.3964 -27.17404       3     Day 0       44640 0.4748225 0.3525822
Drop_6_6A_0_GGATTAAGTTCA 162.2900 -24.10504       3     Day 0       36822 0.4723676 0.3391785
Drop_6_6A_0_TGATCTAGTGTC 155.4231 -19.18974       3     Day 0       35889 0.4664174 0.3408899

我想将选定的标记作为列添加到 X 并根据关联的表达式值调整散点图上的点的大小。

NANOG  = t(data['NANOG',rownames(X)])
SAL4 = t(data['SAL4',rownames(X)])
COL5A2 = t(data['COL5A2',rownames(X)])
ESRRB = t(data['ESRRB',rownames(X)])
ELN    = t(data['ELN',rownames(X)])
POU5f1 = t(data['POU5F1',rownames(X)])
PTN    = t(data['PTN',rownames(X)])
CXCL5  = t(data['CXCL5',rownames(X)])
Z = cbind(X, NANOG, SAL4, POU5f1, ESRRB, COL5A2, ELN, PTN, CXCL5)

绑定此数据后,新的 data.frame Z 看起来像这样:

> head(Z)
                               X1        X2 Cluster Timepoint Transcripts       MEF       ESC     NANOG NA POU5F1 ESRRB   COL5A2 ELN      PTN     CXCL5
Drop_6_6A_0_TACCTAATCTAC 169.3437  20.18623       2     Day 0       49688 0.4366071 0.3260743 0.0000000 NA      0     0 5.113106   0 1.004522 0.2645434
Drop_6_6A_0_TCAGCTTGTCAC 155.8880 -16.69927       3     Day 0       47365 0.4554254 0.3350818 0.2763494 NA      0     0 3.068572   0 1.309109 1.0395819
Drop_6_6A_0_TCGCAATAAGAT 168.4270  36.50967       2     Day 0       44881 0.4114934 0.2595030 0.0000000 NA      0     0 5.264248   0 0.000000 0.0000000
Drop_6_6A_0_AATCTACCAATC 164.3964 -27.17404       3     Day 0       44640 0.4748225 0.3525822 0.0000000 NA      0     0 3.554919   0 1.592698 0.2916205
Drop_6_6A_0_GGATTAAGTTCA 162.2900 -24.10504       3     Day 0       36822 0.4723676 0.3391785 0.0000000 NA      0     0 3.838676   0 1.536569 1.9954283
Drop_6_6A_0_TGATCTAGTGTC 155.4231 -19.18974       3     Day 0       35889 0.4664174 0.3408899 0.0000000 NA      0     0 4.029014   0 6.187616 0.0000000

现在,我可以根据相应的表达式值(如下所示)绘制点大小的单个散点图,但我不确定如何在一个 facet_wrap 图中执行此操作。

library(gridExtra)
g = arrangeGrob(
  ggplot(Z, aes(X1, X2, color=NANOG)) + ggtitle("NANOG") + 
    geom_point() + 
    xlab(paste0("TSNE1")) + 
    ylab(paste0("TSNE2")) +
    theme_bw() + theme(axis.line = element_line(colour = "black"), panel.grid.minor = element_blank(), panel.background = element_blank()) + scale_colour_gradient(low='light blue', high='red') + 
    ggsave(paste0(outdir, timepoint, ".tsne.",lab,".density.clustered.all.genes.TSNE1.TSNE2.nanog.expression.no.noise.pdf"), height=pdf_height, width=pdf_width+5),
  ggplot(Z, aes(X1, X2, color=SAL4)) + ggtitle("SAL4") + 
    geom_point() + 
    xlab(paste0("TSNE1")) + 
    ylab(paste0("TSNE2")) +
    theme_bw() + theme(axis.line = element_line(colour = "black"), panel.grid.minor = element_blank(), panel.background = element_blank()) + scale_colour_gradient(low='light blue', high='red') + 
    ggsave(paste0(outdir, timepoint, ".tsne.",lab,".density.clustered.all.genes.TSNE1.TSNE2.SAL4.expression.no.noise.pdf"), height=pdf_height, width=pdf_width+5),
  ggplot(Z, aes(X1, X2, color=POU5f1)) + ggtitle("POU5F1") + 
    geom_point() + 
    xlab(paste0("TSNE1")) + 
    ylab(paste0("TSNE2")) +
    theme_bw() + theme(axis.line = element_line(colour = "black"), panel.grid.minor = element_blank(), panel.background = element_blank()) + scale_colour_gradient(low='light blue', high='red') + 
    ggsave(paste0(outdir, timepoint, ".tsne.",lab,".density.clustered.all.genes.TSNE1.TSNE2.pou5f1.expression.pdf"), height=pdf_height, width=pdf_width+5), 
  ggplot(Z, aes(X1, X2, color=ESRRB)) + ggtitle("ESRRB") + 
    geom_point() + 
    xlab(paste0("TSNE1")) + 
    ylab(paste0("TSNE2")) +
    theme_bw() + theme(axis.line = element_line(colour = "black"), panel.grid.minor = element_blank(), panel.background = element_blank()) + scale_colour_gradient(low='light blue', high='red') + 
    ggsave(paste0(outdir, timepoint, ".tsne.",lab,".density.clustered.all.genes.TSNE1.TSNE2.ESRRB.expression.pdf"), height=pdf_height, width=pdf_width+5), 
  ggplot(Z, aes(X1, X2, color=COL5A2)) + ggtitle("COL5A2") + 
    geom_point() + 
    xlab(paste0("TSNE1")) + 
    ylab(paste0("TSNE2")) +
    theme_bw() + theme(axis.line = element_line(colour = "black"), panel.grid.minor = element_blank(), panel.background = element_blank()) + scale_colour_gradient(low='light blue', high='green') + 
    ggsave(paste0(outdir, timepoint, ".tsne.",lab,".density.clustered.all.genes.TSNE1.TSNE2.col5a2.expression.pdf"), height=pdf_height, width=pdf_width+5), 
  ggplot(Z, aes(X1, X2, color=ELN)) + ggtitle("ELN") + 
    geom_point() + 
    xlab(paste0("TSNE1")) + 
    ylab(paste0("TSNE2")) +
    theme_bw() + theme(axis.line = element_line(colour = "black"), panel.grid.minor = element_blank(), panel.background = element_blank()) + scale_colour_gradient(low='light blue', high='green') + 
    ggsave(paste0(outdir, timepoint, ".tsne.",lab,".density.clustered.all.genes.TSNE1.TSNE2.eln.expression.pdf"), height=pdf_height, width=pdf_width+5), 
  ggplot(Z, aes(X1, X2, color=PTN)) + ggtitle("PTN") + 
    geom_point() + 
    xlab(paste0("TSNE1")) + 
    ylab(paste0("TSNE2")) +
    theme_bw() + theme(axis.line = element_line(colour = "black"), panel.grid.minor = element_blank(), panel.background = element_blank()) + scale_colour_gradient(low='light blue', high='green') + 
    ggsave(paste0(outdir, timepoint, ".tsne.",lab,".density.clustered.all.genes.TSNE1.TSNE2.ptn.expression.pdf"), height=pdf_height, width=pdf_width+5), 
  ggplot(Z, aes(X1, X2, color=CXCL5)) + ggtitle("CXCL5") + 
    geom_point() + 
    xlab(paste0("TSNE1")) + 
    ylab(paste0("TSNE2")) +
    theme_bw() + theme(axis.line = element_line(colour = "black"), panel.grid.minor = element_blank(), panel.background = element_blank()) + scale_colour_gradient(low='light blue', high='green') + 
    ggsave(paste0(outdir, timepoint, ".tsne.",lab,".density.clustered.all.genes.TSNE1.TSNE2.cxcl5.expression.pdf"), height=pdf_height, width=pdf_width+5), 
  nrow=2, ncol=4
)

上面的代码按预期工作但是非常冗长并且对于大量选择标记来说不够,比如 100 个。我假设我必须以某种方式融化 Z data.frame?任何帮助将不胜感激。

正如 OP 所建议的,一种方法是融化原始数据框 Z:

library(reshape2)
d <- melt(Z, id = 1:5, measure = 6:ncol(Z))

其中 id 可以是 id 变量的整数向量(列索引)或字符串(列名),measure 是一个向量,给出各种度量的位置(标记在这个案例)。然后调用 ggplot:

library(ggplot2)
ggplot(d, aes(x = X1, y = X2, size = value)) +
  geom_point() +
  facet_wrap(~ variable)

根据需要添加标签和其他装饰。使用 OP 从 Z:

中摘录的输出