本文介绍了ggplot2中scale_x_datetime的意外行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下每日收集数据的数据集。

  R> toydata 
日期组系数
1 2011-11-04 23:59:59 1 2.32
2 2011-11-05 23:59:59 1 2.34
3 2011-11 -06 23:59:59 1 2.46
4 2011-11-07 23:59:59 1 2.68
5 2011-11-04 23:59:59 2 2.17
6 2011- 11-05 23:59:59 2 1.90
7 2011-11-06 23:59:59 2 2.13
8 2011-11-07 23:59:59 2 2.52

所有东西似乎都可以正常工作:

  R> toydata $ date 
[1]2011-11-04 23:59:59 EST2011-11-05 23:59:59 EST
[3]2011-11-06 23 :59:59 EST2011-11-07 23:59:59 EST
[5]2011-11-04 23:59:59 EST2011-11-05 23:59:59 EST
[7]2011-11-06 23:59:59 EST2011-11-07 23:59:59 EST



  R>格式(toydata $ date,%d-%b)
[1]04-Nov05-Nov06-Nov07-Nov04-Nov05-Nov 06-Nov07-Nov

但是,当我尝试使用ggplot2使用以下代码

  R> p  R> pq  + scale_x_datetime(major =1 day,format =%d-%b)



我收到了意想不到的结果:



正如你所看到的,11月6日是重复的。我可以通过将x中的标签视为字符串而不是日期来解决此问题,但我很好奇为什么会发生这种情况。我错过了明显的东西吗?

解决方案

只需指定 tz ='EST' 。无论默认情况是重复11月6日,因为夏令时开关。


I have the following dataset with daily collected data.

R> toydata
                 date group coef
1 2011-11-04 23:59:59     1 2.32
2 2011-11-05 23:59:59     1 2.34
3 2011-11-06 23:59:59     1 2.46
4 2011-11-07 23:59:59     1 2.68
5 2011-11-04 23:59:59     2 2.17
6 2011-11-05 23:59:59     2 1.90
7 2011-11-06 23:59:59     2 2.13
8 2011-11-07 23:59:59     2 2.52

Everythings seems to work fine:

R> toydata$date
[1] "2011-11-04 23:59:59 EST" "2011-11-05 23:59:59 EST"
[3] "2011-11-06 23:59:59 EST" "2011-11-07 23:59:59 EST"
[5] "2011-11-04 23:59:59 EST" "2011-11-05 23:59:59 EST"
[7] "2011-11-06 23:59:59 EST" "2011-11-07 23:59:59 EST"

and

R> format(toydata$date, "%d-%b")
[1] "04-Nov" "05-Nov" "06-Nov" "07-Nov" "04-Nov" "05-Nov" "06-Nov" "07-Nov"

However, when I try to plot it using ggplot2 using the following code

R> p <- ggplot(toydata, aes(x = date, y = coef, group = group))
R> pq <- p + geom_line(aes(colour = group)) +
+   scale_x_datetime(major = "1 day", format = "%d-%b")

I get an unexpected result:

As you can see, November, 6 is duplicated. I can get around this problem by just treating the labels in x as strings and not as dates, but I am curious about why this is happening. Am I missing something obvious?

解决方案

Just specify tz='EST' and it works for me. Whatever the default is is repeating Nov 6 because of the daylight savings time switch.

这篇关于ggplot2中scale_x_datetime的意外行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 04:34