Comments (8)
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()
from ggsci.
where is discrete_scale
function? could you provide?
from ggsci.
@saisaitian https://ggplot2.tidyverse.org/reference/discrete_scale.html
from ggsci.
Nice function! Will it ever be integrated into the ggsci package? I mean, since you already have the function here
from ggsci.
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.
Nice function! But how to adjust the order of colors after using the above function?
from ggsci.
@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.
Fixed by 78a24ce
from ggsci.
Related Issues (20)
- Add Bootstrap colors
- Add Tailwind CSS colors
- Add arrow color palette HOT 1
- Add ayu color palette HOT 1
- Manually re-order palette HOT 1
- Release ggsci 3.0.1 HOT 1
- Using `scale_colour_xxx` throws a warning with recent ggplot2 HOT 4
- Release ggsci 3.0.2 HOT 1
- Reverse dependency check notes
- Backward compatibility with ggplot2 < 3.5.0
- Release ggsci 3.0.3 HOT 1
- BMJ color palettes
- Convert Rd syntax to Markdown in roxygen2 documentation
- Release ggsci 3.1.0 HOT 2
- Add Observable 10 color palette
- Replace `sysdata.rda` with code
- Compact continuous palette examples
- Release ggsci 3.2.0 HOT 1
- Add Primer color palette
- Add Atlassian color palette
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ggsci.