11

I want to add an average line to the existing plot.

library(ggplot2)

A <- c(1:10)
B <- c(1,1,2,2,3,3,4,4,5,5)

donnees <- data.frame(A,B) 
datetime<-donnees[,2]
Indcatotvalue<-donnees[,1]
df<-donnees

mn<-tapply(donnees[,1],donnees[,2],mean)
moyenne <- data.frame(template=names(mn),mean=mn)

ggplot(data=df,
   aes_q(x=datetime,
         y=Indcatotvalue)) + geom_line() 

I have tried to add :

geom_line(aes(y = moyenne[,2], colour = "blue"))

or :

lines(moyenne[,1],moyenne[,2],col="blue")

but nothing happens, I don't understand especially for the function "lines".

zx8754
  • 52,746
  • 12
  • 114
  • 209
Flo
  • 151
  • 1
  • 1
  • 6
  • Welcome to SO. Please read [how to provide minimal reproducible examples in R](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#answer-5963610). Then edit & improve it accordingly. A good post usually provides minimal input data, the desired output & what has been tried - ready to copy-paste-run. When I run your code, I get "object 'donnees' not found". Please provide data + required libraries etc. Also, I don't think the shiny context is needed here ("minimal example"). – lukeA Jul 21 '16 at 10:59
  • It doesn't matter if you use ggplot inside R or Shiny. Google for this using `ggplot` and `line` and you will find help such as http://www.sthda.com/english/wiki/ggplot2-add-straight-lines-to-a-plot-horizontal-vertical-and-regression-lines – Roman Jul 21 '16 at 11:18
  • I was looking to see how to add a label, this is a good solution https://stackoverflow.com/questions/32425784/r-ggplot2-labeling-a-horizontal-line-without-associating-the-label-with-a-serie – Beniam Sep 18 '20 at 10:51

2 Answers2

20

When you say average line I'm assuming you want to plot a line that represents the average value of Y (Indcatotvalue). For that you want to use geom_hline() which plots horizontal lines on your graph:

ggplot(data=df,aes_q(x=datetime,y=Indcatotvalue)) +
  geom_line() +
  geom_hline(yintercept = mean(Indcatotvalue), color="blue")

Which, with the example numbers you gave, will give you a plot that looks like this:

plot with stepped with average line

HSchmale
  • 1,838
  • 2
  • 21
  • 48
Simon
  • 9,762
  • 15
  • 62
  • 119
  • Thanks a lot for your answer @Simon. In fact, I would like a line that would represent all the average points (1.5, 3.5, 5.5, 7.5, 9.5) in this example. The line would be represented by ((1, 1.5), (2, 3.5), (3, 5.5), (4, 7.5), (5, 9.5)) for the pairs (x,y) – Flo Jul 21 '16 at 12:34
  • 4
    In case you are also using this solution but it is not working, use the following piece of code `ggplot(data=df,aes_q(x=datetime,y=Indcatotvalue)) + geom_line() + geom_hline(aes(yintercept = mean(Indcatotvalue)), color="blue")` – Kots Jan 26 '18 at 14:29
4

The function stat_summary is perfect here.

I have found the answer in this page groups.google from Brian Diggs:

p + stat_summary(aes(group=bucket), fun.y=mean, geom="line", colour="green")

You need to set the group to the faceting variable explicitly since otherwise it will be type and bucket (which looks like type since type is nested in bucket).

zx8754
  • 52,746
  • 12
  • 114
  • 209
Flo
  • 151
  • 1
  • 1
  • 6