0

My question is related to the second answer to this question and also my previous question (posted below).

Proper way to plot multiple data series in ggplot with custom colors, legend etc

How is it possible to plot a subset of lines according to user's needs (i.e. first and third, all four etc.)? Currently I am using base plot() and would like to change to ggplot2 solution. I need to address that the time series have uneven lengths, as in the original question. I was experimenting with the syntax simialr to the following, but without success:

ggplot(data = dat, aes(x=x, y=y)) + geom_line(aes(y = y[dat$grp == "a"], colour=grp)) + geom_point()

Error: Aesthetics must be either length 1 or the same as the data (20): y, colour, x

P.S.: For those interested, this is what I am trying to do (I keep my previous question updated, however I am the only one posting there): I am trying to build this functionality into my Shiny application. I.e. user chooses a number of parameters, which I convert to a set of IDs which correspond to a subset of time series in my dataframe and the plot outputs the chosen subset of lines.

Shiny + ggplot2: plotting a number of lines in one graph based on user input

Community
  • 1
  • 1
pun11
  • 157
  • 1
  • 11
  • 1
    Use `subset(dat, grp == "a")` as the data instead of messing around inside the `aes`. – Axeman Jan 06 '17 at 13:59
  • Thank you very much. It works for a reasonable number of lines. Now I have hundreds of lines to plot, i.e. hundreds of conditions of a form `subset(dat, grp == "a" | grp == "b" | ... )`. Is there a more elegant, easier solution to subset, eg. something similar to specifying `grp == c("a", "b", ...)`? – pun11 Jan 06 '17 at 14:12
  • 2
    Many thanks. It is for people like you that StackOverflow is a great place. If you want to write an answer, I will be glad to accept it. – pun11 Jan 06 '17 at 14:19
  • You're welcome. There is probably a duplicate somewhere, but I'm on my phone so I can't find it. – Axeman Jan 06 '17 at 14:20
  • It is possible. I spent a lot of time searching, so I just created this question – pun11 Jan 06 '17 at 14:36

0 Answers0