Search code examples
rggplot2geom-point

How to correctly add average, standard deviation and jitter in geom_plot()? ggplot2


I am trying to create a graph that shows the mean, standard deviation and the dots of each value (jitter). However, I was unable to insert the average point and the standard deviations are the same for all categories (and this is incorrect).

My code (example):

# Package
library(ggplot2)

# Dataframe
fruit <- c()
value <- c()
for (i in 0:30) {
  list_fruit <- c('Apple','Banana','Pear')
  fruit <- append(fruit, sample(list_fruit, 1))
  value <-  append(value, sample(1:50, 1))
}
df <- data.frame(fruit, value)

# Seed
set.seed(123)

# Plot
ggplot(df, aes(x = fruit, y = value, color = fruit)) +
  geom_point() +
  geom_jitter(width = 0.1) + 
  geom_linerange(aes(ymin = mean(value)-sd(value), ymax = mean(value)+sd(value)), col = 'black') +
  scale_color_manual(values=c('red','blue','purple'))

Result enter image description here


Solution

  • It's probably because the manual you're referring to uses an old version of ggplot2 which didn't allow for alternate orientations of data. For example fun.y is now called fun. Below an example as per ggplot2 version 3.3.3.

    # Package
    library(ggplot2)
    
    # Dataframe
    fruit <- c()
    value <- c()
    for (i in 0:30) {
      list_fruit <- c('Apple','Banana','Pear')
      fruit <- append(fruit, sample(list_fruit, 1))
      value <-  append(value, sample(1:50, 1))
    }
    df <- data.frame(fruit, value)
    
    # Seed
    set.seed(123)
    
    # Plot
    ggplot(df, aes(x = fruit, y = value, color = fruit)) +
      geom_jitter(width = 0.1) + 
      stat_summary(
        geom = "linerange",
        fun.data = mean_sdl, 
        fun.args = list(mult = 1),
        colour = "black"
      ) +
      stat_summary(
        geom = "point",
        fun = mean,
        colour = "black", size = 3
      ) +
      scale_color_manual(values=c('red','blue','purple'))
    

    Created on 2021-03-17 by the reprex package (v0.3.0)