### Install ggnewscale Development Version Source: https://github.com/eliocamp/ggnewscale/blob/master/README.md Install the latest development version of ggnewscale directly from GitHub. Ensure the devtools package is installed first. ```r # install.packages("devtools") devtools::install_github("eliocamp/ggnewscale") ``` -------------------------------- ### Working with guides() outside of scales Source: https://context7.com/eliocamp/ggnewscale/llms.txt ggnewscale correctly tracks guide definitions even when attached with a standalone guides() call rather than inline in a scale. This example shows two independent guides() calls for the same aesthetic ('colour'), each retaining its own guide layout. ```r library(ggplot2) library(ggnewscale) ggplot(head(iris, 10), aes(Sepal.Length, Sepal.Width)) + geom_point(aes(colour = factor(Petal.Length)), size = 4) + guides(colour = guide_legend(ncol = 2, nrow = 2, order = 1)) + new_scale_colour() + geom_point(aes(colour = factor(Petal.Width)), size = 2, shape = 17) + guides(colour = guide_legend(ncol = 1, nrow = 4, order = 2)) + labs(title = "Independent guides() calls per scale", colour = NULL) ``` -------------------------------- ### Install ggnewscale Package Source: https://context7.com/eliocamp/ggnewscale/llms.txt Install the ggnewscale package from CRAN for the stable version or from GitHub for the development version. ```r install.packages("ggnewscale") ``` ```r # install.packages("devtools") devtools::install_github("eliocamp/ggnewscale") ``` -------------------------------- ### GPL Notice for Terminal Applications Source: https://github.com/eliocamp/ggnewscale/blob/master/LICENSE.md Display this short notice when a command-line program starts in interactive mode. It informs users about the warranty and redistribution terms, directing them to 'show w' and 'show c' for details. ```text ggnewscale Copyright (C) 2018 Elio Campitelli This program comes with ABSOLUTELY NO WARRANTY; for details type 'show w'. This is free software, and you are welcome to redistribute it under certain conditions; type 'show c' for details. ``` -------------------------------- ### Install ggnewscale from CRAN Source: https://github.com/eliocamp/ggnewscale/blob/master/README.md Use this command to install the stable version of the ggnewscale package from the Comprehensive R Archive Network (CRAN). ```r install.packages("ggnewscale") ``` -------------------------------- ### Get Citation for ggnewscale Package Source: https://github.com/eliocamp/ggnewscale/blob/master/README.md Use this R code to retrieve the suggested citation for the ggnewscale package, including a BibTeX entry for LaTeX users. ```r citation("ggnewscale") #> To cite ggnewscale in publications use: #> #> Campitelli E (????). _ggnewscale: Multiple Fill and Colour Scales in #> 'ggplot2'_. doi:10.5281/zenodo.2543762 #> , R package version 0.5.2. #> #> A BibTeX entry for LaTeX users is #> #> @Manual{R-ggnewscale, #> title = {ggnewscale: Multiple Fill and Colour Scales in 'ggplot2'}, #> author = {Elio Campitelli}, #> note = {R package version 0.5.2}, #> doi = {10.5281/zenodo.2543762}, #> } ``` -------------------------------- ### Convenience Wrapper new_scale_colour() for Colour Scales Source: https://context7.com/eliocamp/ggnewscale/llms.txt Use new_scale_colour() (or new_scale_color()) as a shorthand for new_scale('colour'). This example applies two independent color scales to mtcars data, one for cylinder count and another for gear count. ```r library(ggplot2) library(ggnewscale) ggplot(mtcars, aes(mpg, disp)) + # First colour scale: cylinder count, qualitative brewer palette geom_point(aes(colour = factor(cyl)), size = 7) + scale_colour_brewer(type = "qual", name = "Cylinders", guide = guide_legend(order = 1)) + new_scale_colour() + # <-- open second colour scale # Second colour scale: gear count, Set1 palette geom_point(aes(colour = factor(gear)), size = 3) + scale_colour_brewer(palette = "Set1", name = "Gears", guide = guide_legend(order = 2)) + labs(title = "mtcars: cylinders and gears as independent colour scales") # Both legends appear; inner points show gears, outer points show cylinders ``` -------------------------------- ### Open a New Scale Slot with new_scale() Source: https://context7.com/eliocamp/ggnewscale/llms.txt Use new_scale() to insert a scale boundary, allowing subsequent geoms to use a new, independent scale for a specified aesthetic. Previously added layers remain unaffected. This example demonstrates two independent color scales for elevation contours and measurement points. ```r library(ggplot2) library(ggnewscale) # Topographic surface data (built-in volcano dataset) topography <- expand.grid(x = 1:nrow(volcano), y = 1:ncol(volcano)) topography$z <- c(volcano) # Sparse point measurements at random locations set.seed(42) measurements <- data.frame( x = runif(30, 1, 80), y = runif(30, 1, 60), value = rnorm(30) ) ggplot(mapping = aes(x, y)) + # Layer 1: contour lines coloured by elevation using the "D" (viridis) palette geom_contour(data = topography, aes(z = z, colour = after_stat(level))) + scale_colour_viridis_c(option = "D", name = "Elevation") + # --- Insert scale boundary: layers below get a NEW colour scale --- new_scale("colour") + # Layer 2: measurement points coloured by value using the "A" (magma) palette geom_point(data = measurements, aes(colour = value), size = 3) + scale_colour_viridis_c(option = "A", name = "Measurement") + labs(title = "Two independent colour scales on one plot") # Result: two separate colour legends, one for elevation, one for measurements ``` -------------------------------- ### Multiple new_scale_*() calls for independent scales Source: https://context7.com/eliocamp/ggnewscale/llms.txt Call new_scale_*() repeatedly to create three or more independent scales for the same aesthetic. This example shows four independent fill scales on one plot, with each column of tiles having a distinct palette and four legends displayed. ```r library(ggplot2) library(ggnewscale) data <- expand.grid(y = 1:4, x = 1:4) data$z <- rep(c("a", "b"), length.out = nrow(data)) # Helper that creates one tile layer with its own fill scale make_layer <- function(col_number, palette_number) { list( new_scale_fill(), geom_tile( data = ~ .x[.x$x == col_number, ], aes(fill = z) ), scale_fill_brewer( name = paste("Group", col_number), palette = palette_number, guide = guide_legend(order = col_number) ) ) } ggplot(data, aes(x, y)) + make_layer(1, 2) + make_layer(2, 4) + make_layer(3, 6) + make_layer(4, 8) + labs(title = "Four independent fill scales on one plot") ``` -------------------------------- ### Convenience Wrapper new_scale_fill() for Fill Scales Source: https://context7.com/eliocamp/ggnewscale/llms.txt Use new_scale_fill() as a shorthand for new_scale('fill'). This is useful when combining geoms that require distinct fill palettes, such as geom_violin() and geom_point(). This example shows a discrete fill scale for gender and a continuous fill scale for score. ```r library(ggplot2) library(ggnewscale) set.seed(5) df <- data.frame( x = floor(runif(100, 1, 5)), y = floor(runif(100, 1, 10)), gender = sample(c("female", "male"), 100, replace = TRUE), score = runif(100) ) ggplot(df, aes(x, y, group = interaction(x, gender))) + # Fill scale 1: violin fill by gender (discrete) geom_violin(aes(fill = gender)) + scale_fill_discrete(name = "Gender", guide = guide_legend(order = 1)) + new_scale_fill() + # <-- open second fill scale # Fill scale 2: point fill by continuous score geom_point(aes(fill = score), shape = 21, size = 3, inherit.aes = FALSE, data = df[sample(nrow(df), 30), ]) + scale_fill_viridis_c(name = "Score", guide = guide_colorbar(order = 2)) + labs(title = "Violin fill (discrete) + point fill (continuous)") # Two separate fill legends are rendered correctly ``` -------------------------------- ### Using rename_aes() to alias an aesthetic for a specific layer Source: https://context7.com/eliocamp/ggnewscale/llms.txt Use rename_aes() to assign a custom alias to an aesthetic for the next layer. This is a lower-level alternative to new_scale(). The example renames 'colour' to 'topo_color' for the contour layer, then restores normal aesthetic names with clear_aes() before adding a point layer with its own 'colour' aesthetic. ```r library(ggplot2) library(ggnewscale) topography <- expand.grid(x = 1:nrow(volcano), y = 1:ncol(volcano)) topography$z <- c(volcano) set.seed(1) measurements <- data.frame( x = runif(30, 1, 80), y = runif(30, 1, 60), value = rnorm(30) ) ggplot(mapping = aes(x, y)) + # Rename "colour" -> "topo_color" for the next geom only rename_aes(topo_color = "colour") + geom_contour( data = topography, aes(z = z, topo_color = after_stat(level)) ) + scale_colour_viridis_c(option = "D", name = "Elevation") + # Restore normal aesthetic names clear_aes() + geom_point(data = measurements, aes(colour = value), size = 3) + scale_colour_viridis_c(option = "A", name = "Measurement") ``` -------------------------------- ### Using clear_aes() to remove rename_aes() mappings Source: https://context7.com/eliocamp/ggnewscale/llms.txt clear_aes() removes the active rename_aes() binding, restoring normal aesthetic name resolution for subsequent layers. This example shows the lifecycle of rename_aes() and clear_aes(), demonstrating how an alias is active for one geom and then removed for the next. ```r library(ggplot2) library(ggnewscale) # rename_aes + clear_aes lifecycle: p <- ggplot(mtcars, aes(mpg, disp)) + rename_aes(my_colour = "colour") + # alias active geom_point(aes(my_colour = factor(cyl))) + # uses alias scale_colour_brewer(type = "qual") + clear_aes() + # alias removed geom_point(aes(colour = factor(gear)), # back to standard name size = 1, shape = 1) print(p) ``` -------------------------------- ### Standard GPL Copyright Notice Source: https://github.com/eliocamp/ggnewscale/blob/master/LICENSE.md Include this notice at the beginning of each source file to state the exclusion of warranty and specify the terms of redistribution and modification under the GNU GPL. ```text Copyright (C) 2018 Elio Campitelli This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . ``` -------------------------------- ### Write Package Citation to Bib File Source: https://github.com/eliocamp/ggnewscale/blob/master/README.md Automate the process of writing package citation information to a .bib file using knitr, which can then be used for citations in R Markdown documents. ```r knitr::write_bib(c("ggnewscale"), "packages.bib") ``` -------------------------------- ### Add New Scale for Linetype Aesthetic Source: https://github.com/eliocamp/ggnewscale/blob/master/README.md Shows how to create a new scale for the 'linetype' aesthetic using `new_scale()`. This allows for multiple linetype scales within a single ggplot object. ```r new_scale("linetype") ``` -------------------------------- ### Add Multiple Color Scales to ggplot2 Plot Source: https://github.com/eliocamp/ggnewscale/blob/master/README.md Demonstrates how to use `new_scale_color()` to apply a second color scale to geoms added after its invocation. Requires `ggplot2` and `ggnewscale` libraries. The first scale is applied to `geom_contour` and the second to `geom_point`. ```r 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") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.