1

my data is

x_mean    w_mean    m_mean        se      LLCI      ULCI
     1.6667    3.1000    3.1565    0.1164    2.9273    3.3856
     2.6667    3.1000    3.2456    0.0683    3.1110    3.3801
     4.0000    3.1000    3.3644    0.1070    3.1539    3.5750
     1.6667    4.0000    2.9842    0.0727    2.8410    3.1273
     2.6667    4.0000    3.2712    0.0490    3.1747    3.3677
     4.0000    4.0000    3.6539    0.0774    3.5015    3.8064
     1.6667    4.5560    2.8777    0.0933    2.6941    3.0614
     2.6667    4.5560    3.2870    0.0666    3.1559    3.4182
     4.0000    4.5560    3.8328    0.1136    3.6092    4.0563

I want to draw three separate lines distinguished by w_mean (just three unique values for w), with x = x_mean, y = m_mean, in one plot. I also want to add confidence intervals to those lines. Can I implement this using ggplot? Thank you for your help. Let me know if I need to clarify any details.

MC2020
  • 67
  • 6
  • 1
    Could you please include the code you have tried so far and to make life easier for anyone wanting to explore and verify a solution include your data so it can easily be copied: try using `dput(your_dataframe)`. If you have time have a look at [mre] for guidance on asking questions. – Peter Jan 18 '21 at 08:21
  • Does this answer your question? [How to add shaded confidence intervals to line plot with specified values](https://stackoverflow.com/questions/29743503/how-to-add-shaded-confidence-intervals-to-line-plot-with-specified-values) – tjebo Jan 18 '21 at 08:44

1 Answers1

1

The first step, to plot 3 lines, one for each w_mean is straightforward. If the aesthetic defining the color (among others) is a factor, ggplot will automatically separate the lines.

library(ggplot2)

g <- ggplot(df1, aes(x_mean, m_mean, color = factor(w_mean))) +
  geom_line()
g

enter image description here

Now add the confidence bands and make the legend title prettier.

g + geom_ribbon(aes(ymin = LLCI, ymax = ULCI, 
                    fill = factor(w_mean)), 
                alpha = 0.25,
                color = NA,
                show.legend = FALSE) +
  labs(color = "w_mean")

enter image description here

Data

df1 <-
structure(list(x_mean = c(1.6667, 2.6667, 4, 1.6667, 2.6667, 
4, 1.6667, 2.6667, 4), w_mean = c(3.1, 3.1, 3.1, 4, 4, 4, 4.556, 
4.556, 4.556), m_mean = c(3.1565, 3.2456, 3.3644, 2.9842, 3.2712, 
3.6539, 2.8777, 3.287, 3.8328), se = c(0.1164, 0.0683, 0.107, 
0.0727, 0.049, 0.0774, 0.0933, 0.0666, 0.1136), LLCI = c(2.9273, 
3.111, 3.1539, 2.841, 3.1747, 3.5015, 2.6941, 3.1559, 3.6092), 
    ULCI = c(3.3856, 3.3801, 3.575, 3.1273, 3.3677, 3.8064, 3.0614, 
    3.4182, 4.0563)), class = "data.frame", row.names = c(NA, 
-9L))
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66