0

I am doing PCA in R and I got the result. But when I try to plot the first two principal components I get an error:

Warning: Ignoring unknown aesthetics: fill
Error in eval(expr, envir, enclos) : object 'GROUP' not found

Here is my code:

data = read.csv("pca_scores.csv", header = T)
data = data[, c(1:3)]
ggplot(data, aes(PC1, PC2)) +
       geom_point(aes(shape = Group)) +
       geom_text(aes(label = data$X)) +
       stat_ellipse(aes(fill = Group))

I knew the problem is the “Group”. I did not mention the group in the previous code. But I really don't know how to change it

https://i.stack.imgur.com/rHgrj.png

Litty
  • 1,856
  • 1
  • 16
  • 35
Flora
  • 1
  • 3
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Pictures of data are not very helpful. If you don't have a group, just use `geom_point()` rather than `geom_point(aes(shape = Group))`. – MrFlick Feb 28 '18 at 21:54
  • Are you getting both Warning and Error of is it just one thing? And yes provide a sample of that `"pca_scores.csv"` – Aleh Feb 28 '18 at 21:56

1 Answers1

0

Agree with @MrFlick, you should always provide sample data; a screenshot of your data.frame is not useful.

That aside, you can try this:

require(tidyverse);
data %>%
    mutate(Group = gsub("\\(.+\\)$", "", X)) %>%
    ggplot(aes(PC1, PC2)) +
        geom_point(aes(shape = Group)) +
        geom_text(aes(label = X)) +
        stat_ellipse(aes(fill = Group))

A few comments:

  1. You don't need to use data$ inside aes(); just refer to the relevant column directly.
  2. I've added a Group column, which strips the "(PubChem: ...)" part from X.
  3. Keep in mind that stat_ellipse will only draw an ellipse if there are >3 points.
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68