tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.39k stars 2k forks source link

Add gradient midpoint example #5896

Closed teunbrand closed 1 month ago

teunbrand commented 1 month ago

This PR aims to fix #3738 and replaces #5824.

Briefly, an example is included to use the rescaler argument to center a custom divergent scale. The example rendered:

library(ggplot2)
set.seed(1)
df <- data.frame(
  x = runif(100),
  y = runif(100),
  z1 = rnorm(100),
  z2 = abs(rnorm(100))
)

ggplot(df, aes(x, y)) +
  geom_point(aes(colour = z1)) +
  scale_colour_gradientn(
    colours = c("blue", "dodgerblue", "white", "orange", "red"),
    rescaler = ~ scales::rescale_mid(.x, mid = 0)
  )

Created on 2024-05-21 with reprex v2.1.0