Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 41 additions & 29 deletions R/mcmc-distributions.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@
#' }
#' # example of using 'transformations' argument to plot log(sigma),
#' # and parsing facet labels (e.g. to get greek letters for parameters)
#' mcmc_hist(x, transformations = list(sigma = "log"),
#' facet_args = list(labeller = ggplot2::label_parsed)) +
#' facet_text(size = 15)
#' mcmc_hist(x,
#' transformations = list(sigma = "log"),
#' facet_args = list(labeller = ggplot2::label_parsed)
#' ) +
#' facet_text(size = 15)
#' \donttest{
#' # instead of list(sigma = "log"), you could specify the transformation as
#' # list(sigma = log) or list(sigma = function(x) log(x)), but then the
Expand All @@ -85,14 +87,18 @@
#' ### Densities ###
#' #################
#'
#' mcmc_dens(x, pars = c("sigma", "beta[2]"),
#' facet_args = list(nrow = 2))
#' mcmc_dens(x,
#' pars = c("sigma", "beta[2]"),
#' facet_args = list(nrow = 2)
#' )
#' \donttest{
#' # separate and overlay chains
#' color_scheme_set("mix-teal-pink")
#' mcmc_dens_overlay(x, pars = c("sigma", "beta[2]"),
#' facet_args = list(nrow = 2)) +
#' facet_text(size = 14)
#' mcmc_dens_overlay(x,
#' pars = c("sigma", "beta[2]"),
#' facet_args = list(nrow = 2)
#' ) +
#' facet_text(size = 14)
#' x2 <- example_mcmc_draws(params = 6)
#' mcmc_dens_chains(x2, pars = c("beta[1]", "beta[2]", "beta[3]"))
#' }
Expand Down Expand Up @@ -170,7 +176,10 @@ mcmc_dens <- function(
bounds = bounds,
alpha = alpha,
...
)
) +
yaxis_text(FALSE) +
yaxis_title(FALSE) +
yaxis_ticks(FALSE)
Comment on lines +179 to +182
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are already included in the internal .mcmc_dens() function so we don't need to add them again here. These don't turn off the y-axis, they just turn off the text and tick marks, but those were already off.

}

#' @rdname MCMC-distributions
Expand Down Expand Up @@ -237,7 +246,10 @@ mcmc_dens_overlay <- function(
n_dens = n_dens,
bounds = bounds,
...
)
) +
yaxis_text(FALSE) +
yaxis_title(FALSE) +
yaxis_ticks(FALSE)
Comment on lines +250 to +252
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are already included in the internal .mcmc_dens() function so we don't need to add them again here. These don't turn off the y-axis, they just turn off the text and tick marks, but those were already off.

}

#' @rdname MCMC-distributions
Expand Down Expand Up @@ -281,7 +293,8 @@ mcmc_dens_chains <- function(
} else {
scale_color <- scale_color_manual(
values = rep(get_color("m"), n_chains),
guide = "none")
guide = "none"
)
}

ggplot(data) +
Expand Down Expand Up @@ -371,9 +384,6 @@ mcmc_violin <- function(
)
}




# internal -----------------------------------------------------------------
.mcmc_hist <- function(
x,
Expand All @@ -398,7 +408,7 @@ mcmc_violin <- function(
data <- melt_mcmc(x, value.name = "value")
n_param <- num_params(data)

graph <- ggplot(data, aes(x = ~ value)) +
graph <- ggplot(data, aes(x = ~value)) +
geom_histogram(
set_hist_aes(freq),
fill = get_color("mid"),
Expand Down Expand Up @@ -450,16 +460,15 @@ mcmc_violin <- function(
color_chains = FALSE,
geom = c("density", "violin"),
probs = c(0.1, 0.5, 0.9),
trim = FALSE,
alpha = 1,
bw = NULL,
adjust = NULL,
kernel = NULL,
n_dens = NULL,
bounds = NULL,
...
) {

trim = FALSE,
alpha = 1,
bw = NULL,
adjust = NULL,
kernel = NULL,
n_dens = NULL,
bounds = NULL,
...
) {
bw <- bw %||% "nrd0"
adjust <- adjust %||% 1
kernel <- kernel %||% "gaussian"
Expand Down Expand Up @@ -534,16 +543,19 @@ mcmc_violin <- function(
} else {
scale_color <- scale_color_manual(
values = rep(get_color("m"), n_chains),
guide = "none")
guide = "none"
)
}
graph <- graph + scale_color
}

if (n_param == 1) {
graph <-
graph +
labs(x = if (violin) "Chain" else levels(data$Parameter),
y = if (violin) levels(data$Parameter) else NULL)
labs(
x = if (violin) "Chain" else levels(data$Parameter),
y = if (violin) levels(data$Parameter) else NULL
)
} else {
facet_args[["facets"]] <- vars(.data$Parameter)
facet_args[["scales"]] <- facet_args[["scales"]] %||% "free"
Expand All @@ -555,6 +567,6 @@ mcmc_violin <- function(
bayesplot_theme_get() +
yaxis_text(FALSE) +
yaxis_ticks(FALSE) +
yaxis_title(on = n_param == 1 && violin) +
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to keep this for the special case of 1 parameter and a violin plot. If you look at a 1 parameter violin plot you'll see why (the y-axis provides the parameter name)

yaxis_title(FALSE) +
xaxis_title(on = n_param == 1)
}