Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在ggplot2中创建绘图(通过包)后更改图例中的跟踪名称_R_Ggplot2_Plot_Legend - Fatal编程技术网

R 在ggplot2中创建绘图(通过包)后更改图例中的跟踪名称

R 在ggplot2中创建绘图(通过包)后更改图例中的跟踪名称,r,ggplot2,plot,legend,R,Ggplot2,Plot,Legend,我想知道是否有任何简单的方法可以在一个ggplot创建后更改图例中的名称(使用颜色美学)。我知道这感觉有点粗糙,通常会在数据中或创建绘图时更改,但我想更改由另一个包创建的绘图上的标签,并且在该包中没有更改它的选项 很明显,我可以复制这个函数,保存我自己的版本并对其进行更改,但我只想更改一件事,这样看起来更整洁,如果我以后再做的话 这里是一个有一些虚拟数据的例子,基本上我想将fasstr的绘图(每日)统计数据中的平均值和中位数时间序列重新标记为“建模平均值”和“建模中位数”,这样它们就不会与我手动

我想知道是否有任何简单的方法可以在一个ggplot创建后更改图例中的名称(使用颜色美学)。我知道这感觉有点粗糙,通常会在数据中或创建绘图时更改,但我想更改由另一个包创建的绘图上的标签,并且在该包中没有更改它的选项

很明显,我可以复制这个函数,保存我自己的版本并对其进行更改,但我只想更改一件事,这样看起来更整洁,如果我以后再做的话

这里是一个有一些虚拟数据的例子,基本上我想将fasstr的
绘图(每日)统计数据
中的平均值和中位数时间序列重新标记为“建模平均值”和“建模中位数”,这样它们就不会与我手动添加的观察平均值混淆

    library(fasstr)
    library(tibble)
    library(ggplot2)

    #create some fake data
    df <- tibble(Date = seq.Date(from = as.Date("1991-01-01"), as.Date("1997-12-31"), 
                                      by = "day"),
                     DayOfYear = as.numeric(format(Date, "%j")),
                     Value = runif(2557,0,1) + 50 + (cos((1/60)*DayOfYear)+4))

    obsdf <- tibble(Date = seq.Date(from = as.Date("1900-01-01"), as.Date("1900-12-31"), 
                            by = "day"),
                 DayOfYear = as.numeric(format(Date, "%j")),
                 Value = runif(365,0,1) + 51 + (cos((1/60)*DayOfYear)+4))



    # create plot using fasstr package
    plt1<- fasstr::plot_daily_stats(df)

    # add my own trace. I also want to rename the trace "Mean" to 
     # "Modelled Mean" to avoid confusion (and same with Median) 
    plt1$Daily_Statistics +
      geom_line(data = obsdf, aes(x = Date, y = Value, colour = "Observed Mean"))+
      scale_colour_manual(values = c("red", "black","blue"))

无需黑客攻击,只需将标签添加到手动秤上即可

plt1$Daily_Statistics +
  geom_line(data = obsdf, aes(x = Date, y = Value, colour = "Observed Mean"))+
  scale_colour_manual(labels = c("Modelled Mean","Modelled Median","Observed Mean"),
                      values = c("red", "black","blue"))

没问题。一开始我也没想到。
plt1$Daily_Statistics +
  geom_line(data = obsdf, aes(x = Date, y = Value, colour = "Observed Mean"))+
  scale_colour_manual(labels = c("Modelled Mean","Modelled Median","Observed Mean"),
                      values = c("red", "black","blue"))