1

I have made a spaghetti plot from this data

head(df)
       x   id stage
1: 35.15 1001     3
2: 17.22 1002     5
3: 24.69 1003     1
4: 24.88 1004     1
5: 38.00 1004     5
6: 24.08 1005     1
dput(df)
structure(list(x = c(35.15, 17.22, 24.69, 24.88, 38, 24.08, 23.26, 
17.8, 19.35, 24.16, 15.76, 18.27, 21.14, 19.2, 18.06, 20.1, 14.05, 
16.1, 13.77, 12.78, 12.22, 15.42, 12.66, 12.49, 12.31, 12.23, 
24.75, 22.68, 18.99, 28.32, 18.01, 18.98, 23.12, 31.07, 19.68, 
27.79, 25.06, 21.46, 9.14, 14.86, 14.05, 8.62, 23.37, 11.46, 
14.6, 14.07, 9.37, 20.29, 20.18, 18.85, 18.7, 24.02, 17.13, 21.35, 
21.02, 14.13, 17.48, 15.16, 23.29, 25.44, 19.96, 18.56, 27.85, 
29.08, 23.1, 23.39, 18.28, 25.96, 19.59, 18.71, 28, 34.28, 26.41, 
26.87, 24.54, 18.54, 20.75, 25.82, 25.84, 18.35, 20.6, 25.74, 
19.37, 21.12, 23.06, 15.58, 24.53, 22.65, 16.65), id = c("1001", 
"1002", "1003", "1004", "1004", "1005", "1005", "1005", "1006", 
"1006", "1006", "1006", "1007", "1007", "1007", "1007", "1008", 
"1008", "1009", "1009", "1010", "1010", "1011", "1011", "1011", 
"1011", "1012", "1012", "1012", "1013", "1013", "1013", "1013", 
"1014", "1014", "1015", "1015", "1015", "1016", "1016", "1016", 
"1016", "1017", "1018", "1018", "1018", "1018", "1019", "1019", 
"1019", "1019", "1020", "1020", "1020", "1020", "1021", "1021", 
"1022", "1022", "1022", "1022", "1023", "1023", "1024", "1024", 
"1024", "1025", "1025", "1025", "1025", "1026", "1026", "1026", 
"1026", "1027", "1027", "1027", "1027", "1028", "1028", "1028", 
"1028", "1029", "1029", "1029", "1029", "1030", "1030", "1030"
), stage = c("3", "5", "1", "1", "5", "1", "2", "5", "1", "2", 
"3", "5", "1", "2", "3", "5", "1", "5", "1", "5", "1", "5", "1", 
"2", "3", "5", "1", "2", "5", "1", "2", "3", "5", "5", "5", "1", 
"2", "5", "1", "2", "3", "5", "5", "1", "2", "3", "5", "1", "2", 
"3", "5", "1", "2", "3", "5", "1", "5", "1", "2", "3", "5", "3", 
"5", "2", "3", "5", "1", "2", "3", "5", "1", "2", "3", "5", "1", 
"2", "3", "5", "1", "2", "3", "5", "1", "2", "3", "5", "1", "2", 
"5")), class = c("data.table", "data.frame"), row.names = c(NA, 
-89L), .internal.selfref = <pointer: 0x00000000003d0788>, .Names = c("x", 
"id", "stage"))

gg.spaghetti_x<-ggplot(df, aes (x = stage, y = x, group = id, color = id))
gg.spaghetti_x <-gg.spaghetti_x + geom_line() + geom_point() + ylab("X") + stat_summary(fun.y=mean,geom="line",lwd=2,aes(group=1))
gg.spaghetti_x

This gives me a spaghetti plot with a mean, but how do I add sd error bars at each stage? Still keeping all the individiual lines. I tried using stat_summary(fun.y=mean_sd) but that did not work. spaghetti plot enter image description here

User LG
  • 307
  • 2
  • 12
  • What about it didn't work? It's helpful if you can be specific about the issue – camille Aug 10 '18 at 11:43
  • See if [this](https://stackoverflow.com/questions/15063287/add-error-bars-to-show-standard-deviation-on-a-plot-in-r/) can help. – Rui Barradas Aug 10 '18 at 12:11
  • Hi camille.I get an error: Error in layer(data = data, mapping = mapping, stat = StatSummary, geom = geom, : object 'mean_sd' not found – User LG Aug 10 '18 at 18:32

1 Answers1

1

I solved this issue by calculating the mean and sd in separate column and adding geom_errorbar on top of the plot.

gg.spaghetti_x<-ggplot(EDA.subset, aes (x = stage, y = x, group = id, color = id))
gg.spaghetti_x <-gg.spaghetti_x +geom_line() + geom_point() + ylab("X") + stat_summary(fun.y=mean,geom="line",lwd=2,aes(group=1))
gg.spaghetti_x + geom_errorbar(mapping=aes(x=stage, ymin=mean-sd, ymax=mean+sd), width=0.1, size=1, color="black")

Spaghetti plot with mean + sd

User LG
  • 307
  • 2
  • 12