1

Produce smooth error bar around multiple line graph using standard error available in the data frame. I already have the stadard error in the data frame so I could use data +/- se.

Produce smooth error bar around multiple line graph using standard error available in the data frame. I already have the stadard error in the data frame so I could use data +/- se.

data10 <- structure(list(Group = c("Visible", "Visible", "Visible", "Visible", 
"Visible", "Visible", "Visible", "Visible", "Visible", "Visible", 
"Visible", "Visible", "Visible", "Visible", "Visible", "Visible", 
"Visible", "Visible", "Visible", "Visible", "Visible", "Visible", 
"Visible", "Visible", "Visible", "Visible", "Visible", "Visible", 
"Visible", "Visible", "Visible", "Visible", "Remembered", "Remembered", 
"Remembered", "Remembered", "Remembered", "Remembered", "Remembered", 
"Remembered", "Remembered", "Remembered", "Remembered", "Remembered", 
"Remembered", "Remembered", "Remembered", "Remembered", "Remembered", 
"Remembered", "Remembered", "Remembered", "Remembered", "Remembered", 
"Remembered", "Remembered", "Remembered", "Remembered", "Remembered", 
"Remembered", "Remembered", "Remembered", "Remembered", "Remembered", 
"Visible", "Visible", "Visible", "Visible", "Visible", "Visible", 
"Visible", "Visible", "Visible", "Visible", "Visible", "Visible", 
"Visible", "Visible", "Visible", "Visible", "Visible", "Visible", 
"Visible", "Visible", "Visible", "Visible", "Visible", "Visible", 
"Visible", "Visible", "Visible", "Visible", "Visible", "Visible", 
"Visible", "Visible", "Remembered", "Remembered", "Remembered", 
"Remembered", "Remembered", "Remembered", "Remembered", "Remembered", 
"Remembered", "Remembered", "Remembered", "Remembered", "Remembered", 
"Remembered", "Remembered", "Remembered", "Remembered", "Remembered", 
"Remembered", "Remembered", "Remembered", "Remembered", "Remembered", 
"Remembered", "Remembered", "Remembered", "Remembered", "Remembered", 
"Remembered", "Remembered", "Remembered", "Remembered"), Condition = c("CEN", 
"CEN", "CEN", "CEN", "CEN", "CEN", "CEN", "CEN", "CEN", "CEN", 
"CEN", "CEN", "CEN", "CEN", "CEN", "CEN", "IPS", "IPS", "IPS", 
"IPS", "IPS", "IPS", "IPS", "IPS", "IPS", "IPS", "IPS", "IPS", 
"IPS", "IPS", "IPS", "IPS", "CEN", "CEN", "CEN", "CEN", "CEN", 
"CEN", "CEN", "CEN", "CEN", "CEN", "CEN", "CEN", "CEN", "CEN", 
"CEN", "CEN", "IPS", "IPS", "IPS", "IPS", "IPS", "IPS", "IPS", 
"IPS", "IPS", "IPS", "IPS", "IPS", "IPS", "IPS", "IPS", "IPS", 
"CEN", "CEN", "CEN", "CEN", "CEN", "CEN", "CEN", "CEN", "CEN", 
"CEN", "CEN", "CEN", "CEN", "CEN", "CEN", "CEN", "IPS", "IPS", 
"IPS", "IPS", "IPS", "IPS", "IPS", "IPS", "IPS", "IPS", "IPS", 
"IPS", "IPS", "IPS", "IPS", "IPS", "CEN", "CEN", "CEN", "CEN", 
"CEN", "CEN", "CEN", "CEN", "CEN", "CEN", "CEN", "CEN", "CEN", 
"CEN", "CEN", "CEN", "IPS", "IPS", "IPS", "IPS", "IPS", "IPS", 
"IPS", "IPS", "IPS", "IPS", "IPS", "IPS", "IPS", "IPS", "IPS", 
"IPS"), test = c("Pre-test", "Pre-test", "Pre-test", "Pre-test", 
"Pre-test", "Pre-test", "Pre-test", "Pre-test", "Post-test", 
"Post-test", "Post-test", "Post-test", "Post-test", "Post-test", 
"Post-test", "Post-test", "Pre-test", "Pre-test", "Pre-test", 
"Pre-test", "Pre-test", "Pre-test", "Pre-test", "Pre-test", "Post-test", 
"Post-test", "Post-test", "Post-test", "Post-test", "Post-test", 
"Post-test", "Post-test", "Pre-test", "Pre-test", "Pre-test", 
"Pre-test", "Pre-test", "Pre-test", "Pre-test", "Pre-test", "Post-test", 
"Post-test", "Post-test", "Post-test", "Post-test", "Post-test", 
"Post-test", "Post-test", "Pre-test", "Pre-test", "Pre-test", 
"Pre-test", "Pre-test", "Pre-test", "Pre-test", "Pre-test", "Post-test", 
"Post-test", "Post-test", "Post-test", "Post-test", "Post-test", 
"Post-test", "Post-test", "Pre-test", "Pre-test", "Pre-test", 
"Pre-test", "Pre-test", "Pre-test", "Pre-test", "Pre-test", "Post-test", 
"Post-test", "Post-test", "Post-test", "Post-test", "Post-test", 
"Post-test", "Post-test", "Pre-test", "Pre-test", "Pre-test", 
"Pre-test", "Pre-test", "Pre-test", "Pre-test", "Pre-test", "Post-test", 
"Post-test", "Post-test", "Post-test", "Post-test", "Post-test", 
"Post-test", "Post-test", "Pre-test", "Pre-test", "Pre-test", 
"Pre-test", "Pre-test", "Pre-test", "Pre-test", "Pre-test", "Post-test", 
"Post-test", "Post-test", "Post-test", "Post-test", "Post-test", 
"Post-test", "Post-test", "Pre-test", "Pre-test", "Pre-test", 
"Pre-test", "Pre-test", "Pre-test", "Pre-test", "Pre-test", "Post-test", 
"Post-test", "Post-test", "Post-test", "Post-test", "Post-test", 
"Post-test", "Post-test"), trial = c(1, 2, 3, 4, 5, 6, 7, 8, 
9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 
13, 14, 15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 
15, 16, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1, 2, 3, 4, 
5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16), Variables = c("Eye movement time", 
"Eye movement time", "Eye movement time", "Eye movement time", 
"Eye movement time", "Eye movement time", "Eye movement time", 
"Eye movement time", "Eye movement time", "Eye movement time", 
"Eye movement time", "Eye movement time", "Eye movement time", 
"Eye movement time", "Eye movement time", "Eye movement time", 
"Eye movement time", "Eye movement time", "Eye movement time", 
"Eye movement time", "Eye movement time", "Eye movement time", 
"Eye movement time", "Eye movement time", "Eye movement time", 
"Eye movement time", "Eye movement time", "Eye movement time", 
"Eye movement time", "Eye movement time", "Eye movement time", 
"Eye movement time", "Eye movement time", "Eye movement time", 
"Eye movement time", "Eye movement time", "Eye movement time", 
"Eye movement time", "Eye movement time", "Eye movement time", 
"Eye movement time", "Eye movement time", "Eye movement time", 
"Eye movement time", "Eye movement time", "Eye movement time", 
"Eye movement time", "Eye movement time", "Eye movement time", 
"Eye movement time", "Eye movement time", "Eye movement time", 
"Eye movement time", "Eye movement time", "Eye movement time", 
"Eye movement time", "Eye movement time", "Eye movement time", 
"Eye movement time", "Eye movement time", "Eye movement time", 
"Eye movement time", "Eye movement time", "Eye movement time", 
"Hand movement time", "Hand movement time", "Hand movement time", 
"Hand movement time", "Hand movement time", "Hand movement time", 
"Hand movement time", "Hand movement time", "Hand movement time", 
"Hand movement time", "Hand movement time", "Hand movement time", 
"Hand movement time", "Hand movement time", "Hand movement time", 
"Hand movement time", "Hand movement time", "Hand movement time", 
"Hand movement time", "Hand movement time", "Hand movement time", 
"Hand movement time", "Hand movement time", "Hand movement time", 
"Hand movement time", "Hand movement time", "Hand movement time", 
"Hand movement time", "Hand movement time", "Hand movement time", 
"Hand movement time", "Hand movement time", "Hand movement time", 
"Hand movement time", "Hand movement time", "Hand movement time", 
"Hand movement time", "Hand movement time", "Hand movement time", 
"Hand movement time", "Hand movement time", "Hand movement time", 
"Hand movement time", "Hand movement time", "Hand movement time", 
"Hand movement time", "Hand movement time", "Hand movement time", 
"Hand movement time", "Hand movement time", "Hand movement time", 
"Hand movement time", "Hand movement time", "Hand movement time", 
"Hand movement time", "Hand movement time", "Hand movement time", 
"Hand movement time", "Hand movement time", "Hand movement time", 
"Hand movement time", "Hand movement time", "Hand movement time", 
"Hand movement time"), Eye_Mx = c(1.150583333, 1.273916667, 1.213083333, 
1.065166667, 1.2373, 1.19925, 0.93675, 0.950833333, 0.616916667, 
0.440416667, 0.598083333, 0.618583333, 0.693545455, 0.667583333, 
0.873666667, 0.51825, 1.220454545, 1.034583333, 0.874583333, 
1.015166667, 0.532222222, 0.714454545, 0.905583333, 0.898333333, 
0.641666667, 0.787666667, 0.609833333, 0.623583333, 0.69925, 
0.7188, 0.61725, 0.661166667, 1.349, 1.585416667, 1.0145, 1.201090909, 
0.810545455, 0.591090909, 1.1416, 0.697166667, 0.431166667, 0.804583333, 
0.289666667, 0.63875, 0.46825, 0.633, 0.418833333, 0.691166667, 
1.219125, 0.7033, 0.524666667, 0.724818182, 0.648583333, 0.639181818, 
0.596583333, 0.509416667, 0.576272727, 0.483222222, 0.388222222, 
0.647, 0.42575, 0.269818182, 0.488333333, 0.5903, 1.869083333, 
2.066181818, 2.124166667, 2.31525, 2.0943, 1.93625, 1.786916667, 
1.922583333, 1.470833333, 1.421454545, 1.519083333, 1.508833333, 
1.575909091, 1.5135, 1.8025, 1.541, 1.800454545, 1.888666667, 
1.85575, 2.201666667, 1.55725, 1.7781, 1.748, 1.767583333, 1.489333333, 
1.4259, 1.436916667, 1.5855, 1.535666667, 1.4013, 1.3855, 1.356666667, 
1.852888889, 2.463636364, 2.031, 2.195727273, 1.804454545, 1.709090909, 
2.1938, 1.97625, 1.256833333, 1.704363636, 1.418083333, 1.371166667, 
1.459166667, 1.46725, 1.183666667, 1.407, 2.348625, 1.8981, 1.973583333, 
1.746727273, 1.6805, 1.963, 1.68075, 1.872583333, 1.345636364, 
1.339222222, 1.311222222, 1.316833333, 1.215833333, 1.053636364, 
1.415916667, 1.2292), sd = c(0.948671172, 0.678775831, 0.820965004, 
0.771358286, 1.11350558, 0.598444974, 0.794668727, 0.824723627, 
0.481933503, 0.314103185, 0.469586754, 0.576648697, 0.629203681, 
0.528873667, 0.975212642, 0.406696922, 0.986302019, 0.821480975, 
0.776634401, 0.804389643, 0.52690957, 0.881839936, 0.881676756, 
0.842954149, 0.49820502, 0.551171205, 0.611370269, 0.630794947, 
0.605911653, 0.612136659, 0.504005614, 0.478993231, 0.896792758, 
1.545713396, 1.479810742, 1.481512366, 1.016337185, 0.827241616, 
1.987092303, 0.874371549, 0.557526165, 1.312183015, 0.163762763, 
1.081580084, 0.682258832, 0.99675364, 0.582176455, 1.069035235, 
1.352635886, 1.003522136, 0.705413397, 0.93395362, 0.764277848, 
0.989686599, 0.875251492, 0.582424316, 0.618786084, 0.971365119, 
0.4453251, 1.057255968, 0.710771044, 0.157439397, 0.584064339, 
0.966582301, 0.807429305, 0.578682092, 0.911954428, 1.146678771, 
0.977409848, 0.7173858, 0.692368328, 0.84760684, 0.426626052, 
0.392027133, 0.463031406, 0.346331904, 0.435984278, 0.625301164, 
0.733525794, 0.468399014, 0.911551574, 0.845252338, 0.560227896, 
1.191183013, 0.503701088, 0.686482249, 0.812501692, 0.649220856, 
0.448065201, 0.520082782, 0.465629478, 0.601450142, 0.498518229, 
0.432112652, 0.422273393, 0.374147354, 0.631002663, 1.659917846, 
1.024954525, 1.202822771, 0.652806306, 0.768222032, 1.742846509, 
0.782477781, 0.398411581, 0.98639944, 0.580826286, 0.781519247, 
0.683742619, 0.717473487, 0.26632937, 0.748351886, 1.884740371, 
0.875399141, 0.661320505, 0.703044393, 0.49535084, 0.954243365, 
0.645801986, 1.293963499, 0.649359573, 0.623769945, 0.256283426, 
0.8611224, 0.495113363, 0.158687285, 0.522609442, 0.635988959
), se = c(0.273857778, 0.195945704, 0.236992183, 0.222671957, 
0.352121382, 0.172756183, 0.229401102, 0.238077204, 0.139122219, 
0.090673779, 0.135558019, 0.16646414, 0.189712048, 0.152672677, 
0.281519641, 0.117403289, 0.297381248, 0.237141131, 0.22419504, 
0.232207288, 0.175636523, 0.265884745, 0.254518156, 0.243339902, 
0.143819401, 0.159109422, 0.176487395, 0.182094816, 0.174911628, 
0.193574608, 0.145493889, 0.138273435, 0.298930919, 0.446209023, 
0.467957245, 0.446692786, 0.306437191, 0.249422732, 0.62837376, 
0.252409325, 0.160943941, 0.378794609, 0.047274238, 0.312225276, 
0.19695116, 0.287737991, 0.168059866, 0.30860389, 0.478229004, 
0.317341563, 0.203635307, 0.281597612, 0.220628011, 0.298401737, 
0.252663342, 0.168131418, 0.186571024, 0.323788373, 0.1484417, 
0.305203509, 0.205181927, 0.047469764, 0.168604852, 0.305660162, 
0.233084763, 0.174479216, 0.263258567, 0.331017649, 0.309084133, 
0.207091442, 0.19986952, 0.244683019, 0.123156333, 0.118200628, 
0.133665654, 0.099977409, 0.131454206, 0.180508898, 0.211750657, 
0.135215148, 0.274843141, 0.244003332, 0.161723863, 0.343864917, 
0.178085227, 0.217084748, 0.244978478, 0.187413918, 0.129345282, 
0.164464616, 0.134415652, 0.173623701, 0.143909817, 0.136646019, 
0.121899828, 0.108007038, 0.210334221, 0.500484062, 0.32411908, 
0.362664711, 0.196828507, 0.231627658, 0.551136458, 0.225881879, 
0.115011517, 0.297410621, 0.167670106, 0.225605174, 0.197379493, 
0.207116755, 0.076882667, 0.216030581, 0.666356349, 0.276825515, 
0.190906786, 0.21197586, 0.14299547, 0.2877152, 0.186426975, 
0.373535087, 0.195789278, 0.207923315, 0.085427809, 0.248584625, 
0.142926917, 0.047846017, 0.150864351, 0.201117368), ci = c(0.602756906, 
0.431273588, 0.521616278, 0.490097673, 0.796553907, 0.380233796, 
0.504908421, 0.524004393, 0.306205939, 0.199571642, 0.298361189, 
0.366385102, 0.422704785, 0.336030297, 0.619620551, 0.258402896, 
0.662606712, 0.52194411, 0.493449956, 0.511084796, 0.405018549, 
0.59242813, 0.560190685, 0.535587514, 0.316544368, 0.350197476, 
0.388446137, 0.400787988, 0.384977898, 0.437896186, 0.320229889, 
0.304337779, 0.689335936, 0.982099437, 1.058592834, 0.99529355, 
0.682784611, 0.555748479, 1.421480202, 0.555549178, 0.354235225, 
0.833721312, 0.104049895, 0.6872032, 0.433486581, 0.633307048, 
0.369897272, 0.679232583, 1.1308319, 0.71787649, 0.448198289, 
0.627438579, 0.485598977, 0.664880504, 0.556108267, 0.370054755, 
0.415706148, 0.746657327, 0.342307174, 0.671748394, 0.451602376, 
0.105769226, 0.371096776, 0.691451324, 0.513016105, 0.388763919, 
0.5794282, 0.728564932, 0.699196885, 0.455805192, 0.439909848, 
0.538543693, 0.271065261, 0.263367411, 0.29419612, 0.220048794, 
0.292898224, 0.397297405, 0.466060055, 0.297606535, 0.61238868, 
0.537047714, 0.355951823, 0.756841578, 0.421104648, 0.491079817, 
0.545846064, 0.412495252, 0.284687047, 0.37204481, 0.295846856, 
0.382143188, 0.316743371, 0.30911477, 0.268299713, 0.237721887, 
0.485031584, 1.115147982, 0.733208298, 0.808067333, 0.438561244, 
0.516098584, 1.246757287, 0.497162663, 0.253138642, 0.66267216, 
0.369039416, 0.49655364, 0.434429334, 0.455860905, 0.169217609, 
0.475480104, 1.575682382, 0.626222821, 0.420183003, 0.47231165, 
0.314730908, 0.641069416, 0.410323006, 0.822145184, 0.436245697, 
0.479472024, 0.19699688, 0.54713107, 0.314580023, 0.106607569, 
0.332050198, 0.454959094)), class = c("spec_tbl_df", "tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -128L), spec = structure(list(
    cols = list(Group = structure(list(), class = c("collector_character", 
    "collector")), Condition = structure(list(), class = c("collector_character", 
    "collector")), test = structure(list(), class = c("collector_character", 
    "collector")), trial = structure(list(), class = c("collector_double", 
    "collector")), Variables = structure(list(), class = c("collector_character", 
    "collector")), Eye_Mx = structure(list(), class = c("collector_double", 
    "collector")), sd = structure(list(), class = c("collector_double", 
    "collector")), se = structure(list(), class = c("collector_double", 
    "collector")), ci = structure(list(), class = c("collector_double", 
    "collector"))), default = structure(list(), class = c("collector_guess", 
    "collector")), skip = 1), class = "col_spec"))
