Skip to content

Commit 4a96f3f

Browse files
Show all nmfs palettes (#67)
* Update display_nmfs_palette so that it's based in ggplot2 instead of graphics::image() and is easier to use with a function that will display all palettes at once * Add function that shows all palettes * Add @return to documentation for all_nmfs_palettes() and display_nmfs_palette(); update title of latter function; update documentation * Update tests
1 parent c17f262 commit 4a96f3f

File tree

9 files changed

+98
-40
lines changed

9 files changed

+98
-40
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ BugReports: https://github.com/nmfs-ost/nmfspalette/issues
1717
Imports:
1818
cli,
1919
ggplot2,
20+
pals,
2021
rlang
2122
Suggests:
2223
dichromat,

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Generated by roxygen2: do not edit by hand
22

3+
export(all_nmfs_palettes)
34
export(display_nmfs_palette)
45
export(nmfs_cols)
56
export(nmfs_palette)

R/nmfs_cols.R

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -350,27 +350,62 @@ nmfs_palette <- function(palette = "oceans", reverse = FALSE, ...) {
350350
grDevices::colorRampPalette(pal, ...)
351351
}
352352

353-
#' Return function to interpolate a nmfs color palette
353+
#' Return interpolated nmfs color palette
354354
#'
355355
#' @param name Character name of palette in nmfs_palettes.
356356
#' @param n Number of colors in palette.
357-
#' @param ... Additional arguments to pass to [graphics::image()].
358357
#' @examples
359358
#' display_nmfs_palette("oceans", 10)
359+
#' @return A list object showing a specific nmfs color palette in the plot window.
360360
#' @export
361-
display_nmfs_palette <- function(name, n, ...) {
361+
display_nmfs_palette <- function(name, n) {
362362
pal <- nmfs_palette(name)(n)
363-
graphics::image(
364-
1:n,
365-
1,
366-
as.matrix(1:n),
367-
col = pal,
368-
xlab = paste(name),
369-
ylab = "",
370-
xaxt = "n",
371-
yaxt = "n",
372-
bty = "n",
373-
...
363+
df <- data.frame(
364+
x = 1,
365+
y = seq_along(pal),
366+
color = pal
374367
)
375-
graphics::box()
368+
369+
ggplot2::ggplot(df,
370+
ggplot2::aes(x = y,
371+
y = x,
372+
fill = color)) +
373+
ggplot2::geom_tile() +
374+
ggplot2::scale_fill_identity() +
375+
ggplot2::coord_fixed(ratio = 1) +
376+
ggplot2::theme_void() +
377+
ggplot2::labs(title = name) +
378+
ggplot2::theme(plot.title = ggplot2::element_text(hjust = 0.5,
379+
vjust = 1,
380+
size = 12,
381+
face = "bold"),
382+
plot.margin = ggplot2::unit(c(1,1,1,1), "cm"))
383+
# plot.margin = ggplot2::unit(c(2,2,2,2), "cm"))
384+
385+
}
386+
387+
#' Return function that shows all nmfs color palettes
388+
#'
389+
#' @examples
390+
#' all_nmfs_palettes()
391+
#'
392+
#' @return A null object showing all nmfs color palettes in the plot window.
393+
#' @export
394+
all_nmfs_palettes <- function() {
395+
396+
# default setting
397+
old_par <- par(mar = c(0,6,0,0))
398+
399+
# reset par to original setting
400+
on.exit(par(old_par))
401+
402+
do.call(
403+
pals::pal.bands,
404+
c(nmfs_palettes,
405+
list(labels = names(nmfs_palettes),
406+
gap = 0.1,
407+
show.names = FALSE)
408+
)
409+
)
410+
376411
}

README.Rmd

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,15 @@ nmfs_palette("oceans")(10)
6363

6464
## Palettes
6565

66-
To see the palettes, use the `display_nmfs_palette()` function.
66+
Use the `all_nmfs_palettes()` function to see all available palettes.
67+
68+
```{r}
69+
#| label: all_pals
70+
#| fig-height: 4
71+
all_nmfs_palettes()
72+
```
73+
74+
Use the `display_nmfs_palette()` function to see a specific palette.
6775

6876
### Main NOAA Fisheries palettes
6977

man/all_nmfs_palettes.Rd

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/display_nmfs_palette.Rd

Lines changed: 6 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/figures/README-all_pals-1.png

7.85 KB
Loading

tests/testthat/_snaps/snapshot-palettes.md

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,14 @@
1414
[1] "#901200" "#9D1200" "#AA1200" "#B71300" "#C31802" "#CF1D04" "#DB2207"
1515
[8] "#E63A21" "#F3533C" "#FF6C57"
1616

17-
# nmfs_palette() fails
18-
19-
Code
20-
nmfs_palette("foo")
21-
Condition
22-
Error in `interpolate()`:
23-
! need at least two non-NA values to interpolate
24-
2517
# display_nmfs_palette() works
2618

2719
Code
28-
urchin_palette
20+
urchin_palette$data
2921
Output
30-
NULL
22+
x y color
23+
1 1 1 #A8B8FF
24+
2 1 2 #737BE6
25+
3 1 3 #5761C0
26+
4 1 4 #3B469A
3127

tests/testthat/test-snapshot-palettes.R

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,20 @@ test_that("nmfs_palette() works", {
1414
)
1515
})
1616

17+
# Check that fake palette names create an error
1718
test_that("nmfs_palette() fails", {
18-
# Check that fake palette names create an error
19-
expect_snapshot(
20-
error = TRUE,
21-
nmfs_palette("foo")
22-
)
19+
expect_error(nmfs_palette("foo"))
20+
21+
expect_error(nmfs_palette("foo"),
22+
"need at least two non-NA values to interpolate")
2323
})
2424

2525
test_that("display_nmfs_palette() works", {
2626
# Check that display_nmfs_palette returns a function
2727
urchin_palette <- display_nmfs_palette("urchin", 4)
2828

29-
expect_type(urchin_palette, "NULL")
29+
expect_type(urchin_palette, "list")
3030

3131
# Check that display_nmfs_palette() returns a snapshot
32-
expect_snapshot(
33-
urchin_palette
34-
)
32+
expect_snapshot(urchin_palette$data)
3533
})

0 commit comments

Comments
 (0)