简体   繁体   English

在 ggplot 图例中控制颜色、填充和线型

[英]Control color, fill and line type in ggplot legend

I have data of two model results with variation presented as shaded quantiles and for each model a reference benchmark without variation.我有两个 model 结果的数据,其变化显示为阴影分位数,每个 model 的参考基准没有变化。 See the example data here:请参阅此处的示例数据:

# create data
y <- 1+0.1*c(0:29)
r1 <- runif(30,0.8,1.2)
mydata0 <- data.frame(model = "model1",scenario="a_sc1", year = c(2022:2051), q9 = y+2*r1,
                     q7=y+r1, q5=y+r1-1,q3=y-r1,q1=y-2*r1,bench= r1)

mydata1 <- mydata0
mydata1[,4:9] <- r1+mydata1[,4:9]
mydata1$model <- "model2"

mydata <- rbind(mydata0, mydata1)

I plot is in the following way, which is what I need, but the legend is missing the benchmark with line type.我的plot是下面这种方式,正是我需要的,但是图例是缺少了带线型的benchmark。

library(ggplot2)
col2 <- c("darkred","steelblue")
col2f <- c("magenta","green")

ggplot(data=mydata, aes(x=year, group=model)) +
  geom_ribbon(aes(ymin=q1 , ymax=q9,fill=model),  linetype=0, alpha=0.2) +
  geom_ribbon(aes(ymin=q3 , ymax=q7,fill=model),  linetype=0, alpha=0.2) +
  geom_line(aes(x=year, y=q5 ,color=model), linetype=1, size=1.3)+
  geom_line(aes(x=year, y=bench ,color=model), linetype=2, size=1.3)+
  scale_color_manual(values = col2) +
  scale_fill_manual(values = col2f)+
  scale_linetype_manual(values = c(1,2)) +
  labs( y = "var [mg/l]", x="") +
  theme_light() +
  theme(text = element_text(size = 18),axis.text=element_text(colour="black",size=20),legend.text=element_text(size=22))

< < 模型和基准 1 > >

I arrange the data in a long format and plot as follows:我把长格式的数据和plot排列如下:

# second trail: long data
mydata2 <- rbind(mydata[,-9], data.frame(model=mydata$model,scenario="bench",year=mydata$year,q9=NA,q7=NA,q5=mydata$bench,q3=NA,q1=NA))
  
  ggplot(data=mydata2, aes(x=year)) +
    geom_ribbon(aes(ymin=q1 , ymax=q9,fill=model),  linetype=0, alpha=0.2, na.rm = TRUE) +
    geom_ribbon(aes(ymin=q3 , ymax=q7,fill=model),  linetype=0, alpha=0.2, na.rm = TRUE) +
    geom_line(aes(x=year, y=q5 ,color=model, linetype=scenario), size=1.3)+
    scale_color_manual(values = col2) +
    scale_fill_manual(values = col2f)+
    labs( y = "var [mg/l]", x="") +
    theme_light() +
    theme(text = element_text(size = 18),axis.text=element_text(colour="black",size=20),legend.text=element_text(size=22))

< < 模型和基准 2 > Now I do have the legend of the benchmark - the l.netype is correct, but the coloring is missing. > 现在我有了基准的图例 - l.netype 是正确的,但缺少着色。 I did not succeed adding it.我没有成功添加它。 Appreciate any ideas.感谢任何想法。

***** EDIT ****** ***** 编辑 ******

I add here a graphicly edited image of how I want it to appear.我在这里添加了我希望它如何显示的图形编辑图像。 Seems to me that my first approach would be better (not in long format) but how do I add the legend?在我看来,我的第一种方法会更好(不是长格式),但如何添加图例? 模型和基准 - 期望

If you want both the l.netype and the color of your lines to be displayed in the legend then one option would be to map the interaction of model and scenario on the color and l.netype aesthetics, which however requires to set a manual palettes taking the interaction into account:如果您希望 l.netype 和线条的颜色都显示在图例中,那么一个选项是 map model 的交互l.netype colormodel美学的scenario ,但是需要设置手动调色板考虑到相互作用:

library(ggplot2)

pal_color <- rep(col2, each = 2)
pal_lty <- rep(c("solid", "dashed"), 2)

names(pal_color) <- names(pal_lty) <- c("model1.a_sc1", "model1.bench", "model2.a_sc1", "model2.bench")

ggplot(data = mydata2, aes(x = year)) +
  geom_ribbon(aes(ymin = q1, ymax = q9, fill = model), linetype = 0, alpha = 0.2, na.rm = TRUE) +
  geom_ribbon(aes(ymin = q3, ymax = q7, fill = model), linetype = 0, alpha = 0.2, na.rm = TRUE) +
  geom_line(aes(
    x = year, y = q5,
    color = paste(model, scenario, sep = "."),
    linetype = paste(model, scenario, sep = ".")
  ), size = 1.3) +
  scale_color_manual(values = pal_color) +
  scale_linetype_manual(values = pal_lty) +
  scale_fill_manual(values = col2f) +
  labs(y = "var [mg/l]", x = "") +
  theme_light() +
  theme(
    text = element_text(size = 18),
    axis.text = element_text(colour = "black", size = 20),
    legend.text = element_text(size = 22)
  )

在此处输入图像描述

UPDATE Based on the edit containing an image of your desired result we have to do some adjustments, basically a mix of your original and my approach.更新基于包含您想要的结果图像的编辑,我们必须进行一些调整,基本上是您的原始方法和我的方法的混合。 First, stick with color=model so that we get a color/fill legend displaying both the ribbons and the solid lines.首先,坚持使用color=model以便我们得到一个颜色/填充图例,同时显示色带和实线。 To fix the l.netype legend set the breaks (and labels) so that only the benchmark lines are displayed in the legend.要修复 l.netype 图例,请设置中断(和标签),以便图例中仅显示基准线。 Additionally we have to do a hack via the override.aes argument of guide_legend to the colors to l.netype legend:此外,我们必须通过 guide_legend 的override.aes参数将guide_legend修改为 l.netype 图例:

Note: I also fixed the livetypes.注意:我还修复了 livetypes。 I had chosen "dashed" but after a closer look IMHO it should be 22 .我选择了"dashed" ,但仔细一看恕我直言,它应该是22

library(ggplot2)

pal_lty <- rep(c("solid", "22"), 2)

names(pal_lty) <- c("model1.a_sc1", "model1.bench", "model2.a_sc1", "model2.bench")

ggplot(data = mydata2, aes(x = year)) +
  geom_ribbon(aes(ymin = q1, ymax = q9, fill = model), linetype = 0, alpha = 0.2, na.rm = TRUE) +
  geom_ribbon(aes(ymin = q3, ymax = q7, fill = model), linetype = 0, alpha = 0.2, na.rm = TRUE) +
  geom_line(aes(
    x = year, y = q5,
    color = model,
    linetype = paste(model, scenario, sep = ".")
  ), size = 1.3) +
  scale_color_manual(values = col2) +
  scale_linetype_manual(
    values = pal_lty,
    breaks = c("model1.bench", "model2.bench"),
    labels = c("Bench_model1", "Bench_model2"),
    guide = guide_legend(override.aes = list(color = col2))
  ) +
  scale_fill_manual(values = col2f) +
  labs(y = "var [mg/l]", x = "", linetype = "Bench") +
  theme_light() +
  theme(
    text = element_text(size = 18),
    axis.text = element_text(colour = "black", size = 20),
    legend.text = element_text(size = 22)
  )

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM