How can a color gradient based on date be applied to a ggplot2 scatter plot?
In the example below, I would like the more recent points to be a darker shade of blue (or alternately, less transparent).
data <- data.frame(x=c(1,2,3,4,5,6,7,8,9,10,11,12),
y=c(1,2,3,4,5,6,7,8,9,10,11,12),
dt=c("2010-12-31","2010-11-30","2010-10-29","2010-09-30",
"2010-08-31","2010开发者_如何学JAVA-07-30","2010-06-30","2010-05-31","2010-04-30",
"2010-03-31","2010-02-26","2010-01-29"))
p <- ggplot(data, aes(x,y,color=dt)) + geom_point() +
scale_colour_gradient(limits=c("2010-01-29","2010-12_31"),
low="white", high="blue")
print(p)
the dt is factor variable, and probably scale_*_gradient is not available with discrete variable by nature.
you can convert the dt into Date and then into integer that is continuous variable.
Here is an example:
ggplot(data, aes(x,y, colour=as.integer(as.Date(data$dt)))) +
geom_point() +
scale_colour_gradient(limits=as.integer(as.Date(c("2010-01-29","2010-12-31"))),
low="white", high="blue") +
opts(legend.position="none")
精彩评论