0

I am trying to create a biplot of principal components of my dataframe(bwwq), but I keep receiving an error:

Error in FUN(X[[i]], ...) : object 'Location' not found

I don't understand what's happening, because I can see 'Location' in the dataframe that I'm using for plotting. Here is a snippet of the data (there are actually 17obs and 14 variables).

        Season   Location        PC1        PC2       PC3       PC4        PC5
sample1 spring  SiteA     -72.000048 33.5970759  8.873916 -2.201533  2.2538914
sample2 summer  SiteA     -71.492600 34.6823399  4.772814 -3.041415  2.1484592
sample3 fall    SiteA     -63.659760 -1.7413278 -6.658140  5.425656 -1.3978123
sample4 spring  SiteB     -57.476273 -0.3021883 -4.412755  6.253279 -2.9854573 
sample5 summer  SiteB     -64.381669  3.3609588 -3.753914  5.455738 -0.5185786
sample6 fall    SiteB       8.155867  6.5698710  5.965872 -6.753837 -1.9268316

I receive another error if I use PCAvalues$Location in the ggplot call.

Error: Aesthetics must be either length 1 or the same as the data (12): shape

I must be missing something blindingly obvious! What am I doing wrong?!?!

Below, I include the script that I used to make create PCAvalues (based on this and this). When I remove shape = Location, I get a nice PCA biplot with Season indicated by colour.

wq.pca <- prcomp(bwwq[,3:14])
summary(wq.pca)

# Extract PC axes for plotting
PCAvalues <- data.frame(Season = bwwq$Season, 
                        Location = bwwq$Location, 
                        wq.pca$x)

# Extract loadings of the variables
PCAloadings <- data.frame(Variables = rownames(wq.pca$rotation), 
                          wq.pca$rotation)

# Plot
ggplot(data=PCAvalues, aes(x = PC1, y = PC2, 
                           shape = Location, # I THINK THIS IS THE PROBLEM HERE 
                           colour = Season)) +
  geom_segment(data = PCAloadings, 
               aes(x = 0, y = 0,
                   xend = (PC1*5), 
                   yend = (PC2*5)), 
               arrow = arrow(length = unit(1/2, "picas")),
               color = "black") +
  geom_point(size = 3) +
  annotate("text", 
           x = (PCAloadings$PC1*5), 
           y = (PCAloadings$PC2*5),
           label = PCAloadings$Variables) +
  scale_color_brewer(palette = "Set1") +
  theme_bw()
ayesha
  • 135
  • 15
  • 1
    try moving the `shape=Location` outside the `aes()` bit – morgan121 Dec 12 '18 at 05:12
  • The way the heading `Location` is misaligned with the rest of the column seems suspicious. Is it possible that it's actually named ` Location` (with a leading space)? – Tim Goodman Dec 12 '18 at 06:44
  • By the way, if you run `dput(bwwq)` and include the output with your question, it'll make it easier for us to reproduce what you're seeing. (See here for more tips: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example ) – Tim Goodman Dec 12 '18 at 07:19
  • Thanks - I moved `aes(shape=Location)` into `geom_point` and that worked! – ayesha Dec 12 '18 at 10:04

0 Answers0