使用ggplot2中的scale_x_date格式化日期 [英] Formatting dates with scale_x_date in ggplot2
问题描述
在以前的ggplot2版本中,我可以使用以下两个命令之一来格式化我的x日期:
scale_x_date(breaks =1个月,minor_breaks =1周,labels =(date_format =%B))+
或
scale_x_date(major =months,minor =weeks ,format =%B)+
生成%B格式,全月
(恐怕我不能再区分哪一个工作,因为他们都被注释掉了。)
$ b $我不记得什么时候,但是在ubuntu 12.04升级中更新了R或ggplot之后,这不再适用于我。现在,相同的数据产生错误:
scale_labels.continuous(scale)中的错误:
中断和标签不同的长度
第一个和
continuous_scale中的错误(美观,日期,身份,休息=休息,
未使用参数(major =months,minor =weeks格式=%B)
第二个。
如果我删除labels =参数,并应用
scale_x_date(breaks =1个月,minor_breaks = 1周)+
它生成日期格式为YYYY-MM-DD
咨询帮助功能?scale_x_date,我还尝试过以下操作:
scale_x_date(breaks =1个月,minor_breaks =1周,labels = date_format(%B))+
/ pre>
但是这会产生这个错误:
结构错误(list(call = match.call(),aesthetics = aesthetics,
/ pre>
找不到函数date_format
如何在我的x轴上实现月名%B格式? (如果您对产生这些错误消息的机制有其他见解,我也会感谢。)
解决方案新的ggplot2 v 2.0.0,一种方法是:
scale_x_date(date_breaks =1个月,date_minor_breaks = 1周,date_labels =%B)
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.)
解决方案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")
这篇关于使用ggplot2中的scale_x_date格式化日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!