r - ggplot2错误: scale_x_date Showing/Not Dropping Data Outside Specified Limits

标签 r ggplot2

我遇到一个问题,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/删除超出限制的数据而不预先过滤数据?这对我来说似乎是一个错误。该文档指出限制过滤器数据。

resulting plot

最佳答案

这对我来说确实像是一个错误。

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

enter image description here

现在使用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

我会将其发布在 ggplot issues list ,同时使用 subset() 解决这个问题。

关于r - ggplot2错误: scale_x_date Showing/Not Dropping Data Outside Specified Limits,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29900106/

相关文章:

r - 按行而不是按列用向量填充下矩阵

从内联 Rhtml knitr 代码中删除 <code> 标签

java - 将多个 .doc 合并到一个 .csv 中

r - 打印一些 HEX 颜色以查看它们

从长格式的值重建对称矩阵

R 调用具有超过 2 个参数的中缀函数

r - ggplot 中的条隐藏负标签 : geom_bar

r - 将文本添加到 ggplot 中的 geom_line

r - `level` 如何用于在 geom_smooth 中生成置信区间?

带有转换因变量的 ggplot2 线性回归的 R 图