Skip to content

Commit f29b837

Browse files
committed
Minor tweaks to doc and internals
1 parent 48417de commit f29b837

File tree

3 files changed

+73
-42
lines changed

3 files changed

+73
-42
lines changed

R/bayesplot-ggplot-themes.R

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -73,33 +73,39 @@ theme_default <-
7373
)
7474
}
7575

76-
bayes_theme_env <- new.env(parent = emptyenv())
77-
bayes_theme_env$current <- theme_default()
78-
bayes_theme_env$gg_current <- ggplot2::theme_grey()
7976

8077
#' Get, set, and modify the active bayesplot theme
8178
#'
82-
#' These functions are the \pkg{bayesplot} equivalent to
83-
#' \code{\link[ggplot2]{theme_set}} and friends. They set, get, and update the
84-
#' active theme but only apply them to \code{bayesplots}. The current/active
85-
#' theme is automatically applied to every \code{bayesplot} you draw. Use
86-
#' \code{bayesplot_theme_get} to get the current \pkg{bayesplot} theme, and
87-
#' \code{bayesplot_theme_set} to change it. \code{bayesplot_theme_update} and
88-
#' \code{bayesplot_theme_replace} are shorthands for changing individual
89-
#' elements.
79+
#' @description These functions are the \pkg{bayesplot} equivalent to
80+
#' \pkg{ggplot2}'s \code{\link[ggplot2]{theme_set}} and friends. They set,
81+
#' get, and update the active theme but only apply them to \code{bayesplots}.
82+
#' The current/active theme is automatically applied to every \code{bayesplot}
83+
#' you draw.
84+
#'
85+
#' Use \code{bayesplot_theme_get} to get the current \pkg{bayesplot} theme,
86+
#' and \code{bayesplot_theme_set} to change it. \code{bayesplot_theme_update}
87+
#' and \code{bayesplot_theme_replace} are shorthands for changing individual
88+
#' elements.
9089
#'
9190
#' @details \code{bayesplot_theme_set} and friends only apply to
92-
#' \code{bayesplots}. Setting a theme other than the \pkg{ggplot2} default
93-
#' (\code{\link[ggplot2]{theme_grey}}) will override any \pkg{bayesplot} themes.
91+
#' \code{bayesplots}. However, \code{ggplot2::theme_set} can also be used to
92+
#' change the \pkg{bayesplot} theme. Currently, setting a theme with
93+
#' \code{ggplot2::theme_set} (other than the \pkg{ggplot2} default
94+
#' \code{\link[ggplot2]{theme_grey}}) will override the \pkg{bayesplot} theme.
9495
#'
95-
#' @inheritParams ggplot2::theme_set
9696
#' @export
97+
#' @param new The new theme (list of theme elements) to use. This is analogous
98+
#' to the \code{new} argument to \code{\link[ggplot2]{theme_set}}.
99+
#' @param ... A named list of theme settings.
97100
#'
98-
#' @examples
101+
#' @template seealso-helpers
102+
#' @template seealso-colors
99103
#'
104+
#' @examples
100105
#' library(ggplot2)
101106
#'
102-
#' # plot using the default theme automatically
107+
#' # plot using the current value of bayesplot_theme_get()
108+
#' # (the default is bayesplot::theme_default())
103109
#' x <- example_mcmc_draws()
104110
#' mcmc_hist(x)
105111
#'
@@ -115,18 +121,19 @@ bayes_theme_env$gg_current <- ggplot2::theme_grey()
115121
#' mcmc_areas(x, regex_pars = "beta")
116122
#'
117123
#' # change back to the default
118-
#' bayesplot_theme_set(theme_default())
124+
#' bayesplot_theme_set() # same as bayesplot_theme_set(theme_default())
119125
#' mcmc_areas(x, regex_pars = "beta")
120126
#'
121127
#' # change theme for all ggplots
122128
#' theme_set(theme_dark())
123129
#' mcmc_dens_overlay(x)
130+
#'
124131
bayesplot_theme_get <- function() {
125-
if (!identical(bayes_theme_env$gg_current, ggplot2::theme_get())) {
126-
bayes_theme_env$gg_current <- ggplot2::theme_get()
127-
thm <- bayes_theme_env$gg_current
132+
if (!identical(.bayesplot_theme_env$gg_current, ggplot2::theme_get())) {
133+
.bayesplot_theme_env$gg_current <- ggplot2::theme_get()
134+
thm <- .bayesplot_theme_env$gg_current
128135
} else {
129-
thm <- bayes_theme_env$current
136+
thm <- .bayesplot_theme_env$current
130137
}
131138
thm
132139
}
@@ -135,14 +142,14 @@ bayesplot_theme_get <- function() {
135142
#' @export
136143
bayesplot_theme_set <- function(new = theme_default()) {
137144
missing <- setdiff(names(ggplot2::theme_gray()), names(new))
138-
if (length(missing) > 0) {
145+
if (length(missing)) {
139146
warning("New theme missing the following elements: ",
140147
paste(missing, collapse = ", "), call. = FALSE)
141148
}
142149

143-
old <- bayes_theme_env$current
144-
bayes_theme_env$current <- new
145-
bayes_theme_env$gg_current <- ggplot2::theme_get()
150+
old <- .bayesplot_theme_env$current
151+
.bayesplot_theme_env$current <- new
152+
.bayesplot_theme_env$gg_current <- ggplot2::theme_get()
146153
invisible(old)
147154
}
148155

@@ -158,3 +165,12 @@ bayesplot_theme_update <- function(...) {
158165
bayesplot_theme_replace <- function(...) {
159166
bayesplot_theme_set(bayesplot_theme_get() %+replace% ggplot2::theme(...))
160167
}
168+
169+
170+
171+
# internal ----------------------------------------------------------------
172+
173+
.bayesplot_theme_env <- new.env(parent = emptyenv())
174+
.bayesplot_theme_env$current <- theme_default()
175+
.bayesplot_theme_env$gg_current <- ggplot2::theme_grey()
176+

R/zzz.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
.onAttach <- function(...) {
22
ver <- utils::packageVersion("bayesplot")
33
packageStartupMessage("This is bayesplot version ", ver)
4-
packageStartupMessage("- bayesplot theme set to bayesplot::theme_default(). Change it")
5-
packageStartupMessage(" with ggplot2::theme_set() or bayesplot::bayesplot_theme_set()")
6-
packageStartupMessage("- Online documentation at mc-stan.org/bayesplot")
4+
packageStartupMessage("- Online documentation and vignettes at mc-stan.org/bayesplot")
5+
packageStartupMessage("- bayesplot theme set to bayesplot::theme_default()")
6+
packageStartupMessage(" * Does _not_ affect other ggplot2 plots")
7+
packageStartupMessage(" * See ?bayesplot_theme_set for details on theme setting")
78
}

man/bayesplot_theme_get.Rd

Lines changed: 28 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)