我有从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))

你能给我一些建议吗?

最佳答案

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

关于R与ggplot2水平线的平均值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36413752/

10-12 16:33