How can I have a date field formatted properly in a smoothScatter plot?
I have data that looks like this:
> head(data)
date price volume
1 2011-06-26 17:16:05 17.51001 2.000
2 2011-06-26 20:50:00 14.80351 2.981
3 2011-06-26 20:51:00 14.90000 2.000
4 2011-06-26 20:52:00 14.89001 0.790
5 2011-06-26 20:53:00 15.00000 1.000
6 2011-06-26 21:05:01 16.20000 6.500
> str(head(data))
'data.frame': 6 obs. of 3 variables:
$ date : POSIXct, format: "2011-06-26 17:16:05" "2011-06-26 20:50:00" "2011-06-26 20:51:00" "2011-06-26 20:52:00" ...
$ price : num 17.5 14.8 14.9 14.9 15 ...
$ volume: num 2 2.98 2 0.79 1 ...
When I plot it like this:
someColors <- colorRampPalette(c("black", "blue", "o开发者_如何转开发range", "red"), space="Lab")
smoothScatter(data, colramp=someColors)
I get almost exactly what I'm looking for, but it converts the posix dates to numbers. How can I set the x labels more usefully so that my stuff is a bit more readable?
(source: skitch.com)Edit: I can get an approximation of what I want like this:
smoothScatter(data, colramp=someColors, xaxt="n")
axis(1, at=data$date,
labels=lapply(data$date, function(d) strftime(d, "%F")),
tick=FALSE)
That's terribly slow, though. It seems like I should be able to prep the data or advice the label drawer a bit.
In terms of speed, it might help to specify range of dates to use for the x-axis labels. For example:
days <- seq(min(data$date), max(data$date), by = 'month')
axis(1, at=days,
labels=strftime(days, "%F"),
tick=FALSE)
It might also help to round the times to the nearest day:
days <- seq(as.Date(min(data$date)), as.Date(max(data$date)), by = 'month')
精彩评论