2

The Question is easy. I'd like to biplot the results of PCA(mydata), which I did with FactoMineR. As it seems I can only display ether the variables or the individuals with the built in ploting device:

 plot.PCA(pca1, choix="ind/var").

I know it can be easily done with the result of princomp(), but I really like how FactoMineR handles the NA's and it seems easier to me in many ways.

Is there a way? I saw it somehow done with ggplot2 but again only with the result of princomp(), and I have no idea how to change the code so that it works with PCA().

I also saw a solution for doing the individual and variable plot separately with ggplot2 (Look at the bottom), but how do I combine those?

Maybe the solution is somehow in the first link, but then I don't really get it :/.

I hope I made myself clear!

Greetings

Lukas

Community
  • 1
  • 1
Lukas
  • 223
  • 3
  • 12
  • This question has previously been asked and answered [here](http://stackoverflow.com/q/10252639/1036500) and [here](http://stackoverflow.com/q/6578355/1036500). – Ben Mar 13 '14 at 17:27
  • Funny how you post the same link i posted in the question. I found these posts too but the first one is not producing a biplot but a variable&individual plot, this can be done with factominer easily and was not the problem. The second link also only offered a solution for the princomp-output, as stated in the question too. So no, it hasn't been answered. – Lukas Mar 13 '14 at 18:20

2 Answers2

3

You can also do it with ggord, and code is easy to adapt to other ordination objects if you add additional S3 methods for them :

install.packages("devtools")
library(devtools)
install_github("fawda123/ggord")
library(ggord)
library(FactoMineR)
ord <- PCA(iris[, 1:4], graph = FALSE)
ggord(ord, iris$Species)

enter image description here

(Different kinds of scaling are possible though, e.g. row principal (form biplot), column principal (covariance biplot), symmetric biplot, etc, which are currently not supported by goord. Though it would be easy to edit the ggord.PCA S3 method or goord.default method to support this.)

Tom Wenseleers
  • 7,535
  • 7
  • 63
  • 103
1

Hi you can adapt the code of your first link for PCA objects from FactoMineR like this :

PCbiplot2 <- function(res.pca, x="Dim.1", y="Dim.2") {
  if(!require(ggplot2)) install.packages("ggplot2")
  # res.pca being a PCA object
  data <- data.frame(obsnames=row.names(res.pca$ind$coord), res.pca$ind$coord)
  plot <- ggplot(data, aes_string(x=x, y=y)) + geom_text(alpha=.4, size=3,     aes(label=obsnames))
  plot <- plot + geom_hline(aes(0), size=.2) + geom_vline(aes(0), size=.2)
  datapc <- data.frame(varnames=rownames(res.pca$var$coord), res.pca$var$coord)
  mult <- min(
    (max(data[,y]) - min(data[,y])/(max(datapc[,y])-min(datapc[,y]))),
    (max(data[,x]) - min(data[,x])/(max(datapc[,x])-min(datapc[,x])))
  )
  datapc <- transform(datapc,
                      v1 = .7 * mult * (get(x)),
                      v2 = .7 * mult * (get(y))
  )
  plot <- plot + coord_equal() + geom_text(data=datapc, aes(x=v1, y=v2,     label=varnames), size = 5, vjust=1, color="red")
  plot <- plot + geom_segment(data=datapc, aes(x=0, y=0, xend=v1, yend=v2),     arrow=arrow(length=unit(0.2,"cm")), alpha=0.75, color="red")
  plot
}

library(FactoMineR)
fit2 <- PCA(USArrests, graph=F)
PCbiplot2(fit2)

Tell me if it works !

Edit : add libraries like jlhoward suggests

Community
  • 1
  • 1
Victorp
  • 13,636
  • 2
  • 51
  • 55
  • Thanks! I tried it and the following error occurs: "Could not find function "arrow". Is this something that can be found in the packagelist? – Lukas Mar 13 '14 at 15:23
  • http://stackoverflow.com/questions/14677035/arrow-in-ggplot2-no-longer-supported tells me that I simply have to library(grid)... let's see if it works – Lukas Mar 13 '14 at 15:28
  • And it does!! Thanks a bunch victorp! You really saved my day! :) – Lukas Mar 13 '14 at 15:30
  • grid is " now part of the base R distribution" so library(grid) works fine, arrow works without adding the function :) thanks anyway – Lukas Mar 13 '14 at 15:34
  • Good to know ! the CRAN is not very clear... : http://cran.r-project.org/web/packages/grid/ – Victorp Mar 13 '14 at 15:36
  • This answer is a [duplicate](http://stackoverflow.com/a/6579572/1036500) Of course you are allowed to use other people's code but you [must link back to the original](http://blog.stackoverflow.com/2009/06/attribution-required/) – Ben Mar 13 '14 at 17:26
  • True! He changed the first three lines so that it works with FactoMineR. And mentioned it: "[...]adapt the code of your first link[...] like this:" – Lukas Mar 13 '14 at 18:25