图例在ggplot中添加不确定性

董潘

我是R新用户。我试图在ggplot中添加标题为“ RCP4.5”和“ RCP8.5”的df_summary1和df_summary2均值数据的图例,但失败了。谁可以帮我这个事。提前致谢。

ggplot()+
geom_line(data=df_tidy1, aes(x=date, y=Ratio, group=Cell), color="grey") +
geom_line(data=df_tidy2, aes(x=date, y=Ratio, group=Cell), color="grey")+
geom_line(data = df_summary1, aes(x = date, y=mean), color = "red") +
geom_line(data = df_summary2, aes(x = date, y=mean), color = "blue")+
geom_ribbon(data =df_summary1, aes(x= date, ymin=CI_lower, ymax=CI_upper)    ,fill="blue", alpha=0.2)+ 
geom_ribbon(data =df_summary2,aes(x= date, ymin=CI_lower, ymax=CI_upper) ,fill="grey", alpha=0.2)+ 
xlab("data") +  ylab("Average temperature")

这是图 在此处输入图片说明

df_tidy1看起来像:

data.frame(stringsAsFactors=FALSE,
        date = c("1980-01-01", "1981-01-01", "1982-01-01", "1983-01-01",
                 "1984-01-01", "1985-01-01"),
        Cell = c("Acsess.4.5", "Acsess.4.5", "Acsess.4.5", "Acsess.4.5",
                 "Acsess.4.5", "Acsess.4.5"),
       Ratio = c(29.8715846994536, 29.5917808219178, 29.7479452054795,
                 30.2602739726027, 29.266393442623, 29.5342465753425)
)

df_summary看起来像

 data.frame(
        date = c("1980-01-01", "1981-01-01", "1982-01-01", "1983-01-01",
                 "1984-01-01", "1985-01-01"),
           n = c("5", "5", "5", "5", "5", "5"),
        mean = c("29.8715846994536", "29.5917808219178", "29.7479452054795",
                 "30.2602739726027", "29.266393442623", "29.5342465753425"),
          sd = c("0", "0", "0", "0", "0", "0"),
         sem = c("0", "0", "0", "0", "0", "0"),
    CI_lower = c("29.8715846994536", "29.5917808219178", "29.7479452054795",
                 "30.2602739726027", "29.266393442623", "29.5342465753425"),
    CI_upper = c("29.8715846994536", "29.5917808219178", "29.7479452054795",
                 "30.2602739726027", "29.266393442623", "29.5342465753425")
)
ung

您没有提供df_tidy2df_summary2但这应该为您提供一个良好的开端

library(tidyverse)

ggplot() +
  geom_line(data = df_tidy1, aes(x = date, y = Ratio, group = Cell), color = "grey") +
  # geom_line(data = df_tidy2, aes(x = date, y = Ratio, group = Cell), color = "grey") +
  geom_line(data = df_summary1, aes(x = date, y = mean, color = "RCP4.5")) +
  # geom_line(data = df_summary2, aes(x = date, y = mean, color = "blue")) +
  geom_ribbon(data = df_summary1, aes(x = date, ymin = CI_lower, ymax = CI_upper), 
              fill = "blue", alpha = 0.2) +
  # geom_ribbon(data = df_summary2, aes(x = date, ymin = CI_lower, ymax = CI_upper), fill = "grey", alpha = 0.2) +
  xlab("Date") + 
  ylab("Average temperature") +
  scale_color_manual("Legend", values = c("blue")) +
  scale_x_date(expand = c(0, 0), date_breaks = '18 months', date_labels = "%b-%Y") +
  theme_classic(base_size = 14)

reprex软件包(v0.2.1.9000)创建于2018-11-16

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章