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.
If NULL, the default, the data is inherited from the plot data as specified in the call to ggplot (). A data.frame, or other object, will override the plot data. All objects will be fortified to produce a data frame.
The documentation for geom_line () indicates that NA values are always ignored (and that the only difference with na.rm or not is if ignoring NA comes with a warning. They don't currently appear to be ignored as the break the lines in the first two figures below.
geom_path(), geom_line(), and geom_step handle NA as follows: If an NA occurs in the middle of a line, it breaks the line. No warning is shown, regardless of whether na.rm is TRUE or FALSE. If an NA occurs at the start or the end of the line and na.rm is FALSE (default), the NA is removed with a warning.
Here we use dplyr to remove missing values from the graph. The is.na () operator detects whether a value is missing. ! means not, so our dplyr call ends up being filter (!is.na (neighborhood)).
geom_line
does make breaks for NA
s 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()
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()
Presumably your code would be:
ggplot(crew.twelves, aes(x=laffcu, y=as.POSIXlt(date)) + geom_line() +
coord_flip()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With