Error: stat_summary requires the following missing aesthetics: y

Hi, I have been trying to code for a histogram with groups that include the mean value for each group on the plot using geom_point() but keep getting the error "stat_summary requires the following missing aesthetics: y". I would greatly appreciate some help in understanding what is happening here. Thanks in advance.

code used:

fin2.2 <- finch%>%select(Species,WingL,BodyL)
sppf<-factor(finch[,1])
ggplot(fin2.2, aes(x=WingL,fill=Species, color=Species)) +
geom_histogram(position="identity",bins=20, alpha=0.5)+
scale_fill_manual(values=c("red","grey","green","purple","cyan"))+
labs(title= "Histogram of wing lengths in finches",x="Wing lengths (mm)", y = "Frequency")+
geom_point() +
stat_summary(geom = "point", fun = "mean", colour = "sppf", size = 2.0)

Do you mean to do something like this? I used the result of stat_summary to mark the mean WingL for each Species. I put the y value in manually. I see the colors are not quite right, so that needs to be fixed.

library(ggplot2)
fin2.2 <- data.frame(Species = rep(LETTERS[1:5], each = 25),
                     WingL = c(rnorm(25, 5, 1), rnorm(25, 8, 1),rnorm(25, 11, 1),
                               rnorm(25, 14, 1),rnorm(25, 17, 1)))

ggplot(fin2.2, aes(x=WingL, fill=Species, color=Species)) +
  geom_histogram(position="identity",bins=40, alpha=0.5)+
  scale_fill_manual(values=c("red","grey","green","purple","cyan"))+
  labs(title= "Histogram of wing lengths in finches",x="Wing lengths (mm)", y = "Frequency")+
  #geom_point() +
  stat_summary(geom = "point", mapping = aes( y = 7), 
               fun = "mean",  size = 2.0) 

Created on 2020-05-02 by the reprex package (v0.3.0)

Thank you so much for your help. Including the mapping parameter in the code fixed the problem and the plot looks great now. I am still new to coding in R and I wondered why the hashtag in front of #geom_point() improved the code? It was my understanding that # allows R to ignore those sections?

I put the # in front of geo_point to remove it from the code. I did that rather than simply deleting that line so it would be obvious that my solution did not include the geom_point. The stat_summary includes the argument geom = "point" and that is what causes the points to appear on the plot.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.