3

I am trying to create and save several graphics. I am stuck in the case that the factoextra package is used to make the graphs.

  pca.plot<-function(x){
  biplot<-paste(out_f,"\\biplot.jpg", sep="")
  jpeg(file=biplot, type="cairo")
  fviz_pca_biplot(x,  geom = "text")
  dev.off()
}

This is a simple function creating a biplot from an input pca object (pca created using FactoMineR package) out_f is previously defined. When I run the script line by line, it works. When I run it as a script, nothing is created.

  pca.plot<-function(x){
  pve<-paste(out_f,"\\proportion_of_variance_explained.jpg", sep="")
  jpeg(file=pve, type="cairo")
  barplot(x$eig[,2], names.arg=1:nrow(x$eig),
          main = "Variances",
          xlab = "Principal Components",
          ylab = "Percentage of variances",
          col ="steelblue")
  lines(x = 1:nrow(x$eig), x$eig[, 2], type="b", pch=19, col = "red")
  dev.off()   
}

In this case there is no problem. Does anyone know why is there a problem in the first case?

Thanks in advance, John

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
  • 1
    `fviz_pca_biplot` produces a ggplot2 plot. These don't actually get plotted unless you call `print` on them. (On the command line that happens automatically.) –  May 13 '15 at 23:34

1 Answers1

3

The plots produced by factoextra are ggplot2. You should use print(fviz_pca_biplot(res.pca)) as follow:

# Principal component analysis
data(iris)
res.pca <- prcomp(iris[, -5],  scale = TRUE)

# This will do nothing
jpeg("biplot.jpg")
fviz_pca_biplot(res.pca)
dev.off()

# This will do the right thing
jpeg("biplot.jpg")
print(fviz_pca_biplot(res.pca))
dev.off()

Good Luck!

A. Kassambara
  • 228
  • 1
  • 5