Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In ggplot2, specify a confidence interval (95% CI) around geom_smooth (or any trend line) using existing CI variables

Background:

I have a df tracking someone's weight over time (not mine I swear):

df <- data.frame(weight = c(156.8, 170,  185,   199.0, 203,  208), 
                lower95 = c(151.2, 165,  181.6, 194.5, 202,  206.4), 
                upper95 = c(162.8, 175,  187.9, 203.7, 204,  211.9),
                year =    c(2010,  2014, 2017,  2019,  2020, 2021)) 

I'm trying to make a simple geom_smooth like Hadley's in R4DS, like this one:

data(mpg)
ggplot(data = mpg) + 
  geom_smooth(mapping = aes(x = displ, y = hwy))

I like this because it has a shaded area around the main trends like that represents a 95% confidence interval. (I'm not sure how this MPG example is pulling a CI from the MPG data -- I don't see an SE variable -- but it's mostly beside the point for this question. If anyone has a good answer, lmk!).

Problem:

As you can see in my dataset, I've got 2 columns, lower95 and upper95 that contain the CI for each observation. How can I employ those as geom_smooth's shaded area? Or, if not geom_smooth, then any trend line?

What I've tried:

I'm fiddling with things like geom_line and geom_ribbon but only getting errors:

ggplot(data = df) +
  geom_line(data = df$weight) +
  geom_ribbon(data = df, aes(ymin = df$lower95, ymax = df$upper95))
like image 257
logjammin Avatar asked Jan 25 '26 07:01

logjammin


1 Answers

You're pretty close with your last attempt, but you never specified x and y! You also shouldn't use data$column inside aes(), it expects unquoted column names only, and you should put the ribbon first so the line is on top. And you don't need to keep specifying data = df unless you're using different data frames in different layers. Give this a go:

ggplot(data = df, aes(x = year, y = weight) +
  geom_ribbon(aes(ymin = lower95, ymax = upper95)) +
  geom_line()
like image 173
Gregor Thomas Avatar answered Jan 27 '26 23:01

Gregor Thomas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!