在 R 中使用 ggplot2 控制两个变量的双 geom_ribbon 颜色

Control colours on double geom_ribbon for two variables with ggplot2 in R

我想为每个变量显示两个色带,例如(最大 - 最小色带和置信度色带)在 R 中的 ggplot2 中具有 geom_ribbon(),如下例所示。我无法分别为每个色带设置颜色。理想情况下,我可以将调色板映射到分类变量的每个级别 (type),两条色带将从中获取颜色。

## Set up data
set.seed(999)
n <- 100

mn1 <- seq(0.5, 0.9, length.out = n)
mn2 <- seq(0.75, 0.25, length.out = n)

tmp1 <- lapply(seq_len(n), function(x) {
  
  x1 <- rnorm(n, mn1[x], 0.1)
  x2 <- rnorm(n, mn2[x], 0.1)
  
  rbind(cbind(min(x1), mean(x1)-sd(x1), mean(x1), mean(x1)+sd(x1), max(x1)),
        cbind(min(x2), mean(x2)-sd(x2), mean(x2), mean(x2)+sd(x2), max(x2))
  )
})

year <- seq(1900, 1900+n-1, 1)
type <- rep(c("all", "partial"), n)

df1 <- data.frame(rep(year,each = 2), do.call(rbind, tmp1), type)
colnames(df1) <- c("year", "xmin", "xsd_lwr", "xmn", "xsd_upr", "xmax", "type")
head(df1)                  
rm(tmp1, mn1, mn2, year, type, n)

这是我目前在 ggplot2 中的内容:

library(ggplot2)

ggplot(df1, aes(x = year, y = xmn, fill = type, col= type))+
  geom_ribbon(aes(ymin=xsd_lwr, ymax = xsd_upr), linetype = 0, alpha = 0.4)+
  geom_ribbon(aes(ymin=xmin, ymax = xmax), linetype = 0, alpha = 0.4)+
  scale_color_manual(values = c("black", "darkred"))+
  scale_fill_manual(values = c("grey10", "grey30"))+
  geom_line(aes(linetype= type), size = 1)+
  scale_x_continuous(breaks = seq(1900, 2000,10))

更新: 我已经接受了答案,因为它确实可以精确控制四种不同的颜色,但是,我想表明@teunbrand 的评论也可以用作透明度,实际上也可以创建四种颜色,并且具有更好的图例。我已将建议修改为以下内容:

ggplot(df1, aes(x = year, y = xmn,col= type))+
  geom_ribbon(aes(ymin=xsd_lwr, ymax = xsd_upr, fill = type), linetype = 0, alpha = 0.5)+
  geom_ribbon(aes(ymin=xmin, ymax = xmax, fill = type), linetype = 0, alpha =0.5)+
  scale_fill_manual(values = c("tomato", "dodgerblue"))+
  scale_color_manual(values = c("black", "darkred"))+
  geom_line(aes(linetype= type), size = 1)+
  scale_x_continuous(breaks = seq(1900, 2000,10))

您可以在 ribbonaes 中定义一个特定的 fill 组,并在 scale_fill_manual 中将您想要的颜色与其相关联:

ggplot(df1, aes(x = year, y = xmn, fill = type, col= type))+
  geom_ribbon(aes(ymin=xsd_lwr, ymax = xsd_upr), linetype = 0, alpha = 0.4,show.legend=F)+
  scale_fill_manual(values = c("grey10","red","grey30","green"))+
  geom_ribbon(aes(ymin=xmin, ymax = xmax,fill=ifelse(type=='all','all_minmax','partial_min_max')), linetype = 0, alpha = 0.4,show.legend=F)+
  scale_color_manual(values = c("black", "darkred"))+
  geom_line(aes(linetype= type), size = 1)+
  scale_x_continuous(breaks = seq(1900, 2000,10))

请注意,我必须删除 ribbons 的图例以避免显示带有新颜色组的第二个图例。

要尽可能完全控制任意数量的colors/fills,请使用ggnewscale。这也为您提供了完整的图例控制。

我不在控制台上,在 rdrr 上编码。io/snippets,因此很难显示数字输出。但可重现它是

library(ggplot2)
library(ggnewscale)

ggplot(df1, aes(x = year, y = xmn, col = type))+
  geom_ribbon(aes(ymin=xsd_lwr, ymax = xsd_upr, fill = "SE"), linetype = 0, alpha = 0.4)+
  scale_fill_manual(name = NULL, values = c("tomato","darkred"))+
  new_scale_fill()+
  geom_ribbon(aes(ymin=xmin, ymax = xmax, fill= "range"), linetype = 0, alpha = 0.4)+
  scale_fill_manual(name = NULL, values = c("dodgerblue", "darkred"))+
  geom_line(aes(linetype= type), size = 1)+
  scale_color_manual(values = c("black", "darkred"))+
  scale_x_continuous(breaks = seq(1900, 2000,10))