I try to add a legend by shape.
I have a graphic with one legend despite of two shapes and two y axes.
and I would like it with two group of legend, one for each y axe.
edit: question 2 added after
2) In fact it was for display numeric deriv, ie to replace my calc=t/10
by a function doing
f(x)=(t_n-t_n-1)/(date_time_n / date_time_n -1)
where f(x) will be my calc column.
but I think I don't understand derive in R.
my next question there :How a simpler derive is written in R by group (in R, ggplot, dplyr, tidyverse)?
end of edit
Here is my reprex
library(tidyverse)
library(ggplot2)
datas<-data.frame(
t = c(
50 + c(0, cumsum(runif(9, -7, 7))),
70 + c(0, cumsum(runif(9, -10, 10)))
),
orig=c(rep("s1",10),rep("s2",10)),
date_heure = rep(
seq(from=as.POSIXct("2012-1-1 0:00", tz="UTC"),by="hour", length=10) ,
2
)
)
datas<- (datas
%>% mutate (
calc=t/10
)
)
(
ggplot(datas)
+ geom_line(aes(x = date_heure, y = t,colour=orig))
+ geom_line(aes(x = date_heure, y = calc, color=orig))
+ scale_y_continuous(
name = "t",
sec.axis = sec_axis(trans=~(range(datas$calc)),
name = "calc ")
)
+ geom_point(mapping = aes(x = date_heure, y = calc,color=orig), shape = 21, fill = "white")
)