Git Product home page Git Product logo

Comments (8)

nanxstats avatar nanxstats commented on August 17, 2024 2

Good question. Automatic color palette extrapolation is a doable feature and has been planned for ggsci 3.0.0. In case I don't have time to work on that this year, here is how it can be done by yourself:

library("ggsci")

#' Adaptive palette (discrete).
#'
#' Create a discrete palette which will use the first n colors from
#' the supplied color values, and interpolate after n.
adaptive_pal <- function(values) {
  force(values)
  function(n = 10) {
    if (n <= length(values)) {
      values[seq_len(n)]
    } else {
      colorRampPalette(values, alpha = TRUE)(n)
    }
  }
}

pal_npg_adaptive <- function(palette = c("nrc"), alpha = 1) {
  palette <- match.arg(palette)
  
  if (alpha > 1L | alpha <= 0L) stop("alpha must be in (0, 1]")
  
  raw_cols <- ggsci:::ggsci_db$"npg"[[palette]]
  raw_cols_rgb <- col2rgb(raw_cols)
  alpha_cols <- rgb(
    raw_cols_rgb[1L, ], raw_cols_rgb[2L, ], raw_cols_rgb[3L, ],
    alpha = alpha * 255L, names = names(raw_cols),
    maxColorValue = 255L
  )
  
  adaptive_pal(unname(alpha_cols))
}

scale_color_npg_adaptive <- function(palette = c("nrc"), alpha = 1, ...) {
  palette <- match.arg(palette)
  discrete_scale("colour", "npg", pal_npg_adaptive(palette, alpha), ...)
}

scale_fill_npg_adaptive <- function(palette = c("nrc"), alpha = 1, ...) {
  palette <- match.arg(palette)
  discrete_scale("fill", "npg", pal_npg_adaptive(palette, alpha), ...)
}
library("ggplot2")

dsamp <- diamonds[sample(nrow(diamonds), 1000), ]
dsamp$table <- as.factor(dsamp$table)

ggplot(dsamp, aes(carat, price)) + geom_point(aes(colour = table)) +
  theme_bw() + scale_color_npg_adaptive()

adaptive

from ggsci.

saisaitian avatar saisaitian commented on August 17, 2024

where is discrete_scale function? could you provide?

from ggsci.

nanxstats avatar nanxstats commented on August 17, 2024

@saisaitian https://ggplot2.tidyverse.org/reference/discrete_scale.html

from ggsci.

albert-ying avatar albert-ying commented on August 17, 2024

Nice function! Will it ever be integrated into the ggsci package? I mean, since you already have the function here

from ggsci.

nanxstats avatar nanxstats commented on August 17, 2024

I posted a more generic version of the above code here: https://nanx.me/blog/post/ggplot2-color-interpolation/

I will also look into the possibility to incorporate this into the package in future releases.

from ggsci.

zhouyutong123 avatar zhouyutong123 commented on August 17, 2024

Nice function! But how to adjust the order of colors after using the above function?

from ggsci.

nanxstats avatar nanxstats commented on August 17, 2024

@zhouyutong123 I guess the main purpose of this is automation. While you can always use any additional logic to adjust the colors (including ordering) in pal_adaptive(), or simply create a manual color scale and adjust there: https://r-graphics.org/recipe-colors-palette-discrete-manual

from ggsci.

nanxstats avatar nanxstats commented on August 17, 2024

Fixed by 78a24ce

from ggsci.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.