Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scale_x_datetime generates an error?

Tags:

r

ggplot2

I'm trying to use scale_x_datetime for refining my x axis. I read the documention from the ggplot2 website, but the example in this site generates the same error that I'm getting:

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

So is the documentation out of date or what is the problem here?

like image 330
jrara Avatar asked Jul 26 '26 15:07

jrara


1 Answers

The documentation you used is out of date, it is for pre v0.9.0. The correct one is found here. Your example should now look like this:

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"))
like image 117
ROLO Avatar answered Jul 29 '26 10:07

ROLO



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!