3

我正在尝试使用 scale_x_datetime 来优化我的 x 轴。我从ggplot2 website阅读了文档,但是这个站点中的示例产生了与我得到的相同的错误:

> library(ggplot2)
> start <- ISOdate(2001, 1, 1, tz = "") 
> df <- data.frame( 
+    day30  = start + round(runif(100, max = 30 * 86400)), 
+    day7  = start + round(runif(100, max = 7 * 86400)), 
+    day   = start + round(runif(100, max = 86400)), 
+    hour10 = start + round(runif(100, max = 10 * 3600)), 
+    hour5 = start + round(runif(100, max = 5 * 3600)), 
+    hour  = start + round(runif(100, max = 3600)), 
+    min10 = start + round(runif(100, max = 10 * 60)), 
+    min5  = start + round(runif(100, max = 5 * 60)), 
+    min   = start + round(runif(100, max = 60)), 
+    sec10 = start + round(runif(100, max = 10)), 
+    y = runif(100) 
+ ) 
> 
> last_plot() + scale_x_datetime(major = "2 weeks", minor = "1 week") 
Error in continuous_scale(aesthetics, "datetime", identity, breaks = breaks,  : 
  unused argument(s) (major = "2 weeks", minor = "1 week")
>

那么文档是否已过时或这里有什么问题?

4

1 回答 1

7

您使用的文档已过时,它适用于 pre v0.9.0。在这里可以找到正确的。您的示例现在应该如下所示:

library(ggplot2)
library(scales) # for date_breaks()
start <- ISOdate(2001, 1, 1, tz = "") 
df <- data.frame( 
    day30  = start + round(runif(100, max = 30 * 86400)), 
    day7  = start + round(runif(100, max = 7 * 86400)), 
    day   = start + round(runif(100, max = 86400)), 
    hour10 = start + round(runif(100, max = 10 * 3600)), 
    hour5 = start + round(runif(100, max = 5 * 3600)), 
    hour  = start + round(runif(100, max = 3600)), 
    min10 = start + round(runif(100, max = 10 * 60)), 
    min5  = start + round(runif(100, max = 5 * 60)), 
    min   = start + round(runif(100, max = 60)), 
    sec10 = start + round(runif(100, max = 10)), 
    y = runif(100) ) 

qplot(sec10, y, data = df) 
last_plot() + scale_x_datetime(breaks = date_breaks("2 weeks"),
                               minor_breaks = date_breaks("1 week"))
于 2012-08-17T08:41:09.473 回答