ggplot2 and mean value

Good afternoon,

I am trying to make a plot using ggplot2 packages. I would like to make the mean appears on the graphe. I can get this:

However, I don't want the mean line showing mean for each X values but rather want a straight mean line of the value of x=1
Is it possible?

This would be to add mean for each values as a circle and show the difference
Thank you very much!

Hi @Capucine, if I understood correctly, you want to plot the average value of y as a horizontal line across all values of x, right? In that case, you could try this:

mean_y <- mean(d$y, na.rm = TRUE) # get the mean of variable y
ggplot(d, aes(x, y)) +
    geom_hline(yintercept = mean_y) + # draw horizontal line at the mean of y

Where d is your dataframe. Hope that's helpful!

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.