【问题标题】:R with ggplot2 horizontal line for averageR 用 ggplot2 水平线表示平均值
【发布时间】:2016-04-04 22:06:51
【问题描述】:

我的脚本从 csv 文件中读取数据。我正在计算平均值,但我希望将图表上的平均值视为水平线。

avg = myData$Electricity.Costs
mean(avg)

ggplot(data = myData, 
       aes(x = Date, y = Electricity.Costs, 
           group = Budget.Plan.Monthly.Amount, colours = "Budget.Plan.Monthly.Amount")) + 
   geom_line() + 
   geom_point(aes(colour = Budget.Plan.Monthly.Amount))

你能给我一些建议吗?

【问题讨论】:

    标签: r ggplot2 average


    【解决方案1】:
    ggplot(data = myData, 
       aes(x = Date, y = Electricity.Costs, 
           group = Budget.Plan.Monthly.Amount, colours = "Budget.Plan.Monthly.Amount")) + 
    geom_line() + 
    geom_point(aes(colour = Budget.Plan.Monthly.Amount))+
    geom_hline(yintercept = mean(avg))
    

    【讨论】:

    • 非常感谢,我需要将 yintercept = avg 更改为 yintercept = mean(avg)。现在工作正常:)
    猜你喜欢
    • 1970-01-01
    • 2013-03-24
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-14
    • 2011-01-30
    相关资源
    最近更新 更多