Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 geom_line() to skip NA values

Tags:

r

ggplot2

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.

like image 487
Reuben Mathew Avatar asked Aug 10 '13 15:08

Reuben Mathew


People also ask

What happens if plot data is null in ggplot?

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.

Are NA values ignored in Geom_line ()?

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.

How does Geom_path () handle Na in Python?

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.

How do I remove missing values from a graph in Python?

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)).


1 Answers

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()
like image 128
Drew Steen Avatar answered Sep 25 '22 14:09

Drew Steen