Change the load length (arrows) on the PCA section with ggplot2 / ggfortify?

I struggled with changing the length of loads (arrows) in ggplot2 / ggfortify PCA. I examined this answer in detail, and the only information I found was either with the new biplot features or with other completely different PCA packages (ggbiplot, factoextra), none of which relate to the question I would like to answer:

Is it possible to scale / resize PCA downloads in ggfortify?

Below is the code I have to build for the PCA using the R functions, as well as the code for building the PCA using autoplot / ggfortify. You will notice that on the stock exchanges of R I can scale the load just by multiplying by the scalar (* 20 here), so my arrows are not limited in the middle of the PCA chart. Using autoplot ... not so much. What am I missing? I will move on to another package if necessary, but I would really like to better understand ggfortify.

On other sites that I found, the axes of the graph never exceed +/- 2. My graph is +/- 20, and the loads sit firmly around 0, presumably on the same scale as the graphs with smaller axes. I would still like to draw a PCA using ggplot2, but if ggfortify does not, I need to find another package that will be.

#load data geology rocks frame
georoc <- read.csv("http://people.ucsc.edu/~mclapham/earth125/data/georoc.csv")

#load libraries
library(ggplot2)
library(ggfortify)

geo.na <- na.omit(georoc) #remove NA values
geo_matrix <- as.matrix(geo.na[,3:29]) #create matrix of continuous data in data frame
pca.res <- prcomp(geo_matrix, scale = T) #perform PCA using correlation matrix (scale = T)
summary(pca.res) #return summary of PCA

#plotting in stock R
plot(pca.res$x, col = c("salmon","olivedrab","cadetblue3","purple")[geo.na$rock.type], pch = 16, cex = 0.2)
#make legend
legend("topleft", c("Andesite","Basalt","Dacite","Rhyolite"), 
       col = c("salmon","olivedrab","cadetblue3","purple"), pch = 16, bty = "n")
#add loadings and text
arrows(0, 0, pca.res$rotation[,1]*20, pca.res$rotation[,2]*20, length = 0.1)
text(pca.res$rotation[,1]*22, pca.res$rotation[,2]*22, rownames(pca.res$rotation), cex = 0.7)

#plotting PCA
autoplot(pca.res, data = geo.na, colour = "rock.type", #plot results, name using original data frame
         loadings = T, loadings.colour =  "black", loadings.label = T,
         loadings.label.colour = "black")

, , , ggplot2 ggfortify. .

R- , , ggplot

ggplot

+4
1

, , , , OP , , , , , - .

# Load data
DATA <- data.frame(iris)

# Do PCA
PCA <- prcomp(iris[,1:4])

# Extract PC axes for plotting
PCAvalues <- data.frame(Species = iris$Species, PCA$x)

# Extract loadings of the variables
PCAloadings <- data.frame(Variables = rownames(PCA$rotation), PCA$rotation)

    # Plot
ggplot(PCAvalues, aes(x = PC1, y = PC2, colour = Species)) +
  geom_segment(data = PCAloadings, aes(x = 0, y = 0, xend = (PC1*5),
     yend = (PC2*5)), arrow = arrow(length = unit(1/2, "picas")),
     color = "black") +
  geom_point(size = 3) +
  annotate("text", x = (PCAloadings$PC1*5), y = (PCAloadings$PC2*5),
     label = PCAloadings$Variables)

enter image description here

, xend yend geom_segment. , .

, annotate.

0

All Articles