在ggplot2

时间:2015-08-12 15:43:54

标签: r colors ggplot2 luminance

我有一个自定义颜色托盘我想用于geom_bar图。不过我还想手动设置这些颜色的亮度。当我在我的ggplot代码中一起运行scale_fill_manual()和scale_fill_hue()函数时,我收到以下错误:

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)

但这不起作用。

提前感谢您的帮助!以下是没有正确设置亮度的图形版本(以下是数据和代码):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

1 个答案:

答案 0 :(得分:1)

你不能在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))

enter image description here