在 ggplot2 中为手动定义的调色板设置亮度

Setting luminance for manually-defined color pallets in ggplot2

我有一个自定义颜色托盘,我想将其用于 geom_bar 绘图。但是我也想手动设置这些颜色的亮度。当我 运行 scale_fill_manual() 和 scale_fill_hue() 在我的 ggplot 代码中一起运行时,出现以下错误:

Scale for 'fill' is already present. Adding another scale for 'fill', which will replace the existing scale.

这似乎是一个 "scale_fill" 函数覆盖了另一个。在 scale_fill_manual() 函数中设置亮度似乎是合乎逻辑的:

scale_fill_manual(values=cbPalette, l = 55)

但这行不通。

在此先感谢您的帮助!这是图 没有 正确设置亮度的版本(下面是数据和代码):

> Sex_age_survival
Sex       age        estimate        lcl       ucl
Female    Adult     0.6826927 0.62881160 0.7320849
Male      Adult     0.6941044 0.64680030 0.7376429
Female    Juvenile  0.4062997 0.27163540 0.5566986
Male      Juvenile  0.5270193 0.39998360 0.6506502
Female    Chick     0.4296511 0.30855270 0.5394149
Male      Chick     0.3987215 0.25969601 0.5252046
Salina    Nest      0.5643484 0.52096067 0.6054399
Marsh     Nest      0.1434720 0.04435806 0.3013856

limits <- aes(ymin = lcl, ymax = ucl)
dodge <- position_dodge(width=0.7)
cbPalette <- c("#E69F00", "#009E73", "#CC0000", "#0066CC")

Sex_age_plot2 <- ggplot(Sex_age_survival, aes(fill = Sex, x = age, y = estimate)) + 
  theme_bw() +
  geom_bar(width = 0.7, position = "dodge", stat = "identity") +
  geom_errorbar(limits, position = dodge, width=0.2, size = .6, shape = 1) +
  theme(text=element_text(size = 16, family = "Candara"),
    legend.text = element_text(size = 30),
    legend.title = element_text(size = 30),
    axis.title.x = element_text(size = 35, vjust = -0.1),
    axis.text.x  = element_text(size = 30), 
    axis.title.y = element_text(size = 35, vjust = 1.2),
    axis.text.y  = element_text(size = 30), 
    panel.grid.major = element_blank()) +
  xlab("Life stage") + 
  ylab("Apparent survival (± 95% CI)") +
  scale_y_continuous(limits = c(0, 1)) +
  scale_fill_hue(l = 55) + # seems like I cannot run this in addition to scale_fill_manual()
  scale_fill_manual(values=cbPalette) # seems like I cannot run this in addition to scale_fill_hue()
Sex_age_plot2

您不能像在 ggplot2 中那样组合比例:相反,您需要手动进行颜色操作:

library(colorspace)

pal <- c("#E69F00", "#009E73", "#CC0000", "#0066CC")

hcl <- coords(as(hex2RGB(pal), "polarLUV"))
hcl[, "L"] <- 55

pal2 <- hex(polarLUV(hcl), fixup = TRUE)

plot(rep(1:4, 2), rep(1:2, each = 4), pch = 20, cex = 5, col = c(pal, pal2))