用ggplot绘制水平线

时间:2015-07-28 07:39:00

标签: r ggplot2

我想在ggplot2的折线图中制作一条红色的水平平均线,但我不知道该怎么做。它不应该代表列E的值的平均值。

我证明了这一点,但它没有奏效:

ggplot(data = df3, aes(x = df3$timestamp, y = df3[,1], group = 1)) + 
  geom_line() + 
  ylab("values") + 
  xlab("time") + 
  geom_hline(yintercept=mean(df3[,1]))

数据框称为df3,x行时间序列为列timestamp,y行为列指标E

这里是数据框头:

           E               timestamp
11 -22.30933 2015-02-09 09:05:00.712
14 -22.17142 2015-02-09 09:06:00.703
17 -21.24673 2015-02-09 09:07:00.703
20 -21.58154 2015-02-09 09:08:00.702
23 -21.07082 2015-02-09 09:09:00.702
26 -22.49973 2015-02-09 09:10:00.702

1 个答案:

答案 0 :(得分:2)

一般来说,您应该能够使用geom_line(stat = "hline", yintercept = "mean"),如下例所示:

require(ggplot2)
ggplot(mtcars, aes(x = wt, y=mpg)) + 
  geom_point() +
  geom_line(stat = "hline", yintercept = "mean", colour = "red")

line

相关问题