color_scheme_set("gray")
x <- example_mcmc_draws(chains = 1)
dim(x)
colnames(x)
###################################
### vertical & horizontal lines ###
###################################
(p <- mcmc_intervals(x, regex_pars = "beta"))
# vertical line at zero (with some optional styling)
p + vline_0()
p + vline_0(linewidth = 0.25, color = "darkgray", linetype = 2)
# vertical line(s) at specified values
v <- c(-0.5, 0, 0.5)
p + vline_at(v, linetype = 3, linewidth = 0.25)
my_lines <- vline_at(v, alpha = 0.25, linewidth = 0.75 * c(1, 2, 1),
color = c("maroon", "skyblue", "violet"))
p + my_lines
# \donttest{
# add vertical line(s) at computed values
# (three ways of getting lines at column means)
color_scheme_set("brightblue")
p <- mcmc_intervals(x, regex_pars = "beta")
p + vline_at(x[, 3:4], colMeans)
p + vline_at(x[, 3:4], "colMeans", color = "darkgray",
lty = 2, linewidth = 0.25)
p + vline_at(x[, 3:4], function(a) apply(a, 2, mean),
color = "orange",
linewidth = 2, alpha = 0.1)
# }
# using the lbub function to get interval lower and upper bounds (lb, ub)
color_scheme_set("pink")
parsed <- ggplot2::label_parsed
p2 <- mcmc_hist(x, pars = "beta[1]", binwidth = 1/20,
facet_args = list(labeller = parsed))
(p2 <- p2 + facet_text(size = 16))
b1 <- x[, "beta[1]"]
p2 + vline_at(b1, fun = lbub(0.8), color = "gray20",
linewidth = 2 * c(1,.5,1), alpha = 0.75)
p2 + vline_at(b1, lbub(0.8, med = FALSE), color = "gray20",
linewidth = 2, alpha = 0.75)
##########################
### format axis titles ###
##########################
color_scheme_set("green")
y <- example_y_data()
yrep <- example_yrep_draws()
(p3 <- ppc_stat(y, yrep, stat = "median", binwidth = 1/4))
# turn off the legend, turn on x-axis title
p3 +
legend_none() +
xaxis_title(size = 13, family = "sans") +
ggplot2::xlab(expression(italic(T(y)) == median(italic(y))))
################################
### format axis & facet text ###
################################
color_scheme_set("gray")
p4 <- mcmc_trace(example_mcmc_draws(), pars = c("alpha", "sigma"))
myfacets <-
facet_bg(fill = "gray30", color = NA) +
facet_text(face = "bold", color = "skyblue", size = 14)
p4 + myfacets
# \donttest{
##########################
### control tick marks ###
##########################
p4 +
myfacets +
yaxis_text(FALSE) +
yaxis_ticks(FALSE) +
xaxis_ticks(linewidth = 1, color = "skyblue")
# }
##############################
### change plot background ###
##############################
color_scheme_set("blue")
# add grid lines
ppc_stat(y, yrep) + grid_lines()
# panel_bg vs plot_bg
ppc_scatter_avg(y, yrep) + panel_bg(fill = "gray90")
ppc_scatter_avg(y, yrep) + plot_bg(fill = "gray90")
color_scheme_set("yellow")
p5 <- ppc_scatter_avg(y, yrep, alpha = 1)
p5 + panel_bg(fill = "gray20") + grid_lines(color = "white")
# \donttest{
color_scheme_set("purple")
ppc_dens_overlay(y, yrep[1:30, ]) +
legend_text(size = 14) +
legend_move(c(0.75, 0.5)) +
plot_bg(fill = "gray90") +
panel_bg(color = "black", fill = "gray99", linewidth = 3)
# }
###############################################
### superimpose a function on existing plot ###
###############################################
# compare posterior of beta[1] to Gaussian with same posterior mean
# and sd as beta[1]
x <- example_mcmc_draws(chains = 4)
dim(x)
purple_gaussian <-
overlay_function(
fun = dnorm,
args = list(mean(x[,, "beta[1]"]), sd(x[,, "beta[1]"])),
color = "purple",
linewidth = 2
)
color_scheme_set("gray")
mcmc_hist(x, pars = "beta[1]", freq = FALSE) + purple_gaussian
# \donttest{
mcmc_dens(x, pars = "beta[1]") + purple_gaussian
# }
Run the code above in your browser using DataLab