Search code examples
rggplot2scatter-plot

How to plot stat_mean for scatterplot in R ggplot2?


For each treatment tmt, I want to plot the means using stat_summary in ggplot2 with different colour size. I find that the there are mulitple means being plotted over the current points. Not sure how to rectify it.

df <- data.frame(x = rnorm(12, 4,1), y = rnorm(12, 6,4), tmt = rep(c("A","B","C"), each = 4))

  ggplot(aes(x = x, y = y, fill  = tmt), data = df) +
  geom_point(shape=21, size=5, alpha = 0.6) +
  scale_fill_manual(values=c("pink","blue", "purple")) +
  stat_summary(aes(fill = tmt), fun = 'mean', geom = 'point', size = 5) + 
  scale_fill_manual(values=c("pink","blue", "purple"))

Plot without the last two lines of code

enter image description here

Plot with the entire code

enter image description here


Solution

  • Using stat_summary you compute the mean of y for each pair of x and tmt. If you want the mean of x and the mean of y per tmt I would suggest to manually compute the means outside of ggplot and use a second geom_point to plot the means. In my code below I increased the size and used rectangles for the means:

    df <- data.frame(x = rnorm(12, 4,1), y = rnorm(12, 6,4), tmt = rep(c("A","B","C"), each = 4))
    
    library(ggplot2)
    library(dplyr)
    
    df_mean <- df |> 
      group_by(tmt) |> 
      summarise(across(c(x, y), mean))
    
    ggplot(aes(x = x, y = y, fill  = tmt), data = df) +
      geom_point(shape=21, size=5, alpha = 0.6) +
      geom_point(data = df_mean, shape=22, size=8, alpha = 0.6) +
      scale_fill_manual(values=c("pink","blue", "purple"))