This QUESTION posted 3 years ago is very similar to mine and was lucky enough that it did solve my problem. However, I have a follow-up question regarding the format of the legend's label. I would like to make it into MMYYYY
. I know I can do a format = "%b %Y"
but I am not sure which argument to place it. Thanks!
Asked
Active
Viewed 976 times
0

AVANISH RAJBHAR
- 527
- 3
- 9

mand3rd
- 383
- 1
- 12
-
Have you try: `scale_x_date(date_labels = "%b %Y")` ? If it is not working, please provide a reproducible example of your dataset: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – dc37 Feb 25 '20 at 05:55
-
@dc37 Thanks for your reply. Date is not an element of the x-axis. The link provided has the reproducible example. – mand3rd Feb 25 '20 at 06:15
1 Answers
3
Posting the answer with reference to the other question.
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)
this was an answer in the other question and if you want to format the date as MMYYYY then,
Date_F <- function(x){
format(as.Date(x, origin = '1970-01-01',"%Y-%m-%d"),"%m%Y")
}
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=Date_F)
Hope this helps!

Ashish
- 337
- 3
- 11