Skip to content

Instantly share code, notes, and snippets.

@dmarcelinobr
Created January 1, 2016 21:54
Show Gist options
  • Save dmarcelinobr/6884a308619e1e6a9750 to your computer and use it in GitHub Desktop.
Save dmarcelinobr/6884a308619e1e6a9750 to your computer and use it in GitHub Desktop.
ggplot2 geom_line() example with disconnected lines for NA values
# ggplot2 example
# Show how geom_line() does not connect the line through NA values
library(ggplot2)
# Make some fake data
mydata <- data.frame(x = 2000:2010, y = rnorm(11, 5, 2))
# First plot, all lines are connected correctly
ggplot(mydata, aes(x, y)) + geom_line()
# Now let's make some of the values NA
mydata[sample(1:nrow(mydata), 2, replace = FALSE),"y"] <- NA
# Now plot the result with the new NAs introduced
ggplot(mydata, aes(x, y)) + geom_line()
# We should see a plot with disconnected lines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment