nmfspalette icon indicating copy to clipboard operation
nmfspalette copied to clipboard

Discrete scales don't always use exact palette colors

Open DanielDaye-NOAA opened this issue 1 year ago • 4 comments

I think that this is a result of colorRampPalette function. If you are creating a discrete palette where the number of colors is not 2, or not equal to the number of colors in the palette, the selected colors do not match up to those in the chosen palette (looks to be interpolating between the two closest colors - processblue and reflexblue). It might require a different approach if the intent is for discrete palettes to use the exact colors supplied by nmfs_palettes

> oceans <- nmfs_palettes[["oceans"]]
> crPal <- colorRampPalette(oceans)

> oceans
[1]   o66white    o33white processblue  reflexblue    national   o50pblack 
[2] "#A6D4EC"  "#54ADDB"  "#0085CA"  "#003087"  "#002364"  "#001743" 

> crPal(6)
[1] "#A6D4EC"  "#54ADDB"  "#0085CA"  "#003087"  "#002364"  "#001743"
> crPal(3)
[1] "#A6D4EC"  "#005AA8"  "#001743"

image

DanielDaye-NOAA avatar Nov 07 '24 22:11 DanielDaye-NOAA

@DanielDaye-NOAA sorry for the late reply on this!

Looking at function documentation for nmfs_palette(), it seems like interpolation is the intention; However, I think the point you made about wanting a discrete palette is a good one! Do you think adding a new function that only allows use of the tones in the official branding guidance would be helpful? Or do you envision this working some other way?

k-doering-NOAA avatar Jan 10 '25 18:01 k-doering-NOAA

I think that another function, or the adding an argument for choosing between discrete/interpolated values would be helpful if the intention is to stick to official NOAA colors, though the discrete palettes would limit the number of groups to the number of colors in the palette.

Here's a solution that I had been using in one of our reports:

scale_color_SAFE <- function(rev = FALSE, discrete = TRUE) {

  # create pallete
  newpal <- c("#FF4438","#FF8300","#93D500","#1ECAD3","#0093D0","#7F7FFF")

  # reverse if TRUE
  if (rev) {newpal = rev(newpal)}

  # create colorRampPalette
  crp <- colorRampPalette(newpal)

  #  use scale_color_manual for discrete, scale_color_gradientn for interpolated 
  if (discrete) {
    scale_color_manual(values = newpal)
  } else {
    scale_color_gradientn(colors = crp(256))
  }
}

DanielDaye-NOAA avatar Jan 17 '25 14:01 DanielDaye-NOAA

@DanielDaye-NOAA thank you for sharing your solution on this - would you be interested in incorporating this into the package and submitting it as a pull request?

I'm also happy to work together on this if you are interested but not quite sure how to get this into the package! Feel free to reach out here or by email ([email protected])

k-doering-NOAA avatar Jan 24 '25 22:01 k-doering-NOAA

Incorporate this option as a separate argument in the existing function

k-doering-NOAA avatar Feb 27 '25 19:02 k-doering-NOAA