rplotpcabiplot

How to make a pretty biplot in R without using external packages?


I'm a huge fan of the typical R plots. Today I have to make a biplot, but the typical biplot are ugly. There is a way to make it prettier, draw the ellipses, etc. without using others packages?

If is not possible, how can I draw it looking as the classical R plots?

DF <- iris
PCA <- prcomp(DF[,c(1:4)], scale. = T, center = T)
biplot(PCA)

The result:

enter image description here

A desired result (made in PAST3)

enter image description here


Solution

  • I notice that the points plotted in your PAST3 version do not seem to match up with those in the R biplot. It looks like the y-axis is flipped in the two versions.

    The structure returned from prcomp has what you need to make one in the nicer style. The projected points are in PCA$x so you can get the desired plot from base R plotting with

    plot(PCA$x[,1:2], pch=20, col=iris$Species)
    

    biplot

    For adding ellipses, I always use dataEllipse from the car package, but that goes outside your request to not use other packages.

    UPDATE

    As requested in the comment, I am adding how to add ellipses using the car package.

    library(car)
    plot(PCA$x[,1:2], pch=20, col=iris$Species)
    dataEllipse(PCA$x[,1], PCA$x[,2], iris$Species, lwd=1,
        group.labels = NULL, plot.points=FALSE, add=TRUE,
        fill=TRUE, fill.alpha=0.02) 
    

    Biplot with ellipses

    I picked something that I thought looked nice, but dataEllipse allows you to make many kinds of adjustments. Just look at the help page so that you can tune it to your taste.