Formatting dates with scale_x_date in ggplot2

In a previous version of ggplot2, I was able to use one of the following two 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 create the format "% B", the full name of the month.

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

I donโ€™t remember when, but after updating R or ggplot in the ubuntu 12.04 update, this no longer worked for me. Now the same data causes an 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 label = arguments and apply

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

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

Counseling using a feature? scale_x_date, I also tried the following:

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

But this causes this error:

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

How do I get the month format "% B" on my x axis? (If you have more information about the mechanism that generates these error messages, I would also appreciate it.)

+36
date formatting r ggplot2
May 13 '12 at 23:43
source share
2 answers

With the new ggplot2 v 2.0.0 way to do this:

 scale_x_date(date_breaks = "1 month", date_minor_breaks = "1 week", date_labels = "%B") 
+26
Jan 04 '16 at 15:05
source share

Nevermind, the answer was to use the version contained in the documentation,

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

And include library(scales) as documentation .

+54
May 14 '12 at
source share



All Articles