I am trying to plot a density line with and want to shade or fill only the area associated with the 95% of the x axis. I am trying to follow answers given in the attached answers, but non of them talk about shading an area when we are plotting more than one distributions at the same time with a grouping factor. In this case the grouping factor is the different central electrodes ("Fz", "Cz, "Pz"). I am trying to visualise something similar to the highest density interval, or the area under the curve comprising between percentile 5 and 95.
My data looks something like this:
> head(dframe1)
x y Electrode
1 1.571296 0.0001474116 Fz
2 1.576496 0.0001487649 Fz
3 1.581697 0.0001497564 Fz
4 1.586897 0.0001504074 Fz
5 1.592098 0.0001507446 Fz
6 1.597298 0.0001507776 Fz
And at the moment the code I am using to plot the distributions by group in ggplot looks like this:
p1 <- ggplot(data = dframe1, mapping = aes(x = x, y = y)) +
geom_density_line(stat = "identity", size=.5, alpha=0.3, aes(color=Electrode, fill=Electrode)) +
scale_fill_discrete(breaks=c("Fz","Cz","Pz")) +
guides(colour = FALSE) +
geom_vline(xintercept = 0) +
xlab("values") +
xlim(-2, 10) +
ylab("density") +
ylim(0, .7) +
theme(axis.text=element_text(size=12),
axis.title=element_text(size=16),
plot.title = element_text(size=18)) +
labs(title="Interval")
I sketched something similar to what I am looking for:
Of course I could use bayestestR HDI standard output but I prefer the ggplot aesthetic and flexibility.
Any help will be much appreciated.
dput()
the plot is empty when we use the small data you shared! – Duckdnorm
to avoid the need todput
a large data frame and permit easy validation of solutions. – Limeydput()
so I did not know what to do. Thanks for your comments though. – Unai Vicente