75

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.)

Mittenchops
  • 18,633
  • 33
  • 128
  • 246
  • 3
    There were many significant changes in ggplot 0.9.0. You will find the [transition guide](http://cloud.github.com/downloads/hadley/ggplot2/guide-col.pdf) very helpful in getting up to speed. – joran May 14 '12 at 00:07

2 Answers2

112

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")
dfrankow
  • 20,191
  • 41
  • 152
  • 214
YCR
  • 3,794
  • 3
  • 25
  • 29
  • 7
    My answer below is now out of date. This has the right params. – Mittenchops Dec 05 '16 at 01:38
  • Hi, I need to order months in a different way, like month.abb[c(7:12, 1:6)]. Any hint? ¡Thanks! – Rafael Apr 17 '20 at 13:37
  • Works good, but how to show only short names like : Jan, Feb, Mar, ... ? – Andrew Dec 10 '20 at 00:12
  • It is controlled by date_labels. Use the list in the strptime function to fit your requirements: https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/strptime. For abbreviate months, it is %b – YCR Dec 10 '20 at 14:00
69

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

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

And to include library(scales) as the documentation says.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Mittenchops
  • 18,633
  • 33
  • 128
  • 246
  • 3
    You should consider recognizing this as an complete answer and close this thread. – Eric Fail May 22 '12 at 04:03
  • 1
    Maybe updating the parameters to `date_break` and `date_minor_break` since the new `ggplot2` version... – drmariod Sep 27 '16 at 11:38
  • correct @drmariod, it seems the API has changed again and my answer is no longer valid (though the advice to use the version found is the documentation is still good!). I'll recognize YCR's answer as now correct, if I can revise... – Mittenchops Dec 05 '16 at 01:36