How to plot a CI only in the predicted line

Viewed 49

I used a GNM (Generalised Non-Linear Model) to make a prediction and stored all the data in a data frame. I also stored a pred_up and pred_down variables, which are simply the upper and lower part of the confidence interval, which I would like to plot as a shaded area. By the code bellow I get the following chart

ggplot(plot_males, aes(x = year, y = real_data)) +
        geom_line(aes(color = "Obs"), na.rm=TRUE) +
        geom_line(aes(x = year, y = pred, color = "Prediction"), na.rm=TRUE) +
        geom_line(aes(x = year, y = pred_up, color = "CI"), na.rm=TRUE) +
        geom_line(aes(x = year, y = pred_down, color = "CI"), na.rm=TRUE)

enter image description here

What I am trying to accomplish is something like the chart bellow, with the confidence interval area shaded. I know that the ggplot2 has a way of plotting confidence intervals directly but it does not has a GNM method so that is why I am storing my upper and lower CI intervals in the dataset.

Is there a way of shading the interval?

enter image description here

1 Answers

Try to add

geom_ribbon(aes(x = year, ymin = pred_down, ymax = pred_up), 
              fill = "red", alpha = 0.15, na.rm = TRUE)
Related