library(ggplot2); library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ tibble 3.1.2 ✓ dplyr 1.0.8
## ✓ tidyr 1.2.0 ✓ stringr 1.4.0
## ✓ readr 2.1.2 ✓ forcats 0.5.1
## ✓ purrr 0.3.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(ggthemes); library(gridExtra)
##
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
##
## combine
In this program we will show how to (a) highlight an area under the Normal distribution curve, and (b) conduct a triplot in Bayesian analysis using ggplot2.
In the following analysis, we will simulate a normal distribution with mean = -0.96 and sd = 1.922. We will then highlight AUC for x <= -1, from -1 to 1, and x >= 1.
ggplot(NULL, aes(c(-8,6))) +
geom_area(stat = "function", fun = dnorm, args=list(mean=-0.96, sd=1.922), fill = "red", xlim = c(-8, -1)) +
geom_area(stat = "function", fun = dnorm, args=list(mean=-0.96, sd=1.922), fill = "orange", xlim = c(-1, +1)) +
geom_area(stat = "function", fun = dnorm, args=list(mean=-0.96, sd=1.922), fill = "green", xlim = c(1, 6)) +
labs(x="Percent change in BMD", y="Probability density") + scale_x_continuous(breaks = seq(from=-7, to=6, by=1))
In the following codes we will plot 3 normal curves representing prior, likelihood and posterior distribution.
ggplot(data.frame(x = c(-6, 6)), aes(x)) + scale_x_continuous(breaks = seq(from=-6, to=6, by=1)) +
mapply(function(mean, sd, col) {
stat_function(fun=dnorm, args=list(mean=mean, sd=sd), col=col)
},
# enter means, standard deviations and colors here
mean = c(-1.01, -1.3, -0.96),
sd = c(2.2, 2.12, 1.92),
col = c("grey", "red", "blue")) + labs(x="Percent change in BMD", y="Probability density")