Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
570 views
in Technique[技术] by (71.8m points)

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

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...