我遇到一个问题,ggplot2
显示的数据超出了比例尺中指定的限制。请参阅下面的示例。如果限制设置为这些值,为什么我会在图表中看到 2009/01/01 之前和 2015/01/01 之后的点?
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))
是否仍然可以保留指定的中断/比例,但仅使用 ggplot/删除超出限制的数据而不预先过滤数据?这对我来说似乎是一个错误。该文档指出限制过滤器数据。
最佳答案
这对我来说确实像是一个错误。
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")
使用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")
现在使用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")
我会将其发布在 ggplot issues list ,同时使用 subset()
解决这个问题。
关于r - ggplot2错误: scale_x_date Showing/Not Dropping Data Outside Specified Limits,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29900106/