So I do a PCA analysis, and I usually plotted the results with ggplot2, but I just recently discovered ggbiplot which can show arrows with the variables.
ggbiplot seems to be working ok, though it shows some problems (like the imposibility of changing point size, hence the whole layer thing I do in the MWE).
The problem I am facing now is that, while ggplot2 plots adjust the plot width to the plotting area, ggbiplot does not. With my data, the ggbiplot is horribly narrow and leaves horribly wide vertical margins, even though it expands the same x axis interval as the ggplot2 plot (it is, in fact, the same plot).
I am using the iris data here, so I had to make the png width extra large so the problem I am facing becomes evident. Please check the MWE below:
data(iris)
head(iris)
pca.obj <- prcomp(iris[,1:4],center=TRUE,scale.=TRUE)
pca.df <- data.frame(Species=iris$Species, as.data.frame(pca.obj$x))
rownames(pca.df) <- NULL
png(filename="test1.png", height=500, width=1000)
print(#or ggsave()
ggplot(pca.df, aes(x=PC1, y=PC2)) +
geom_point(aes(color=Species), cex=3)
)
dev.off()
P <- ggbiplot(pca.obj,
obs.scale = 1,
var.scale=1,
ellipse=T,
circle=F,
varname.size=3,
groups=iris$Species, #no need for coloring, I'm making the points invisible
alpha=0) #invisible points, I add them below
P$layers <- c(geom_point(aes(color=iris$Species), cex=3), P$layers) #add geom_point in a layer underneath (only way I have to change the size of the points in ggbiplot)
png(filename="test2.png", height=500, width=1000)
print(#or ggsave()
P
)
dev.off()
This code produces the following two images.
ggplot2 output (desired plot width):
ggbiplot output (plot too narrow for plotting area):
See how, while ggplot2 adjusts the plot width, to the plot area, ggbiplot does not. With my data, the ggbiplot plot is extremely narrow and leaves large vertical margins.
My question here is: How to make ggbiplot behave as ggplot2? How can I adjust the plot width to my desired plotting area (png size) with ggbiplot? Thanks!
Change the ratio
argument in coord_equal()
to some value smaller than 1 (default in ggbiplot()
) and add it to your plot. From the function description: "Ratios higher than one make units on the y axis longer than units on the x-axis, and vice versa."
P + coord_equal(ratio = 0.5)
NOTE: as @Brian noted in the comments, "changing the aspect ratio would bias the interpretation of the length of the principal component vectors, which is why it's set to 1."