Lets say I have a data table called dt
| group | value |
| - | - |
| a | 1 |
| b | 2 |
| a | 3 |
| b | 4 |
| a | 5 |
And I already have my Jittered Strip Plot and I need to indicate the standard error in ggplot2 by using geom_error. Since there are two varibles a and b in column group. How do I specify the group when I calculate mean() and sd() in the geom_error function
Do I have to create another table to store mean and sd for my dataset?
CodePudding user response:
The way to do it is to compute the summary statistics before the plot.
suppressPackageStartupMessages({
library(ggplot2)
library(data.table)
})
setDT(dt)
dt[, c("mean", "sd") := list(mean(value), sd(value)), by = group]
ggplot(dt, aes(group, value))
geom_jitter()
geom_errorbar(aes(ymin = mean - sd, ymax = mean sd), width = 0.5)
Data used
set.seed(2)
dt <- data.frame(group = sample(c("a", "b"), 20, TRUE),
value = sample(100, 20, TRUE))