mand3rd
mand3rd

Reputation: 393

Format date in legend for ggplot in R

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!

Upvotes: 0

Views: 1364

Answers (1)

Ashish
Ashish

Reputation: 367

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!

Upvotes: 3

Related Questions