1

I am very new to R and trying to plot a PCA figure of my data using ggbiplot. So please bear with me if my question does not make any senses to you. Basically, I was following the tutorial I found here, except I was using my own data set.

Everything was fine until I wish to use the code below to plot a figure:

g <- ggbiplot(ir.pca, obs.scale = 1, var.scale = 1, 
                  groups = ir.ppm, ellipse = TRUE, 
                  circle = TRUE)

Then, I encountered an error stating : Error in names(ell)[1:2] <- c("xvar", "yvar") : 'names' attribute [2] must be the same length as the vector [0]

After that, I edited my code and using the default setting for groups =, which should be = NULL as I recall.

g <- ggbiplot(ir.pca, obs.scale = 1, var.scale = 1, 
                  groups = ir.ppm, ellipse = TRUE, 
                  circle = TRUE) `

With the edited code, I did able to plot the PCA figure but It cannot categorize the observations into different groups as I desired. Although I still does not know the meaning of the error: Error in names(ell)[1:2] <- c("xvar", "yvar") : 'names' attribute [2] must be the same length as the vector [0] , I do suspect that it may have something to do with my factor ir.ppm.

Here are all the code I have used before I have encountered the error.

ppm3 = read.csv("normalize_GasPhase_heatmap_no_ID_transpose.csv", header = TRUE, row.names = 1)
ppm3_1 <- ppm3[,1:30]
ir.ppm <- ppm3[,31]
ir.pca <- prcomp(ppm3_1, center = TRUE, scale. = TRUE)
library(ggbiplot)
g <- ggbiplot(ir.pca, obs.scale = 1, var.scale = 1, groups = ir.ppm, ellipse = TRUE, circle = TRUE)

In total, I have 6 observations and 31 variables in my raw data ppm3.

I have been browsing some questions related to plotting PCA figure with ggbiplot in stackoverflow, but it seems not much people encountered the same problem as I did. I would really appreciate if anyone can offer me some help. Thank you.

A.Chiu
  • 11
  • 3
  • having same problem, tried to follow this [tutorial ](https://www.r-bloggers.com/computing-and-visualizing-pca-in-r/) but once I try to use my data I get the same issue. Have you found a solution ? – nlv Nov 16 '16 at 20:08

1 Answers1

2

You only have one observation for each of your factors in ir.ppm. You need more observations for each of the factors in order to display ellipses.

One work around is to remove the ellipses option like this:

g <- ggbiplot(ir.pca, obs.scale = 1, var.scale = 1, 
                  groups = ir.ppm,
                  circle = TRUE)
Parth
  • 21
  • 5