2

I'm getting the following error message for trying to do a (basic) plot of a PCA using ggplot2:

Error in plot_label(p = p, data = plot.data, label = label, label.label = label.label, : Unsupported class: princomp

The plot works in R's own biplot function biplot(prin_comp) so I'm not sure if I've got something wrong in how I'm asking ggplot to do this or if it is a problem with the PCA output that makes it unsuitable for ggplot. I can't post data (confidentiality issues) but it is quite a big data set and I could post the PCA output if that would help?

EDIT: Here is an example using mtcars dataset, which has same results:

library("ggbiplot")
data(mtcars)
View(mtcars)
pca <- princomp(mtcars, scale=1)
princomp(x = mtcars, scale = 1)

Standard deviations:
     Comp.1      Comp.2      Comp.3      Comp.4      Comp.5      Comp.6 
134.3827868  37.5472829   3.0226511   1.2860724   0.8922099   0.6530910 
     Comp.7      Comp.8      Comp.9     Comp.10     Comp.11 
  0.3037193   0.2814568   0.2467490   0.2073344   0.1952988 

 11  variables and  32 observations.


ggbiplot(pca)

Error in plot_label(p = p, data = plot.data, label = label, label.label = label.label, : Unsupported class: princomp

Again, that works in base R's biplot function. I am looking to get the following kind of output (not the plot shown in answer below), hence using biplot rather than autoplot, apologies if this was not obvious: biplot mtcars

Can't find similar error on googling or search on here but happy to be linked to if anyone else can find it.

divibisan
  • 11,659
  • 11
  • 40
  • 58
Zurgle
  • 31
  • 1
  • 3
  • 1
    Please [see here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making a reproducible R question folks can help with. That includes a representative sample of data—doesn't have to be your actual data, can instead be a commonly available dataset or random data that simulates the issue—and all necessary code needed – camille Aug 22 '18 at 16:00
  • Have edited to add an example with mtcars dataset. Thanks. – Zurgle Aug 23 '18 at 10:30

2 Answers2

0

Try this;

Installing and loading in ggbiplot:

install_github("vqv/ggbiplot")
library("ggbiplot")
--Rest of your code--

The biplot:

ggbiplot::ggbiplot(pca)

This should show the biplot.

(edited for formatting)

0
 Warning in install.packages :
   package ‘ggbiplot’ is not available for this version of R

The package ggbiplot is not available for many version R. Perhapes you could try other package just like library("FactoMineR") library("factoextra"), there are some examples you can find here: http://www.sthda.com/english/articles/31-principal-component-methods-in-r-practical-guide/112-pca-principal-component-analysis-essentials/#r-packages

ouflak
  • 2,458
  • 10
  • 44
  • 49
Jim
  • 31
  • 3