Search code examples
rggplot2pca

How to use autoplot in R to plot rotated components?


I'm using the autoplot function in R (from the ggfortify package) to plot the first two components of a PCA, like this, using the iris dataset as an example:

data<-iris
data_dims<-(data[, 1:4])
data$Species<-as.factor(data$Species)
plot1<-autoplot(prcomp(data_dims, center = TRUE, scale = TRUE), data = data, colour = 'Species', frame = TRUE)

However, I'm wondering if anyone could help me to plot the rotated solution instead? I know to get the rotated solution like this:

pcompanalysis<-prcomp(data, center = TRUE, scale = TRUE)

pcompanalysis_rot <- varimax(pcompanalysis$rotation)

However, I'm unsure how to then use this rotated solution in the autoplot() function?

Edit: Examples are now using the iris dataset to allow reproduction.


Solution

  • Here is a possibile solution:

    library(ggfortify)
    library(pracma)
    
    # Generate a dataset
    set.seed(1234) 
    n <- 20
    p <- 25
    ncomp <- 2
    data_dims <- matrix(rnorm(n*p),nrow=n)
    data <- data.frame(data_dims, Species=cut(runif(n), breaks=4))
    
    # Perform PCA and calculate varimax rotated scores
    pca.obj <- prcomp(data_dims, center=T, scale=T)
    rawLoadings <- pca.obj$rotation[,1:ncomp] %*% diag(pca.obj$sdev, ncomp, ncomp)
    varimax.obj <- varimax(rawLoadings)
    rotatedLoadings <- varimax.obj$loadings
    
    # Create a prcomp object with varimax rotated scores
    pca_obj <- list(sdev=pca.obj$sdev, rotation=t(varimax.obj$rotmat), 
                  center=NULL, scale=NULL, x=scale(pca.obj$x[,1:ncomp]))
    name.pcs <- paste0("PC",1:ncomp)
    dimnames(pca_obj$rotation) <- list(name.pcs,name.pcs)
    dimnames(pca_obj$x) <- list(rownames(data_dims),name.pcs)
    class(pca_obj) <- "prcomp"
    
    plot1 <- autoplot(pca_obj, data=data, colour='Species', 
                      frame=TRUE, variance_percentage=F)
    plot1
    

    For more details see here.

    enter image description here