Skip to content

Commit

Permalink
create scale_id_continuous
Browse files Browse the repository at this point in the history
  • Loading branch information
geryan committed Oct 7, 2024
1 parent 145455d commit a30fdc9
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 2 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
50 changes: 50 additions & 0 deletions R/scale_id_continuous.R
Original file line number Diff line number Diff line change
@@ -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",
)

}
23 changes: 22 additions & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ ggplot(mpg) +

Works with `terra` plotting:

```{r, fig.height=3}
```{r, fig.height=4}
library(sdmtools)
library(terra)
Expand All @@ -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"
)
```

24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,27 @@ plot(
```

<img src="man/figures/README-unnamed-chunk-16-1.png" width="100%" />

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"
)
```

<img src="man/figures/README-unnamed-chunk-17-1.png" width="100%" />
Binary file modified man/figures/README-unnamed-chunk-16-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added man/figures/README-unnamed-chunk-17-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions man/scale_id_continuous.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a30fdc9

Please sign in to comment.