I am using the below code to generate a PCA plot in Rstudio:
col <- c(rep("orange2", 9), rep("red", 16), rep("forestgreen", 11))
pca <- prcomp(t(counts))
plot(pca$x[,1:2], xlab="", ylab="", col=col, pch=20, cex=2, type="n") +
text(pca$x[,1:2], sample, col=col) +
lam <- pca$sdev
v <- lam^2
v <- v/sum(v)
k1 <- round(100*v[1], 2)
k2 <- round(100*v[2], 2)
title(main=paste("All samples,", k1+k2, "% of variance explained"), xlab=paste(k1, "% of
variance explained"), ylab=paste(k2, "% of variance explained")) +
legend(-80, -25, c("PEDIS:2", "PEDIS:3", "PEDIS:4"), text.col=c("orange2", "red",
"forestgreen"), cex=0.8)
Running these lines sequentially works fine to create the plot for exporting as a pdf
or png
, but I was hoping to run this code to generate a data object so I can merge this figure with another one. How should I go about doing this? I've tried adding example <- plot...
to the beginning of line 3 but I get this error:
Error in title(main = paste("All samples,", k1 + k2, "% of variance explained"), : non-numeric argument to binary operator