Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting dates with scale_x_date in ggplot2

In a previous version of ggplot2, I was able to use one of the two following commands to format my x dates: Either

scale_x_date(breaks = "1 month", minor_breaks = "1 week", labels=(date_format="%B")) + 

or

scale_x_date(major="months", minor="weeks", format="%B") + 

to produce "%B" format, of full month name.

(I'm afraid I can no longer distinguish which one worked, because they were both commented out.)

I don't recall when, but after updating either R or ggplot in an ubuntu 12.04 upgrade, this no longer worked for me. Now, the very same data produces the error:

Error in scale_labels.continuous(scale) :    Breaks and labels are different lengths 

With the first, and

Error in continuous_scale(aesthetics, "date", identity, breaks = breaks,  :    unused argument(s) (major = "months", minor = "weeks", format = "%B") 

With the second.

If I remove the labels= argument, and apply

scale_x_date(breaks = "1 month", minor_breaks = "1 week") + 

it produces a date format of "YYYY-MM-DD" on the first of each month.

Consulting with the help for function ?scale_x_date, I've also tried the following:

scale_x_date(breaks = "1 month", minor_breaks = "1 week", labels=date_format("%B")) + 

But this produces this error:

Error in structure(list(call = match.call(), aesthetics = aesthetics,  :    could not find function "date_format" 

How can I achieve month-name "%B" formatting on my x axis? (If you have any additional insights into the mechanics producing these error messages, I'd also appreciate it.)

like image 245
Mittenchops Avatar asked May 13 '12 23:05

Mittenchops


1 Answers

With the new ggplot2 v 2.0.0, a way to do it is :

scale_x_date(date_breaks = "1 month", date_minor_breaks = "1 week",              date_labels = "%B") 
like image 72
YCR Avatar answered Sep 23 '22 00:09

YCR