0

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): test1

ggbiplot output (plot too narrow for plotting area): test2

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!

DaniCee
  • 2,397
  • 6
  • 36
  • 59
  • 1
    I think it's forcing a 1:1 aspect ratio by default using something like `coord_fixed()` - I don't have ggbiplot installed and can't remember if something like `p + coord_cartesian()` would override the current aspect ratio. – Marius Sep 14 '17 at 07:58
  • I had the same pb with the PCA plot from factominer. impossible to change! – agenis Sep 14 '17 at 07:58
  • + theme(aspect.ratio=...) – Andre Elrico Sep 14 '17 at 08:04

1 Answers1

2

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."

markus
  • 25,843
  • 5
  • 39
  • 58
  • Is there a way to have this info to do this automatically (instead of having to check the output plot first, and then change it)? In a way that it automatically fits to the plot area, like ggplot2... – DaniCee Sep 14 '17 at 08:54
  • 2
    @DaniCee changing the aspect ratio would bias the interpretation of the length of the principal component vectors, which is why it's set to 1. – Brian Sep 14 '17 at 12:15
  • @Brian that is a really interesting point! I might just do the opposite and use coord_fixed() when using ggplot2 and leave ggbiplot as is – DaniCee Sep 15 '17 at 06:21