Skip to content

Instantly share code, notes, and snippets.

@amoeba
Created April 3, 2015 18:41
Show Gist options
  • Select an option

  • Save amoeba/ab1408c231ef983b8903 to your computer and use it in GitHub Desktop.

Select an option

Save amoeba/ab1408c231ef983b8903 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