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.
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))