I want some figure like this two geom_riboon from: https://doi.org/10.1016/j.neuroimage.2019.116060
This is head of my data:
time mean sem class
1 -200 -0.03459213 0.002035373 n
2 -196 -0.03065102 0.001801943 n
3 -192 -0.02696143 0.001602451 n
4 -188 -0.02354627 0.001436726 n
5 -184 -0.02040872 0.001302610 n
6 -180 -0.01753815 0.001196551 n
I wrote this code:
ggplot(df, aes(time,
color = class))
geom_line(mapping = aes(y = mean))
geom_ribbon(aes(y = mean,
ymin = mean - sem,
ymax = mean sem,
color = class))
But it doesn't create shaded area and I only see the mean line.
CodePudding user response:
Your problem is that geom_line
and geom_ribbon
require different aesthetics:
for geom_line
you need to set color
, for geom_ribbon
one uses fill
:
library(tidyverse)
df <- expand.grid(time = 1:100, class = c("n", "m"))
df$mean <- cumsum(rnorm(200)) / 2
df$sem <- runif(200) / 2 .1
head(df)
#> time class mean sem
#> 1 1 n -0.4234046 0.2881793
#> 2 2 n -0.7435868 0.1144850
#> 3 3 n 0.4751080 0.5943168
#> 4 4 n -0.3778799 0.1435532
#> 5 5 n -0.7692029 0.1378587
#> 6 6 n -0.9309995 0.1538270
ggplot(df, aes(time))
geom_line(aes(y = mean, color = class))
geom_ribbon(aes(ymin = mean - sem, ymax = mean sem, fill = class), alpha = .5)
Created on 2022-07-05 by the reprex package (v2.0.1)