p <- ggplot(data10, aes(x = trial, y = Eye_Mx)) + 
   geom_line(aes(color = Variables, linetype = Variables), lwd=1.2) + 
   scale_color_manual(values = c("darkred", "steelblue")) + facet_grid(Condition ~ Group)+ theme_bw() + xlab("Trial Pre- / Post-test") + ylab("Hand and Eye Movement time (s)") +
   scale_x_continuous(limits = c(1,16), breaks = seq(1,16,1)) + theme(axis.text.x = element_text(size = 10,face="bold", angle = 90),#, angle = 10, hjust = .5, vjust = .5),
                                                                      axis.text.y = element_text(size = 10, face = "bold"),
                                                                      axis.title.y = element_text(vjust= 1.8, size = 16),
                                                                      axis.title.x = element_text(vjust= -0.5, size = 16),
                                                                      axis.title = element_text(face = "bold")) +  theme(legend.position="top")+
   geom_vline(xintercept=8.5, linetype="dashed", color = "black", size=1.5) 
 
 p + guides(fill=guide_legend(title="Variables:")) + theme(legend.text=element_text(size=14),legend.title=element_text(size=14) ) +
   theme(strip.text = element_text(face="bold", size=12)) 
 

2 Answers2

1

As your standard errors are already included in your data you could add a confidence interval around you lines via geom_ribbon like so:

