ggplot facet_wrap选择了data.frame的列?

时间:2016-09-09 02:01:10

标签: r ggplot2 facet-wrap

我有一个data.frame let testInstance = TestClass() Reader.funcDictionary.updateValue(.A, forKey: "a") testInstance.executeFunction(currentInstance: Reader()) // prints 1 Library.funcDictionary.updateValue(.C, forKey: "c") Library.funcDictionary.updateValue(.D, forKey: "d") testInstance.executeFunction(currentInstance: Library()) // prints 2 ,其中包含点/样本坐标XX1

X2

我想将选定的标记作为列添加到> 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

绑定此数据后,新的data.frame 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) 看起来像这样:

Z

现在,我能够绘制各个散点图,其点大小与相应的表达式值相对应(如下所示),但我不确定如何在一个facet_wrap图中执行此操作。

> 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

上面的代码按预期工作,但是非常冗长,并且不足以容纳大量的100个选定标记。我假设我必须以某种方式融合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 ) data.frame?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

正如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)

根据需要添加标签和其他装饰。使用来自Z的OP提取输出:

enter image description here