user2117258 user2117258 - 3 months ago 5
R Question

ggplot facet_wrap selected columns of data.frame?

I have a data.frame

X
which contains point/sample coordinates
X1
and
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


I would like to add selected markers as columns to
X
and size the points on a scatter plot according to the associated expression value.

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)


After binding this data, the new data.frame
Z
looks something like this:

> 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


Now, I am able to plot individual scatterplots with points sized to corresponding expression values (shown below), but I'm unsure how to do this within one facet_wrap plot.

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
)


The above code works as anticipated but is very lengthy and not sufficient for a large number, say 100, selected markers.. I am assuming that I would have to somehow melt the
Z
data.frame? Any help would be greatly appreciated.

Answer

As OP suggested, one approach is to melt the original dataframe Z:

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

where id can be a vector of integers (of column indices) or strings (of column names) for id variables, and measure is a vector that gives the positions of the various measures (the markers in this case). Then call ggplot:

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

adding labels and other embellishments as desired. Output using OP's extract from Z:

enter image description here