[英]Check or replace the value for the argument “palette” in the function ggplot2::discrete_scale()?
[英]Manual ordering of color palette when using discrete_scale to generate custom ggplot2 scales
我创建了一个自定义 ggplot2 填充/色阶作为discrete_scale
discrete_scale
的shade
我实际上有 4 种基于此参数选择的不同调色板:浅色、中间调、深色、轮廓。
我面临的问题是,当我在比例包装函数(填充和颜色)中调用这些不同的阴影时,这些 colors 的顺序会有所不同。 如何确保一致的订单?
palette_light_custom <- c("#FF61c3", "#53c1e8", "#82FF73", "#FFFA5C", "#FFA200", "#FF6D5C", "#C478FF", "#C478FF", "#C478FF")
palette_midtone_custom <- c("#f84eb7", "#f3413a", "#f48c00", "#fffa5c", "#56dc5b", "#219ed4", "#b86ef4", "#b86ef4", "#b86ef4")
palette_dark_custom <- c("#e0008a", "#eb0008", "#f48c00", "#fffa5c", "#15a736", "#0085cf", "#8745c6", "#8745c6", "#8745c6")
palette_outline_custom <- c("#a30059", "#005ba1", "#6729a7", "#FFFA5C", "#FFA200", "#FF6D5C", "#C478FF", "#C478FF", "#C478FF")
pal_light <- function() { scales::manual_pal(palette_light_custom) }
pal_midtone <- function() { scales::manual_pal(palette_midtone_custom) }
pal_dark <- function() { scales::manual_pal(palette_dark_custom) }
pal_outline <- function() { scales::manual_pal(palette_outline_custom) }
scale_colour_custom <- function(shade, ...) {
if (shade == "light") {
discrete_scale("colour", "custom", pal_light_custom(), ...)
} else if (shade == "midtone") {
discrete_scale("colour", "custom", pal_midtone_custom(), ...)
} else if (shade == "dark") {
discrete_scale("colour", "custom", pal_dark_custom(), ...)
} else if (shade == "outline") {
discrete_scale("colour", "custom", pal_outline_custom(), ...)
} else {
stop("Incorrect or missing shade parameter in scale_color_custom()")
}
}
scale_color_custom <- scale_colour_custom
scale_fill_custom <- function(shade, ...) {
if (shade == "light") {
discrete_scale("fill", "custom", pal_light_custom(), ...)
} else if (shade == "midtone") {
discrete_scale("fill", "custom", pal_midtone_custom(), ...)
} else if (shade == "dark") {
discrete_scale("fill", "custom", pal_dark_custom(), ...)
} else if (shade == "outline") {
discrete_scale("fill", "custom", pal_outline_custom(), ...)
} else {
stop("Incorrect or missing shade parameter in scale_color_custom()")
}
}
ggplot(data=iris,
aes(x = Sepal.Length,
color = Species,
fill = Spcies)) +
geom_density() +
scale_colour_custom("outline") +
scale_fill_custom("midtone") +
theme_minimal()
感谢您评论中的额外细节; 这能解决你的问题吗?
library(tidyverse)
palette_light_custom <- c("#FF61c3", "#FF6D5C", "#FFA200", "#FFFA5C", "#82FF73", "#53c1e8", "#C478FF", "#C478FF", "#C478FF")
palette_midtone_custom <- c("#f84eb7", "#f3413a", "#f48c00", "#fffa5c", "#56dc5b", "#219ed4", "#b86ef4", "#b86ef4", "#b86ef4")
palette_dark_custom <- c("#e0008a", "#eb0008", "#d97d00", "#fffa5c", "#15a736", "#0085cf", "#8745c6", "#8745c6", "#8745c6")
palette_outline_custom <- c("#ad006b", "#b50006", "#c26f00", "#f7f000", "#006e19", "#005ba1", "#6729a7", "#6729a7", "#6729a7")
scales::show_col(palette_light_custom)
scales::show_col(palette_midtone_custom)
scales::show_col(palette_dark_custom)
scales::show_col(palette_outline_custom)
pal_light <- function() { scales::manual_pal(palette_light_custom) }
pal_midtone <- function() { scales::manual_pal(palette_midtone_custom) }
pal_dark <- function() { scales::manual_pal(palette_dark_custom) }
pal_outline <- function() { scales::manual_pal(palette_outline_custom) }
scale_colour_custom <- function(shade, ...) {
if (shade == "light") {
discrete_scale("colour", "custom", pal_light(), ...)
} else if (shade == "midtone") {
discrete_scale("colour", "custom", pal_midtone(), ...)
} else if (shade == "dark") {
discrete_scale("colour", "custom", pal_dark(), ...)
} else if (shade == "outline") {
discrete_scale("colour", "custom", pal_outline(), ...)
} else {
stop("Incorrect or missing shade parameter in scale_color_custom()")
}
}
scale_fill_custom <- function(shade, ...) {
if (shade == "light") {
discrete_scale("fill", "custom", pal_light(), ...)
} else if (shade == "midtone") {
discrete_scale("fill", "custom", pal_midtone(), ...)
} else if (shade == "dark") {
discrete_scale("fill", "custom", pal_dark(), ...)
} else if (shade == "outline") {
discrete_scale("fill", "custom", pal_outline(), ...)
} else {
stop("Incorrect or missing shade parameter in scale_color_custom()")
}
}
ggplot(data=iris,
aes(x = Sepal.Length,
color = Species,
fill = Species)) +
geom_density() +
scale_colour_custom("outline") +
scale_fill_custom("light") +
theme_minimal()
ggplot(data=iris,
aes(x = Sepal.Length,
color = Species,
fill = Species)) +
geom_density() +
scale_colour_custom("outline") +
scale_fill_custom("midtone") +
theme_minimal()
ggplot(data=iris,
aes(x = Sepal.Length,
color = Species,
fill = Species)) +
geom_density() +
scale_colour_custom("outline") +
scale_fill_custom("dark") +
theme_minimal()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.