library(ggplot2)

ggplot(data10, aes(x = trial, y = Eye_Mx)) + 
  geom_line(aes(color = Variables, linetype = Variables), lwd=1.2) + 
  geom_ribbon(aes(ymin = Eye_Mx - 1.96 * se, ymax = Eye_Mx + 1.96 * se, fill = Variables), alpha = .3) +
  scale_color_manual(values = c("darkred", "steelblue")) + facet_grid(Condition ~ Group)+ theme_bw() + xlab("Trial Pre- / Post-test") + ylab("Hand and Eye Movement time (s)") +
  scale_x_continuous(limits = c(1,16), breaks = seq(1,16,1)) + theme(axis.text.x = element_text(size = 10,face="bold", angle = 90),#, angle = 10, hjust = .5, vjust = .5),
                                                                     axis.text.y = element_text(size = 10, face = "bold"),
                                                                     axis.title.y = element_text(vjust= 1.8, size = 16),
                                                                     axis.title.x = element_text(vjust= -0.5, size = 16),
                                                                     axis.title = element_text(face = "bold")) +  theme(legend.position="top")+
  geom_vline(xintercept=8.5, linetype="dashed", color = "black", size=1.5) +
  guides(fill=guide_legend(title="Variables:")) + theme(legend.text=element_text(size=14),legend.title=element_text(size=14) ) +
  theme(strip.text = element_text(face="bold", size=12))

