| Title: | Multiple Fill and Colour Scales in 'ggplot2' |
|---|---|
| Description: | Use multiple fill and colour scales in 'ggplot2'. |
| Authors: | Elio Campitelli [cre, aut] (ORCID: <https://orcid.org/0000-0002-7742-9230>) |
| Maintainer: | Elio Campitelli <[email protected]> |
| License: | GPL-3 |
| Version: | 0.5.2.9000 |
| Built: | 2026-05-07 08:33:35 UTC |
| Source: | https://github.com/eliocamp/ggnewscale |
Add method to plots that have new scales
## S3 method for class 'ggplot_rename_next' e1 + e2## S3 method for class 'ggplot_rename_next' e1 + e2
e1 |
PLot |
e2 |
element to add to the plot (layer, scale, guides, list of layers). |
Creates a new scale "slot". Geoms added to a plot after this function will use a new scale definition.
new_scale(new_aes) new_scale_fill() new_scale_color() new_scale_colour()new_scale(new_aes) new_scale_fill() new_scale_color() new_scale_colour()
new_aes |
A string with the name of the aesthetic for which a new scale will be created. |
new_scale_color(), new_scale_colour() and new_scale_fill() are just
aliases to new_scale("color"), etc...
library(ggplot2) # Equivalent to melt(volcano), but we don't want to depend on reshape2 topography <- expand.grid(x = 1:nrow(volcano), y = 1:ncol(volcano)) topography$z <- c(volcano) # point measurements of something at a few locations 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")library(ggplot2) # Equivalent to melt(volcano), but we don't want to depend on reshape2 topography <- expand.grid(x = 1:nrow(volcano), y = 1:ncol(volcano)) topography$z <- c(volcano) # point measurements of something at a few locations 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")
This is a slightly more focused version of new_scale() which
supports explicit rename of an aesthetic mapping.
rename_aes(...) clear_aes()rename_aes(...) clear_aes()
... |
name-value pair with the new name of the aes and the original name it's replacing. |
library(ggplot2) # Equivalent to melt(volcano), but we don't want to depend on reshape2 topography <- expand.grid(x = 1:nrow(volcano), y = 1:ncol(volcano)) topography$z <- c(volcano) # point measurements of something at a few locations measurements <- data.frame(x = runif(30, 1, 80), y = runif(30, 1, 60), thing = rnorm(30)) ggplot(mapping = aes(x, y)) + rename_aes(topo_color = "color") + geom_contour(data = topography, aes(z = z, topo_color = stat(level))) + # Color scale for topography scale_color_viridis_c(option = "D") + clear_aes() + # geoms below will use another color scale geom_point(data = measurements, size = 3, aes(colour = thing)) + # Color scale applied to geoms added after new_scale_color() scale_color_viridis_c(option = "A")library(ggplot2) # Equivalent to melt(volcano), but we don't want to depend on reshape2 topography <- expand.grid(x = 1:nrow(volcano), y = 1:ncol(volcano)) topography$z <- c(volcano) # point measurements of something at a few locations measurements <- data.frame(x = runif(30, 1, 80), y = runif(30, 1, 60), thing = rnorm(30)) ggplot(mapping = aes(x, y)) + rename_aes(topo_color = "color") + geom_contour(data = topography, aes(z = z, topo_color = stat(level))) + # Color scale for topography scale_color_viridis_c(option = "D") + clear_aes() + # geoms below will use another color scale geom_point(data = measurements, size = 3, aes(colour = thing)) + # Color scale applied to geoms added after new_scale_color() scale_color_viridis_c(option = "A")