Skip to content

Commit

Permalink
Merge pull request #7 from idem-lab/reversie
Browse files Browse the repository at this point in the history
Reversie
  • Loading branch information
geryan authored Sep 18, 2024
2 parents 53ba164 + 5c1db6f commit 910a8a8
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 30 deletions.
72 changes: 57 additions & 15 deletions R/idpalette.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,28 @@
#' Create colour palettes based on IDEM, IDDU, and ACEFA colours
#'
#' @param p `character`. Which palette? See `?idpal` for details.
#' @param n `numeric`. How many colours
#' @param n `numeric`. How many colours?
#' @param rev `logical` Reverse the colour order?
#'
#' @return `idpalette` class object of hex colours length `n`.
#' The `idpalette` class has a print method that will plot the colours in the
#' object, but it is underneath that a `character` string of length `n` and can
#' be otherwise treated as such.
#'
#' @return `character` of hex colours length `n`
#' @export
#'
#' @examples
#'
#' idpalette("idem", 20)
#' idpalette(
#' "idem",
#' n = 20,
#' rev = TRUE
#' )
#'
idpalette <- function(
p,
n = NULL
n = NULL,
rev = FALSE
){

pal <- idpal(p)
Expand All @@ -34,6 +45,10 @@ idpalette <- function(

idp <- grDevices::colorRampPalette(pal)(n)

if(rev){
idp <- rev(idp)
}

structure(idp, class = "idpalette", name = p)

}
Expand All @@ -43,55 +58,82 @@ idpalette <- function(

#' @title IDEM palette
#' @description
#' An alias for `idpalette(p = "idem", n)`
#' An alias for `idpalette(p = "idem", n, rev)`
#'
#' @param n `numeric`. How many colours?
#'
#' @return `character` of length `n`
#' @param rev `logical` Reverse the colour order?
#'
#' @return `idpalette` class object of hex colours length `n`.
#' The `idpalette` class has a print method that will plot the colours in the
#' object, but it is underneath that a `character` string of length `n` and can
#' be otherwise treated as such.
#'
#' @export
#'
#' @examples
#' idem(7)
idem <- function(n = NULL){
idem <- function(
n = NULL,
rev = FALSE
){
idpalette(
p = "idem",
n = n
n = n,
rev = rev
)
}

#' @title IDDU palette
#' @description
#' An alias for `idpalette(p = "iddu", n)`
#' An alias for `idpalette(p = "iddu", n, rev)`
#'
#' @param n `numeric`. How many colours?
#' @param rev `logical` Reverse the colour order?
#'
#' @return `character` of length `n`
#' @return `idpalette` class object of hex colours length `n`.
#' The `idpalette` class has a print method that will plot the colours in the
#' object, but it is underneath that a `character` string of length `n` and can
#' be otherwise treated as such.
#' @export
#'
#' @examples
#' iddu(5)
iddu <- function(n = NULL){
iddu <- function(
n = NULL,
rev = FALSE
){
idpalette(
p = "iddu",
n = n
n = n,
rev = rev
)
}

#' @title ACEFA palette
#' @description
#' An alias for `idpalette(p = "acefa", n)`
#' An alias for `idpalette(p = "acefa", n, rev)`
#'
#' @param n `numeric`. How many colours?
#' @param rev `logical` Reverse the colour order?
#'
#' @return `character` of length `n`
#' @return `idpalette` class object of hex colours length `n`.
#' The `idpalette` class has a print method that will plot the colours in the
#' object, but it is underneath that a `character` string of length `n` and can
#' be otherwise treated as such.
#' @export
#'
#' @examples
#' acefa(5)
acefa <- function(n = NULL){
acefa <- function(
n = NULL,
rev = FALSE
){
idpalette(
p = "acefa",
n = n
n = n,
rev = rev
)
}

Expand Down
18 changes: 15 additions & 3 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,25 @@ ggplot(mpg) +
scale_fill_manual(values = idpalette("idem")) +
theme_bw()
```
```{r}

Works with `terra` plotting:

```{r, fig.height=3}
library(sdmtools)
library(tidyterra)
library(terra)
r <- example_raster(seed = 20240802)
plot(r, col = idem(100))
par(mfcol = c(1, 2))
plot(
r,
col = idem(100)
)
plot(
r,
col = idem(100, rev = TRUE)
)
```

27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,30 @@ ggplot(mpg) +
```

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

Works with `terra` plotting:

``` r
library(sdmtools)
library(terra)
#> terra 1.7.78
```

``` r

r <- example_raster(seed = 20240802)

par(mfcol = c(1, 2))

plot(
r,
col = idem(100)
)

plot(
r,
col = idem(100, rev = TRUE)
)
```

<img src="man/figures/README-unnamed-chunk-15-1.png" width="100%" />
11 changes: 8 additions & 3 deletions man/acefa.Rd

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

Binary file added man/figures/README-unnamed-chunk-15-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 8 additions & 3 deletions man/iddu.Rd

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

11 changes: 9 additions & 2 deletions man/idem.Rd

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

18 changes: 14 additions & 4 deletions man/idpalette.Rd

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

0 comments on commit 910a8a8

Please sign in to comment.