0

I am using a model to predict some numbers. My prediction also includes a confidence interval for each number. I need to plot the actual numbers + predicted numbers and their quantile values on the same plot. Here is a simple example:

actualVals = c(12,20,15,30)
lowQuantiles = c(19,15,12,18)
midQuantiles = c(22,22,17,25)
highQuantiles = c(30,25,25,30)

and I'm looking for something like this, perhaps by using ggplot(): enter image description here

Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
Mohammad
  • 1,078
  • 2
  • 18
  • 39
  • What did you try so far? –  Oct 15 '15 at 02:00
  • @Pascal I tried geom_boxplot() which is not exactly what I want. In my case I have many data points I want to show so fat boxplots do not work in my case. I'm also confused how to plot the line (red) and boxplots on the same plot using ggplot. As you probably know people usually use the "melt" function to plot two curves on the same plot. I used to code in Matlab before and it felt much easier for doing similar tasks like this!! – Mohammad Oct 15 '15 at 02:11

1 Answers1

1

You can use geom_errorbar, among others you can see at ?geom_errorbar. I created a data.frame from your variables, dat and added dat$x <- 1:4.

ggplot(dat) +
  geom_errorbar(aes(x, y=midQuantiles, ymax=highQuantiles, ymin=lowQuantiles, width=0.2), lwd=2, color="blue") +
  geom_point(aes(x, midQuantiles), cex=4, shape=22, fill="grey", color="black") +
  geom_line(aes(x, actualVals), color="maroon", lwd=2) +
  geom_point(aes(x, actualVals), shape=21, cex=4, fill="white", color='maroon') +
  ylim(0, 30) +
  theme_bw()

enter image description here

Rorschach
  • 31,301
  • 5
  • 78
  • 129