How can I make all my PCA plots the same shape using ggplots?
2
0
Entering edit mode
20 months ago
mgranada3 ▴ 50

I am using DESeq2 for gene expression analysis. I have 3 different conditions which I am normalizing to my control so I have produced 3 different PCA plots for my data. For some reason one of my conditions (B) keeps automatically adjusting its shape when I stretch my figure window in R studio. This was the code I originally used to produce these figures:

> pcaData <- plotPCA(vsd, intgroup=c("condition", "type"), returnData=TRUE)
> percentVar <- round(100 * attr(pcaData, "percentVar"))
> ggplot(pcaData, aes(PC1, PC2, color=condition, shape=type)) +
+     geom_point(size=4) +
+     xlab(paste0("PC1: ",percentVar[1],"% variance")) +
+     ylab(paste0("PC2: ",percentVar[2],"% variance")) + 
+     coord_fixed()

I then tried to adjust my axis limits by adding "scale_x_continuous(limits = c(-50,70))" so my code now looks like this:

> pcaData <- plotPCA(vsd, intgroup=c("condition", "type"), returnData=TRUE)
> percentVar <- round(100 * attr(pcaData, "percentVar"))
> ggplot(pcaData, aes(PC1, PC2, color=condition, shape=type)) +
+     geom_point(size=4) +
+     xlab(paste0("PC1: ",percentVar[1],"% variance")) +
+     ylab(paste0("PC2: ",percentVar[2],"% variance")) + 
+     scale_x_continuous(limits = c(-50,70)) +
+     coord_fixed()

But my figures still look off, I noticed that my quadrants in (A) and (C) look rectangular but more square in (B). For publication purposes I would look to have 3 uniform images.

enter image description here

deseq2 axis ggplots • 2.5k views
ADD COMMENT
0
Entering edit mode

Use cowplot.

cowplot::plot_grid(list_of_plots, align="hv", axis="tbl")

...and then play with the parameters until it looks well-aligned.

ADD REPLY
0
Entering edit mode

There is some tweaking that can be done, but not much you can do when values are on different scales.

A numerical spread of principal components is what it is, and you have no control over it. In your panel C PC1 values seem to be in the -70 to +70 range, while they are -50 to +70 in panel B. The range difference on the Y axis is even more dramatic. If you force the size of your coordinate system to be the same in all images - that will line them up perfectly - there will be empty areas at the edges of some images but not the others.

ADD REPLY
0
Entering edit mode

OP could also distort the sizes of the grids to force them all to have the same dimensions, but this will make distances mean different things on different graphs. This may or may not be misleading.

ADD REPLY
1
Entering edit mode
20 months ago

Use + theme(aspect.ratio = 1) and they'll all be square.

ADD COMMENT
0
Entering edit mode
20 months ago
foejvs546 ▴ 20

Hi mgranada3 ,

You could try save the plot using these commands for each PCA plot. If you specifiy the size like this, are the plots the same size?

my_filename = 'PCA plot 1.png'
png(my_filename, height = 1000, width = 1000, res=200)
print(my_plt)
dev.off()

You will have to assign your plot as an object like this first:

my_plt <- ggplot(......
ADD COMMENT

Login before adding your answer.

Traffic: 2898 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6