7

I have a data set that has multiple NA values in it. When plotting this data, ggplot's geom_line() option joins lines across the NA values. Is there any way to have ggplot skip joining the lines across NA values?

Edit: A thousand apologies to all involved. I made a mistake in my manipulation of the data frame. I figured out my problem. My x axis was not continuous when I created a subset. The missing data had not been replaced by NAs, so the data was being linked because there were no NAs created in the subset between rows.

cincodenada
  • 2,877
  • 25
  • 35
Reuben Mathew
  • 598
  • 4
  • 22
  • remove them from the dataframe before you plot it. – Simon O'Hanlon Aug 10 '13 at 15:04
  • Would you mind showing us your data and your code + your plot and explain what you require?? – Arun Aug 10 '13 at 15:26
  • Please *delete your comment* and ***edit your question*** with this instead. – Arun Aug 10 '13 at 17:30
  • You can't upload plot right now because you don't have enough reputation; I think you need 50 reputation or so. Thanks for posting your actual code, but please note that it would be more helpful to post a _minimal_, _reproducible_ example (since I don't have your `crew.twelves` data frame, I can't do much with your code. Instructions for good reproducible examples are here: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Drew Steen Aug 10 '13 at 17:33
  • Please see: http://stackoverflow.com/questions/9617629/connecting-across-missing-values-with-geom-line – PatrickT Dec 28 '14 at 11:10
  • This question is seriously lacking a MWE... – PatrickT Dec 28 '14 at 11:11

1 Answers1

16

geom_line does make breaks for NAs in the y column, but it joins across NA values in the x column.

# Set up a data frame with NAs in the 'x' column
independant <- c(0, 1, NA, 3, 4)
dependant <- 0:4
d <- data.frame(independant=independant, dependant=dependant)

# Note the unbroken line
ggplot(d, aes(x=independant, y=dependant)) + geom_line()

enter image description here

I assume that your NA values are in your as.POSIXlt(date). If so, one solution would be to map the columns with NA values to y, and then use coord_flip to make the y axis horizontal:

ggplot(d, aes(x=dependant, y=independant)) + geom_line() +
  coord_flip()

enter image description here

Presumably your code would be:

ggplot(crew.twelves, aes(x=laffcu, y=as.POSIXlt(date)) + geom_line() +
  coord_flip()
Drew Steen
  • 16,045
  • 12
  • 62
  • 90
  • You hit it right on the head. The subset created for the independent values were missing NAs and were thus providing R with the continuous data. R was performing as expected. PEBCAK to be sure. – Reuben Mathew Aug 10 '13 at 17:59
  • 3
    Welcome to SO. Please note that it is good form to upvote any helpful answer (by clicking on the little up arrow near the top of the answer) and to 'accept' the best one (by clicking on the green arrow by the answer). Doing this will motivate future users to answer your questions. – Drew Steen Aug 10 '13 at 20:03
  • 2
    If you would believe it, I need to have a reputation of 15 to upvote your response. I promise you that I will be back here when I have a reputation of 15 to upvote your response. I have a reputation of 6 right now. – Reuben Mathew Aug 11 '13 at 02:29