Scatter plot with ggplot2 colored by dates

Solution 1:

Just add a labeler function:

ggplot(cars,aes(speed,dist,colour=as.integer(dt))) + geom_point(alpha = 0.6) +
  scale_colour_gradientn(colours=c('red','green','blue'), labels=as.Date)

enter image description here

Solution 2:

Getting around the 'origin must be supplied error'... One approach is to make a wrapper for as.Date that has the origin specified, than call this as a labeller.

as.Date_origin <- function(x){
  as.Date(x, origin = '1970-01-01')
}

data(cars)
cars['dt'] = seq(Sys.Date(),Sys.Date()-980,-20)

ggplot(cars,aes(speed,dist,colour=as.integer(dt))) + geom_point(alpha = 0.6) +
  scale_colour_gradientn(name = 'Date', colours=c('red','green','blue'), labels=as.Date_origin)