1

I have the following dataframe:

df <- data.frame(
  time = factor(c(1, 1, 2, 2)),
  value = c(1, 5, 3, 4),
  group = factor(c(1, 2, 1, 2)),
  upper = c(1.1, 5.3, 3.3, 4.2),
  lower = c(0.8, 4.6, 2.4, 3.6)
)

I want to plot the column "value" as a line and then an area that goes "under" which minimum point is lower and maximum point is upper.

So far I have this:

ggplot(df, aes(time, value, colour = group)) +
  geom_line(aes(group = group)) +
  geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2)

But I don't want an error bar but an area, like this: enter image description here

Dave2e
  • 22,192
  • 18
  • 42
  • 50
Paula
  • 497
  • 2
  • 8

1 Answers1

3

The geom_ribbon() function should provide the desired result.

df <- data.frame(
   time = factor(c(1, 1, 2, 2)),
   value = c(1, 5, 3, 4),
   group = factor(c(1, 2, 1, 2)),
   upper = c(1.1, 5.3, 3.3, 4.2),
   lower = c(0.8, 4.6, 2.4, 3.6))

g<-ggplot(df, aes(time, value, colour = group, group = group)) +
   geom_line() +
   geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2) +
   geom_ribbon(aes(ymin=lower, ymax=upper, fill = group), alpha=0.5)

print(g)

enter image description here

Dave2e
  • 22,192
  • 18
  • 42
  • 50