4

I try to add a legend by shape.

I have a graphic with one legend despite of two shapes and two y axes.

enter image description here

and I would like it with two group of legend, one for each y axe.

enter image description here

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

phili_b
  • 885
  • 9
  • 27
  • I can't reproduce your output using your code ( the secondary axis is coming out different on my system). – deepseefan Sep 20 '19 at 02:09
  • It doesn't bother if fake datas are non determinists: my question is only about shapes in legend :) – phili_b Sep 23 '19 at 18:54

1 Answers1

0

You can use the linetype property and manually assign the legend names and line colors, as follows:

(
  ggplot(datas) 
  +   geom_line(mapping=aes(x = date_heure, y = t, color=orig, linetype = "s1"))
  +   geom_line(mapping=aes(x = date_heure, y = calc, color=orig, linetype = "s2"))
  +   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")
  +   scale_color_manual(name = "calc", values=c("red", "blue"))
  +   scale_linetype_manual(name = "orig", values = c('solid', 'solid'), 
                            guide = guide_legend(override.aes = list(colour=c("red", "blue"))))

)

This produces the following plot:

enter image description here

Hope it helps.

Taher A. Ghaleb
  • 5,120
  • 5
  • 31
  • 44
  • thanks, I've added a 2nd question. In fact the origin of the question. – phili_b Sep 20 '19 at 05:03
  • Hi @phili_b, You should probably ask a separate question for that and I'll be happy to answer it. Please accept this answer if it worked for you. Thanks. – Taher A. Ghaleb Sep 20 '19 at 05:20
  • My 2nd question from this one [How a simpler derive is written in R by group (in R, ggplot, dplyr, tidyverse)?](https://stackoverflow.com/questions/58026182/how-a-simpler-derive-is-written-in-r-by-group-in-r-ggplot-dplyr-tidyverse) – phili_b Sep 20 '19 at 10:05
  • 1
    I answered your second question. Please check it out. – Taher A. Ghaleb Sep 20 '19 at 15:35
  • a little warning but not about your answer : It doesn't work with plotly.  Every legends have to be reset with plotly [plotly_build modifies legend and labels](https://stackoverflow.com/questions/43278035/plotly-build-modifies-legend-and-labels) – phili_b Sep 24 '19 at 19:12
  • 1
    I see. I'll check it out and get back to you. Thanks. – Taher A. Ghaleb Sep 25 '19 at 02:57