Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2 Bug: scale_x_date Showing/Not Dropping Data Outside Specified Limits

Tags:

r

ggplot2

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.

resulting plot

like image 550
k13 Avatar asked Apr 27 '15 15:04

k13


1 Answers

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

enter image description here

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

enter image description here

I would post this at the ggplot issues list, and in the meantime work around it with subset().

like image 195
Ben Bolker Avatar answered Oct 28 '22 15:10

Ben Bolker