Skip to content

Commit 20de7be

Browse files
committed
update examples
1 parent ab44a18 commit 20de7be

File tree

3 files changed

+68
-92
lines changed

3 files changed

+68
-92
lines changed

R/scale-colour.R

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -63,26 +63,28 @@
6363
#' see the [paper on the colorspace package](https://arxiv.org/abs/1903.06490)
6464
#' and references therein.
6565
#' @examples
66-
#' v <- ggplot(faithfuld, aes(waiting, eruptions, fill = density)) +
67-
#' geom_tile()
68-
#' v
66+
#' # A standard plot
67+
#' p <- ggplot(mpg, aes(displ, hwy, colour = cty)) +
68+
#' geom_point()
6969
#'
70-
#' v + scale_fill_continuous(type = "gradient")
71-
#' v + scale_fill_continuous(type = "viridis")
70+
#' # You can use the scale to give a palette directly
71+
#' p + scale_colour_continuous(palette = c("#FEE0D2", "#FC9272", "#DE2D26"))
7272
#'
73-
#' # The above are equivalent to
74-
#' v + scale_fill_gradient()
75-
#' v + scale_fill_viridis_c()
73+
#' # The default colours are encoded into the theme
74+
#' p + theme(palette.colour.continuous = c("#DEEBF7", "#9ECAE1", "#3182BD"))
7675
#'
77-
#' # To make a binned version of this plot
78-
#' v + scale_fill_binned(type = "viridis")
76+
#' # You can globally set default colour palette via the theme
77+
#' old <- update_theme(palette.colour.continuous = c("#E5F5E0", "#A1D99B", "#31A354"))
7978
#'
80-
#' # Set a different default scale using the options
81-
#' # mechanism
82-
#' tmp <- getOption("ggplot2.continuous.fill") # store current setting
83-
#' options(ggplot2.continuous.fill = scale_fill_distiller)
84-
#' v
85-
#' options(ggplot2.continuous.fill = tmp) # restore previous setting
79+
#' # Plot now shows new global default
80+
#' p
81+
#'
82+
#' # The default binned colour scale uses the continuous palette
83+
#' p + scale_colour_binned() +
84+
#' theme(palette.colour.continuous = c("#EFEDF5", "#BCBDDC", "#756BB1"))
85+
#'
86+
#' # Restoring the previous theme
87+
#' theme_set(old)
8688
#' @export
8789
scale_colour_continuous <- function(..., palette = NULL, aesthetics = "colour",
8890
guide = "colourbar", na.value = "grey50",
@@ -190,38 +192,24 @@ scale_fill_binned <- function(..., palette = NULL, aesthetics = "fill", guide =
190192
#' @seealso
191193
#' The `r link_book("discrete colour scales section", "scales-colour#sec-colour-discrete")`
192194
#' @examples
193-
#' # Template function for creating densities grouped by a variable
194-
#' cty_by_var <- function(var) {
195-
#' ggplot(mpg, aes(cty, colour = factor({{var}}), fill = factor({{var}}))) +
196-
#' geom_density(alpha = 0.2)
197-
#' }
195+
#' # A standard plot
196+
#' p <- ggplot(mpg, aes(displ, hwy, colour = class)) +
197+
#' geom_point()
198+
#'
199+
#' # You can use the scale to give a palette directly
200+
#' p + scale_colour_discrete(palette = pal_brewer(palette = "Dark2"))
198201
#'
199-
#' # The default, scale_fill_hue(), is not colour-blind safe
200-
#' cty_by_var(class)
202+
#' # The default colours are encoded into the theme
203+
#' p + theme(palette.colour.discrete = pal_grey())
201204
#'
202-
#' # (Temporarily) set the default to Okabe-Ito (which is colour-blind safe)
203-
#' okabe <- c("#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
204-
#' withr::with_options(
205-
#' list(ggplot2.discrete.fill = okabe),
206-
#' print(cty_by_var(class))
207-
#' )
205+
#' # You can globally set default colour palette via the theme
206+
#' old <- update_theme(palette.colour.discrete = pal_viridis())
208207
#'
209-
#' # Define a collection of palettes to alter the default based on number of levels to encode
210-
#' discrete_palettes <- list(
211-
#' c("skyblue", "orange"),
212-
#' RColorBrewer::brewer.pal(3, "Set2"),
213-
#' RColorBrewer::brewer.pal(6, "Accent")
214-
#' )
215-
#' withr::with_options(
216-
#' list(ggplot2.discrete.fill = discrete_palettes), {
217-
#' # 1st palette is used when there 1-2 levels (e.g., year)
218-
#' print(cty_by_var(year))
219-
#' # 2nd palette is used when there are 3 levels
220-
#' print(cty_by_var(drv))
221-
#' # 3rd palette is used when there are 4-6 levels
222-
#' print(cty_by_var(fl))
223-
#' })
208+
#' # Plot now shows new global default
209+
#' p
224210
#'
211+
#' # Restoring the previous theme
212+
#' theme_set(old)
225213
scale_colour_discrete <- function(..., palette = NULL, aesthetics = "colour", na.value = "grey50",
226214
type = getOption("ggplot2.discrete.colour")) {
227215
if (!is.null(type) && is.null(palette)) {

man/scale_colour_continuous.Rd

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

man/scale_colour_discrete.Rd

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

0 commit comments

Comments
 (0)