0

I have a big data frame with IDs, date, and test results in it - I want to run a for loop to go through all the IDs and plot a line graph that shows the evolution of the results across time, but also to add some key-indicators as points in the graph. (key indicators come from a different data frame).

Not all the IDs have all the indicators - the problem is that when I plot these points, I use dplyr filtering to filter for ID == i and something like key_ind != 0. My problem is that when there are no key indicators for a certain ID, the filtered data frame has 0 observations and ggplot returns an error.

I want that when there are no points to plot (the test results still get plotted as a line) - they wont be plotted, but the line graph of the results still be plotted. Does that make sense? How can I do that? I have tried using tryCatch() but it didn't work.

Corel
  • 581
  • 3
  • 21
  • 3
    the question asked will attract interest provided it is accompanied with a reproducible example. See this [post](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on how to create one. – mnm Jun 03 '18 at 11:33
  • 2
    You can set an if else condition and run the ggplot() only when nrow(Data)!=0 – Vedha Viyash Jun 03 '18 at 11:44
  • Yes, eventually that's what I did, I just thought that there is a more elegant solution. – Corel Jun 03 '18 at 11:59
  • It seems likely there is a more elegant solution, but you should provide some sample data (or better, a reproducible example). – Weihuang Wong Jun 03 '18 at 13:08

0 Answers0