R用ggplot2水平线表示平均值

时间:2016-04-04 22:06:52

标签: r ggplot2 average

我的脚本从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))
你可以给我一些建议吗?

1 个答案:

答案 0 :(得分:4)

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))