Reputation: 1651
The following R code creates a graph with two interactive sliders which control the range of the x-axis:
library(manipulate)
x = 1:100
y = 1:100*2
manipulate(plot(x, y, xlim=c(x.min,x.max)),
x.min = slider(min(x),max(x)),
x.max = slider(min(x),max(x)) )
Now I would like to do the same but the x
values to be a time series. For example:
# Create a date vector length 100 in day increments from the date "2014-01-01"
x = seq(from=as.POSIXct("2014-01-01"), by=as.difftime(1, units="days"), length.out=100)
y = 1:100*2
manipulate(plot(x, y, xlim=c(x.min,x.max)),
x.min = slider(min(x),max(x)),
x.max = slider(min(x),max(x)) )
However, I get an error:
Error in slider(min(x), max(x)) : min, max, amd initial must all be numeric values
I assume this is because min(x)
is not numeric, its a date. But how can I do this with dates?
Upvotes: 1
Views: 781
Reputation: 1616
I have outfitted your question based on the following SO link: How to manipulate (using manipulate pkg) ggplot2 with time-stamped axis?
Prior to using manipulate
, it would be a good idea to combine your x
and y
lists into a data frame. I also switched your POSIXct
class to Date
since the H:M:S are just "00:00:00".
> df <- data.frame(x = seq(from=as.POSIXct("2014-01-01"), by=as.difftime(1, units="days"), length.out=100), y = 1:100*2)
> df$x <- as.Date(df$x, format = "%Y-%m-%d")
> with(df, manipulate(plot(x, y, xlim=c(x.min, x.max), xlab = "Date", ylab = "Value"),
+ x.min = slider(as.numeric(min(x)),as.numeric(max(x)), label = "Minimum Date"),
+ x.max = slider(as.numeric(min(x)),as.numeric(max(x)), label = "Maximum Date")
+ )
+ )
You will notice that your slider bar values are numeric but the final output on your graph is in a "Date" format.
Upvotes: 1