SBL
SBL

Reputation: 111

Adding shaded SE manually to a lineplot ggplot

I have the following plot, that I am trying to add SE shading bars to.

enter image description here It is using this dataframe of predicted values from my model.

data.frame(
   x = c(-0.4, -0.2, 0, 0.2, 0.4, 0.6, 0.8),
   predicted = c(0.68210177774552,0.824971306553031,
                 0.911923452641176,0.957884075392982,0.98037732841837,
                 0.990970636204506,0.995869238009664),
   std.error = c(0.38195580456581,0.22923983122453,
                 0.123053035742282,0.183476660090966,0.328809680207388,
                 0.488362992665536,0.651775655589864),
   conf.low = c(0.503706928219636,0.750465569524191,
                 0.890530388978947,0.940737822137466,0.963272013048915,
                 0.976820374876465,0.985337590318705),
   conf.high = c(0.819367367560485,0.880765628369274,
                 0.929466912178259,0.97022643270505,0.989602181967697,
                 0.996513543605265,0.998845128758168),
   group = as.factor(c("PupilAmp_SMC",
                           "PupilAmp_SMC","PupilAmp_SMC","PupilAmp_SMC",
                           "PupilAmp_SMC","PupilAmp_SMC","PupilAmp_SMC")),
   group_col = as.factor(c("PupilAmp_SMC",
                           "PupilAmp_SMC","PupilAmp_SMC","PupilAmp_SMC",
                           "PupilAmp_SMC","PupilAmp_SMC","PupilAmp_SMC"))
)

I keep trying to use the following code, but I keep getting an error.. not sure how to make the ribbon geom work. Any help would be appreciated!

PupilPEA_SMC <- ggplot(PEAPupilGrob, aes(x= x, predicted))  +
                geom_line(color = "red", size = 2) + 
                ylim(.65,1.0) + 
                labs (y = "Likelihood of Post-Error Accuracy", x = "Single Trial Pupillary Amplitude") +
                geom_ribbon(PEAPupilGrob, aes(ymin=predicted+std.error, ymax= predicted - std.error), alpha=0.1, fill = "red",  
              color = "black", linetype = "dotted") +
                theme_bw() +
                theme(text = element_text(size = 20))
PupilPEA_SMC

Upvotes: 0

Views: 41

Answers (1)

Michiel Duvekot
Michiel Duvekot

Reputation: 1881

This would work:

ggplot(PEAPupilGrob, aes(x = x, predicted))  +
  geom_line(color = "red", size = 2) + 
  ylim(0.3, 1.7) + 
  labs (y = "Likelihood of Post-Error Accuracy", x = "Single Trial Pupillary Amplitude") +
  geom_ribbon(
    aes(
      ymin = predicted - std.error, 
      ymax = predicted + std.error
    ), 
    alpha = 0.1, 
    fill = "red",  
    color = "black", 
    linetype = "dotted"
  ) +
  theme_bw() +
  theme(text = element_text(size = 20))

plot predicted +/- std.error as ribbon

Upvotes: 1

Related Questions