在 ggplot2 中使用 scale_x_date 格式化日期 [英] Formatting dates with scale_x_date in ggplot2

查看:39
本文介绍了在 ggplot2 中使用 scale_x_date 格式化日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以前版本的 ggplot2 中,我能够使用以下两个命令之一来格式化我的 x 日期:要么

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

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

生成%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.)

我不记得是什么时候了,但是在 ubuntu 12.04 升级中更新了 R 或 ggplot 之后,这对我不再有效.现在,同样的数据产生了错误:

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

与第一个,和

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

第二个.

如果我删除了labels=参数,然后应用

If I remove the labels= argument, and apply

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

它在每个月的第一天生成YYYY-MM-DD"的日期格式.

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

咨询函数 ?scale_x_date 的帮助,我还尝试了以下操作:

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

但这会产生此错误:

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

如何在我的 x 轴上实现月份名称%B"格式?(如果您对产生这些错误消息的机制有任何其他见解,我也将不胜感激.)

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

推荐答案

使用新的 ggplot2 v 2.0.0,一种方法是:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