使用ggplot2在R中绘制水平线表示平均值

6

我有一个脚本,可以从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))

你能给我一些建议吗?

关于IT技术,以下是需要翻译的内容:
1个回答

8
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)。现在运行正常 :) - Karol

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接