stefan
  • 90,330
  • 6
  • 25
  • 51
0

Is it necessary to use your data? You could simply use geom_smooth and set se=TRUE.

p <- ggplot(data10, aes(x = trial, y = Eye_Mx)) + 
  geom_line(aes(color = Variables, linetype = Variables), lwd=1.2) + 
  scale_color_manual(values = c("darkred", "steelblue")) + facet_grid(Condition ~ Group)+ theme_bw() + xlab("Trial Pre- / Post-test") + ylab("Hand and Eye Movement time (s)") +
  scale_x_continuous(limits = c(1,16), breaks = seq(1,16,1)) + theme(axis.text.x = element_text(size = 10,face="bold", angle = 90),#, angle = 10, hjust = .5, vjust = .5),
                                                                     axis.text.y = element_text(size = 10, face = "bold"),
                                                                     axis.title.y = element_text(vjust= 1.8, size = 16),
                                                                     axis.title.x = element_text(vjust= -0.5, size = 16),
                                                                     axis.title = element_text(face = "bold")) +  theme(legend.position="top")+
  geom_vline(xintercept=8.5, linetype="dashed", color = "black", size=1.5) +
  geom_smooth(method="loess", se=TRUE, fullrange=FALSE, level=0.95)

p + guides(fill=guide_legend(title="Variables:")) + theme(legend.text=element_text(size=14),legend.title=element_text(size=14) ) +
  theme(strip.text = element_text(face="bold", size=12)) 

Bloxx
  • 1,495
  • 1
  • 9
  • 21
  • This code just gives me one line with standard error – Reuben Newton Addison Nov 14 '21 at 03:45
  • Simply remove `guides(fill=guide_legend(title="Variables:"))`. If you want to change the title of the legend then you have to change it for all legends using e.g. `+ labs(fill = "Variables:", linetype = "Variables:", color = "Variables:")`. Otherwise they the legends don't get merged. – stefan Nov 14 '21 at 21:54