R: add middle row to existing plot

I made an RShiny application with graph using ggplot.

Now I want to add the middle row 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 tried to add:

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

or:

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

but nothing happens :( I especially do not understand the function of the "line".

Thanks for your reply...

+4
source share
2 answers

When you say the middle line, I assume that you want to build a line representing the average value of Y ( Indcatotvalue). For this you want to use geom_hline()one that displays horizontal lines on your chart:

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

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

enter image description here

+4
source

Source: https://habr.com/ru/post/1648633/


All Articles