I have a dataframe having three columns: Date, X, and Y. I want to make a scatter plot between X and Y and give color to scatter points based on "Date" column. The plot should also show the color bar (with labels min(Date) to max(Date)).
Below is the sample dataframe:
df <- structure(list(Date = structure(c(17471, 17472, 17473, 17474, 17475, 17476, 17477, 17478, 17479, 17480), class = "Date"), X = c(0.796174166775646, 0.848555231972632, 0.683680046726043, 0.686642470840829, 0.720126049914675, 0.627708683319572, 0.592784894803222, 0.49770995584235, 0.92458842974608, 0.776247170913462), Y = c(0.855872728731457, 0.730466555438912, 0.733560880833523, 0.847513809554321, 0.766668352652661, 0.731916270305317, 0.657927669736621, 0.488083725626701, 0.771059482797226, 0.866358366092603 )), row.names = c(NA, 10L), class = "data.frame")
Below is what I tried so far:
rbPal <- colorRampPalette(c('red','blue'))
df$Col <- rbPal(10)[as.numeric(cut(df$Date,breaks = 10))]
plot(df$X, df$Y, xlab="X", ylab="Y", xlim=c(0,1), ylim=c(0,1), cex.lab=2, cex.axis=2, cex.main=2, cex.sub=2, pch=20,family = "Times New Roman", col = df$Col)
But, I didn't get what I was looking for. I want a continuous color bar to be added with labels from min(Date) to max(Date).
library(ggplot2)
ggplot( df, aes(X,Y)) +
geom_point(shape=21, stroke=1, aes(fill=Date),color="grey",size=6) +
scale_fill_gradient(low = "white", high = "black", labels=function(x)as.Date(x, origin="1970-01-01") )
I was surprised as to how clunky it appears to be with grey scale color gradients for Dates.