Experiment
GuideColourbar2 <- ggproto("GuideColourbar2", `_inherit` = GuideColourbar)
guide_colorbar2 <- function (title = waiver(), theme = NULL, nbin = NULL, display = "raster",
raster = deprecated(), alpha = NA, draw.ulim = TRUE, draw.llim = TRUE,
angle = NULL, position = NULL, direction = NULL, reverse = FALSE,
order = 0, available_aes = c("colour2", "color2", "fill2"),
...)
{
if (lifecycle::is_present(raster)) {
deprecate_soft0("3.5.0", "guide_colourbar(raster)", "guide_colourbar(display)")
check_bool(raster)
display <- if (raster)
"raster"
else "rectangles"
}
display <- arg_match0(display, c("raster", "rectangles",
"gradient"))
nbin <- nbin %||% switch(display, gradient = 15, 300)
theme <- deprecated_guide_args(theme, ...)
if (!is.null(position)) {
position <- arg_match0(position, c(.trbl, "inside"))
}
check_number_decimal(alpha, min = 0, max = 1, allow_na = TRUE)
new_guide(title = title, theme = theme, nbin = nbin, display = display,
alpha = alpha, angle = angle, draw_lim = c(isTRUE(draw.llim),
isTRUE(draw.ulim)), position = position, direction = direction,
reverse = reverse, order = order, available_aes = available_aes,
name = "colourbar2", super = GuideColourbar2)
}
scale_fill2_viridis_c <- function (name = waiver(), ..., alpha = 1, begin = 0, end = 1,
direction = 1, option = "D", values = NULL, space = "Lab",
na.value = "grey50", guide = "colourbar2", aesthetics = "fill")
{
continuous_scale(aesthetics, name = name, palette = scales::pal_gradient_n(scales::pal_viridis(alpha,
begin, end, direction, option)(6), values, space), na.value = na.value,
guide = guide, ...)
}
sf::st_read(system.file("shape/nc.shp", package="sf")) |>
tibble() |>
ggplot() +
aes(geometry = geometry) +
geom_sf()
## Reading layer `nc' from data source
## `/Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/library/sf/shape/nc.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 100 features and 14 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
## Geodetic CRS: NAD27

last_plot() +
aes(fill2 = as.numeric(FIPS)) +
scale_fill2_viridis_c()
## Error in `validate_guide()`:
## ! Unknown guide: colourbar2
library(ggplot2)
library(ggnewscale)
# Equivalent to melt(volcano)
topography <- expand.grid(x = 1:nrow(volcano),
y = 1:ncol(volcano))
topography$z <- c(volcano)
# point measurements of something at a few locations
set.seed(42)
measurements <- data.frame(x = runif(30, 1, 80),
y = runif(30, 1, 60),
thing = rnorm(30))
ggplot(mapping = aes(x, y)) +
geom_contour(data = topography, aes(z = z, color = stat(level))) +
# Color scale for topography
scale_color_viridis_c(option = "D") +
# geoms below will use another color scale
new_scale_color() +
geom_point(data = measurements, size = 3, aes(color = thing)) +
# Color scale applied to geoms added after new_scale_color()
scale_color_viridis_c(option = "A")
## Warning: `stat(level)` was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(level)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

p <- last_plot()
p$scales$scales[[1]]$aesthetics
## [1] "colour_new"
p$scales$scales[[2]]$aesthetics
## [1] "colour"
p$scales$scales[[1]]$guide
## <ggproto object: Class GuideColourbar, GuideLegend, Guide, gg>
## add_title: function
## arrange_layout: function
## assemble_drawing: function
## available_aes: colour_new color fill
## build_decor: function
## build_labels: function
## build_ticks: function
## build_title: function
## draw: function
## draw_early_exit: function
## elements: list
## extract_decor: function
## extract_key: function
## extract_params: function
## get_layer_key: function
## hashables: list
## measure_grobs: function
## merge: function
## override_elements: function
## params: list
## process_layers: function
## setup_elements: function
## setup_params: function
## train: function
## transform: function
## super: <ggproto object: Class GuideColourbar, GuideLegend, Guide, gg>
p$scales$scales[[2]]$guide
## [1] "colourbar"