rcolorsggplot2luminance

Setting luminance for manually-defined color pallets in ggplot2


I have a custom color pallet I'd like to use for a geom_bar plot. However I'd also like to set the luminance of these colors manually. When I run the scale_fill_manual() and scale_fill_hue() functions together in my ggplot code, I get the following error:

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

Which seems like one "scale_fill" function is overriding the other. It seemed logical to set the luminance within the scale_fill_manual() function like:

scale_fill_manual(values=cbPalette, l = 55)

But this doesn't work.

Thanks in advance for your help! Here is a version of the figure without the luminance set correctly (below are the data and code):This figure is the version without the luminance set to 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

Solution

  • You can't combine scales like that in ggplot2: instead, you'll need to do the colour manipulation manually:

    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))
    

    enter image description here