[R] Fast way to draw mean values and 95% confidence intervals of groups with ggplot2

Luigi Marongiu m@rong|u@|u|g| @end|ng |rom gm@||@com
Thu Nov 16 12:59:16 CET 2023


Hello,
I have triplicate (column A) readings (column D) of samples exposed to
different concentrations (column C) over time (column B).
Is it possible to draw a line plot of the mean values for each
concentration (C)? At the moment, I get a single line.
Also, is there a simple way to draw the 95% CI around these data? I
know I need to use ribbon with the lower and upper limit, but is there
a simple way for ggplot2 to calculate directly these values?
Here is a working example:

```
A = c(rep(1, 28), rep(2, 28), rep(3, 28))
B = rep(c(0, 15, 30, 45, 60, 75, 90), 12)
C = rep(c(rep(0, 7), rep(0.6, 7), rep(1.2, 7),
          rep(2.5,7)),3)
D = c(731.33,    761.67,    730,    761.67,    741.67,    788.67,    784.33,
      686.67,    685.33,    680,    693.67,    684,    704,    709.67,    739,
      731,    719,    767,    760.67,    776.67,    768.67,    675,    671.67,
      668.67,    677.33,    673.67,    687,    696.67,    727,    750.67,
      752.67,    786.67,    794.67,    843.33,    946,    732.67,    737.33,
      775.33,    828,    918,    1063,    1270,    752.67,    742.33,
  735.67,
      747.67,    777.33,    803.67,    865.67,    700,    700.67,    705.67,
      722.67,    744,    779,    837,    748,    742,    754,    747.67,
      775.67,    808.67,    869,    705.67,    714.33,    702.33,    730,
      710.67,    731,    744,    686.33,    687.33,    670,    702.33,
      669.33,    707.33,    708.33,    724,    747,    761.33,    715,
      697.67,    728,    728)

df = data.frame(A, B, C, D)
library(ggplot2)
ggplot(data=df, aes(x=B, y=D, z=C, color =C)) +
  geom_line(stat = "summary", fun = "mean") +
  geom_ribbon()
```

Thank you



More information about the R-help mailing list