I am having an issue where ggplot2 is displaying data outside of the limits specified in the scale. See example below. Why am I seeing points before 2009/01/01 and after 2015/01/01 in the graph if the limits are set to these values?
library(ggplot2)
library(scales)
set.seed(100)
z <- seq.Date(as.Date("2008/12/1"), as.Date("2015/12/14"), "day")
l <- expand.grid(z, c("a", "b", "c"))
w <- data.frame(x= l[, 1], t = l[, 2])
w$val <- runif(nrow(w))
ggplot(data=w, aes_string(x="x", y="val"))+scale_x_date(
labels = date_format("%m/%d/%Y"),
limits= c(as.Date("2009/1/1"), as.Date("2015/1/1")),
breaks = "1 year")+
geom_point(aes(color = t))
Is it possible to still leave the breaks/scale as specified but just remove the data outside the limits using ggplot/without pre-filtering the data? This seems like a bug to me. The documentation states that limits filter data.

This does look like a bug to me.
library(ggplot2)
library(scales)
set.seed(100)
z <- seq.Date(as.Date("2008/12/1"), as.Date("2015/12/14"), "day")
l <- expand.grid(z, c("a", "b", "c"))
w <- data.frame(x= l[, 1], t = l[, 2])
w$val <- runif(nrow(w))
n <- as.Date("2009/1/1")
With scale_x_continuous:
ggplot(w,aes(as.numeric(x),val))+geom_point()+
scale_x_continuous(limits=c(as.numeric(n),NA))+
geom_vline(xintercept=as.numeric(n),colour="red")

Now with scale_x_date:
ggplot(w,aes(x,val))+geom_point()+
scale_x_date(limits=c(n,NA))+
geom_vline(xintercept=as.numeric(n),colour="red")

I would post this at the ggplot issues list, and in the meantime work around it with subset().
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