diff --git a/DESCRIPTION b/DESCRIPTION index f0121cf..2b0f49d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -10,7 +10,9 @@ Encoding: UTF-8 Roxygen: list(markdown = TRUE) RoxygenNote: 7.3.1 Imports: - grDevices + ggplot2, + grDevices, + scales Suggests: testthat (>= 3.0.0) Config/testthat/edition: 3 diff --git a/NAMESPACE b/NAMESPACE index bfc533e..b4c84aa 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,6 +6,7 @@ export(iddu) export(idem) export(idpal) export(idpalette) +export(scale_id_continuous) importFrom(grDevices,rgb) importFrom(graphics,image) importFrom(graphics,par) diff --git a/R/scale_id_continuous.R b/R/scale_id_continuous.R new file mode 100644 index 0000000..80a550f --- /dev/null +++ b/R/scale_id_continuous.R @@ -0,0 +1,50 @@ +#' @title scale ID continuous +#' @description Convenience wrapper to `ggplot2::continuous_scale` to allow use of +#' `idpalette` or any arbitrary set of colours to be used as a `ggplot2` fill +#' or colour scale +#' +#' @param cols `character`. A vector of colours +#' @param aesthetics Scale aesthetic. +#' @param na.value Garn - whaddaya want in the `NA`s? +#' +#' @return A `ggproto` object of class `Scale` and `ScaleContinuous` +#' @export +#' +#' @examples +#' +#' library(ggplot2) +#' +#' ggplot(mtcars) + +#' geom_point( +#' aes( +#' x = disp, +#' y = hp, +#' colour = qsec +#' ), +#' size = 5 +#' ) + +#' scale_id_continuous( +#' cols = iddu(), +#' aesthetics = "colour" +#' ) +#' +scale_id_continuous <- function( + cols, + aesthetics = c("fill", "colour"), + na.value = "transparent" +){ + + aesthetics <- match.arg(aesthetics) + + ggplot2::continuous_scale( + aesthetics = aesthetics, + palette = scales::pal_gradient_n( + colours = cols, + values = NULL, + space = "Lab" + ), + na.value = na.value, + guide = "colourbar", + ) + +} diff --git a/README.Rmd b/README.Rmd index 982db76..5ec0b09 100644 --- a/README.Rmd +++ b/README.Rmd @@ -153,7 +153,7 @@ ggplot(mpg) + Works with `terra` plotting: -```{r, fig.height=3} +```{r, fig.height=4} library(sdmtools) library(terra) @@ -172,3 +172,24 @@ plot( ) ``` +Sometimes `ggplot2` won't play nicely being fed a vector of values for +continuous scales, so `scale_id_continuous` allows `idpalette` or any arbitrary +vector of colours to be used: +```{r, fig.height= 4} +library(ggplot2) + +ggplot(mtcars) + + geom_point( + aes( + x = disp, + y = hp, + colour = qsec + ), + size = 5 + ) + + scale_id_continuous( + cols = iddu(), + aesthetics = "colour" + ) +``` + diff --git a/README.md b/README.md index 23ee380..a00bb1b 100644 --- a/README.md +++ b/README.md @@ -194,3 +194,27 @@ plot( ``` + +Sometimes `ggplot2` won’t play nicely being fed a vector of values for +continuous scales, so `scale_id_continuous` allows `idpalette` or any +arbitrary vector of colours to be used: + +``` r +library(ggplot2) + +ggplot(mtcars) + + geom_point( + aes( + x = disp, + y = hp, + colour = qsec + ), + size = 5 + ) + + scale_id_continuous( + cols = iddu(), + aesthetics = "colour" + ) +``` + + diff --git a/man/figures/README-unnamed-chunk-16-1.png b/man/figures/README-unnamed-chunk-16-1.png index e032296..e735c89 100644 Binary files a/man/figures/README-unnamed-chunk-16-1.png and b/man/figures/README-unnamed-chunk-16-1.png differ diff --git a/man/figures/README-unnamed-chunk-17-1.png b/man/figures/README-unnamed-chunk-17-1.png new file mode 100644 index 0000000..19020c0 Binary files /dev/null and b/man/figures/README-unnamed-chunk-17-1.png differ diff --git a/man/scale_id_continuous.Rd b/man/scale_id_continuous.Rd new file mode 100644 index 0000000..27e4c08 --- /dev/null +++ b/man/scale_id_continuous.Rd @@ -0,0 +1,46 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/scale_id_continuous.R +\name{scale_id_continuous} +\alias{scale_id_continuous} +\title{scale ID continuous} +\usage{ +scale_id_continuous( + cols, + aesthetics = c("fill", "colour"), + na.value = "transparent" +) +} +\arguments{ +\item{cols}{\code{character}. A vector of colours} + +\item{aesthetics}{Scale aesthetic.} + +\item{na.value}{Garn - whaddaya want in the \code{NA}s?} +} +\value{ +A \code{ggproto} object of class \code{Scale} and \code{ScaleContinuous} +} +\description{ +Convenience wrapper to \code{ggplot2::continuous_scale} to allow use of +\code{idpalette} or any arbitrary set of colours to be used as a \code{ggplot2} fill +or colour scale +} +\examples{ + +library(ggplot2) + +ggplot(mtcars) + + geom_point( + aes( + x = disp, + y = hp, + colour = qsec + ), + size = 5 + ) + + scale_id_continuous( + cols = iddu(), + aesthetics = "colour" + ) + +}