New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scale_x_date limits not subsetting data #1090

Closed
iankotliar opened this Issue Apr 27, 2015 · 5 comments

Comments

Projects
None yet
2 participants
@iankotliar

iankotliar commented Apr 27, 2015

Posted on Stack Overflow:

http://stackoverflow.com/questions/29900106/ggplot2-bug-ggplot-showing-not-dropping-data-outside-limits-of-scale#29900106

I've noticed that scale_x_date 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))

resulting plot

n <- as.Date("2009/1/1")
ggplot(w,aes(x,val))+geom_point()+
scale_x_date(limits=c(n,NA))+
  geom_vline(xintercept=as.numeric(n),colour="red")

resulting plot

@iankotliar iankotliar changed the title from scale_x_date limits not properly subsetting data to scale_x_date limits not subsetting data Apr 28, 2015

@hadley

This comment has been minimized.

Member

hadley commented Apr 28, 2015

Limits are automatically expanded using the expand parameter.

@hadley hadley closed this Apr 28, 2015

@iankotliar

This comment has been minimized.

iankotliar commented Apr 28, 2015

Just to be clear, the limits of the scales are controlled by the expand parameter, but the limits of the data should not be affected. At least this is the behavior when using scale_x_continuous. Currently scale_x_date is functioning differently than scale_x_continuous.

See below

  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

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

resulting plot

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

resulting plot

@hadley

This comment has been minimized.

Member

hadley commented Apr 28, 2015

Oh, hmmm, that's a more compelling bug.

@hadley hadley reopened this Apr 28, 2015

@hadley

This comment has been minimized.

Member

hadley commented Jul 21, 2015

Here's a somewhat simpler reproducible example:

library(ggplot2)

df <- data.frame(
  x = as.Date("2000-01-01") + 0:50,
  y = 1, 
  z = 0:50
)
ggplot(df, aes(x, y, colour = z >= 1)) +
  geom_point()

ggplot(df, aes(x, y, colour = z >= 1)) +
  geom_point() + 
  scale_x_date(limits = c(df$x[[2]], NA))

ggplot(df, aes(as.numeric(x), y, colour = z >= 1)) +
  geom_point() + 
  scale_x_continuous(limits = c(df$x[[2]], NA))
@hadley

This comment has been minimized.

Member

hadley commented Jul 21, 2015

Fixed in ae1303f

@hadley hadley closed this Jul 21, 2015

@lock lock bot locked as resolved and limited conversation to collaborators Jun 20, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.