I thought this should be straightforward, but I'm lost, despite tons of information online.
My Problem: I have a vector of data points, for which I want to plot a density curve, then color the area under the curve to signify the Highest Density Interval (HDI). Naturally, I'm trying to achieve this with ggplot2
package, and specifically with qplot()
, since my data comes as a vector, and not a data frame.
Reproducible Example
library(ggplot2)
library(HDInterval)
## create data vector
set.seed(789)
dat <- rnorm(1000)
## plot density curve with qplot and mark 95% hdi
qplot(dat, geom = "density")+
geom_vline(aes(xintercept = c(hdi(dat))))
So I get this:
But what I really want is something like this:
Is there a simple way to achieve this with ggplot2::qplot
?