2

I consider the following plot.

d=data.frame(x=c(1,2,4,5,7,8,9), y=c(0,2,3,5,6,7,9))
d2=data.frame(x=c(1,2,3.5,7.2,9), y=c(0,3,7,1.5,4))
ggplot() + geom_step(data=d, mapping=aes(x=x, y=y)) + geom_step(data=d2, mapping=aes(x=x, y=y), color=2)  

We have a plot with multiple (here two) step functions that have different number and location of change points on x. So I cannot directly add them to the same data.frame without more data manipulation. What would be the best option to add a legend to this plot?

enter image description here

tomka
  • 2,516
  • 7
  • 31
  • 45
  • 2
    Follow rcs' advise: https://stackoverflow.com/a/3777592/8583393 – markus May 28 '20 at 18:37
  • 1
    @markus Thanks but I do not think rcs' advice directly applies here. In their `test_data` each date is associated with one value but there are equally many observations. In my data I have different change points (times) and different number of change points! So you would need to explain first how I can get to a format like `test_data` from my data `d` and `d2` before I can use rcs' advice to use `melt` to bring data in a long format used by ggplot. Please reconsider whether this question should be reopened. – tomka May 29 '20 at 13:53

2 Answers2

3

Here you go, name the geom with a string inside the aes, then use a named vector for the colors using those names.

  ggplot() + 
      geom_step(data=d, mapping=aes(x=x, y=y, color = "name1")) + #name the layer
      geom_step(data=d2, mapping=aes(x=x, y=y, color= "name2")) +
      scale_color_manual(name = "legend title",
                         values = c("name1" = "black", #match the name of the element to the name in the aes
                                    "name2" = "red"))
NotThatKindODr
  • 729
  • 4
  • 14
1

I may have completely misunderstood the question, but I don't fully understand what the problem of a classic "making long" approach is?

The different x do not matter. Internally, the geom calculates the statistics for each group defined by aes, kind of creating n different data sets from your data. IMO, this is the "most ggplot-y" way, because it makes use of the advantages of mapping a variable to an aesthetic.

library(tidyverse)
d <- data.frame(x = c(1, 2, 4, 5, 7, 8, 9), y = c(0, 2, 3, 5, 6, 7, 9))
d2 <- data.frame(x = c(1, 2, 3.5, 7.2, 9), y = c(0, 3, 7, 1.5, 4))

d_bind <- bind_rows(list(d = d, d2 = d2), .id = "id")

ggplot() +
  geom_step(data = d_bind, mapping = aes(x = x, y = y, color = id)) +
  scale_color_manual(values = c(d = "black", d2 = "red"))

tjebo
  • 21,977
  • 7
  • 58
  • 94