0

I am trying to plot BOTH the individuals and averages on the same ggplot. I need to have color be the same between the individuals and averages. Aes seems to be the source of my problem, I have many more observations > 3000 and it will be important to see where averages and individuals lie in the plot space. I have tried separating everything into data.frames to combat the issue of using "$" in the aes function. I think the problem arises when I use "color" or "label" in the aes function. Maybe, ggplot does not like that the number of species' names are not the same?

# Libraries needed for example
library(dplyr)
library(ggplot2)

# Individuals PC1 and PC2 values
pc1 <- c(1,2,3,4,5,6,7,8,9,10)
pc2 <- c(4,5,6,7,8,9,10,11,12,13)
species <- c("D.a", "D.a", "D.b","D.b","D.c","D.c","D.d","D.d", "D.e", 
"D.e")

# Individual data frame
P1 <- cbind.data.frame(species,pc1,pc2)

# Averages of individuals
P2 <- P1 %>% group_by(species) %>%  summarise(pc1 = mean(pc1), pc2 = 
mean(pc2))

# GGplot
ggplot(P1, aes(x= pc1, y= pc2, color= species)) + geom_point(alpha= 0.2) 
+ geom_point(P2)

I expect to see the average values with the same color as their respective individual's color. This will hopefully evolve into allowing the same expectation with labels.

3
  • You are passing thee whole dataset in geom_point. You may need to gather the 'P2' into 'long' format Commented Sep 5, 2019 at 1:56
  • Do you need library(tidyr); gather(P2, key, val, pc1:pc2) %>% ggplot(aes(x = key, y = val, color = species)) + geom_point(alpha = 0.2) Commented Sep 5, 2019 at 1:59
  • Hello Akrun! This is not what I want, I want the PC1 and PC2 to be on the X and Y axes respectively Commented Sep 5, 2019 at 2:05

1 Answer 1

1

Be explicit about the data source and the aes mappings and it should work:

ggplot(P1) + 
    geom_point(alpha = 0.2, aes(x = pc1, y = pc2, color = species)) +
    geom_point(data = P2, aes(x = pc1, y = pc2, color = species))

output

Sign up to request clarification or add additional context in comments.

3 Comments

How would I be able to put the label of the averages on the graph? While keeping the individual points with the color? Thanks so much
Not entirely sure what you mean; is this it? Add onto end of code. + geom_text(data=P2, aes(x=pc1,y=pc2, label=paste(pc1,", ",pc2)))
I figured it out! I just needed to be more specific with the aes elements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.