0

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

morgan121
  • 2,213
  • 1
  • 15
  • 33
mrad
  • 173
  • 1
  • 11
  • You haven't shared a reproducible data For eg - what is `counts`? What is `pca` ? Have a look at this post https://stackoverflow.com/questions/26034177/save-multiple-ggplots-using-a-for-loop – Ronak Shah Feb 03 '20 at 06:05
  • what do you mean by merge? – StupidWolf Feb 03 '20 at 18:49

0 Answers0