【发布时间】:2021-05-08 06:07:01
【问题描述】:
我创建了一个自定义 ggplot2 填充/颜色比例作为 discrete_scale 的包装,ggplot2 比例函数中唯一的参数是 discrete_scale 是 shade。我实际上有 4 种基于此参数选择的不同调色板:浅色、中间调、深色、轮廓。
我面临的问题是,当我在比例包装函数(填充和颜色)中调用这些不同的阴影时,这些颜色的顺序会有所不同。如何确保订单一致?
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()
【问题讨论】:
标签: r ggplot2 color-palette