【问题标题】:Months displayed incorrectly when using ggplot2使用 ggplot2 时月份显示不正确
【发布时间】:2016-09-26 12:07:13
【问题描述】:

您好,我遇到了一个问题,即 March 在我的图表中出现了两次,但在我的数据中却没有出现。

我的数据看起来像。我的数据框叫做 try1。

    Month                 Year    tcol
   2016-01-01 00:00:00    06      1461.0
   2016-02-01 00:00:00    06      259.5
   2016-03-01 00:00:00    06      191.2
   2016-04-01 01:00:00    06      151.5
   2016-05-01 01:00:00    06      119.6
   2016-06-01 01:00:00    06      1372.5
   2016-07-01 01:00:00    06      954.0
   2016-08-01 01:00:00    06      1784.0
   2016-09-01 01:00:00    06      1369.0
   2016-10-01 01:00:00    06      6077.0
   2016-11-01 00:00:00    06      1638.0
   2016-12-01 00:00:00    06      3308.0

我的代码看起来像。

ggplot(try1, aes(Month,tcol)) + 
       geom_point(aes(colour = Year),size=2) + 
       geom_line(aes(colour = Year), size=0.73)+
       theme_bw()+
       guides(col = guide_legend(ncol = 2))+
       scale_x_datetime(
                        breaks=date_breaks("1 months"), 
                        labels=date_format("%B"))+
       xlab("")+ #x axis label 
       ylab("Total Coliforms")

问题是当我绘制图表时,March 出现了两次。十月似乎被排除在外。

The resulting graph

感谢您的帮助。

【问题讨论】:

  • 月份列是什么数据类型?因素还是日期?
  • 我无法重现该问题。您能否运行dput(try1) 并将结果复制到您的问题中。这样我们就可以使用相同类别的对象了。
  • @9Heads,您的月份标签已关闭 1 个月;它们从 12 月开始,到 11 月结束)。不太确定你是如何做到的。 :)
  • @Benjamin 这是默认时区而不是我的系统时区的错误。更正后Link of the plot

标签: r datetime ggplot2 formatting


【解决方案1】:

我怀疑这是时区问题。例如,有了这些数据

structure(list(Month = structure(list(sec = c(0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0), min = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L), hour = c(0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 0L, 0L), mday = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L), mon = 0:11, year = c(116L, 116L, 116L, 116L, 116L, 116L, 
116L, 116L, 116L, 116L, 116L, 116L), wday = c(5L, 1L, 2L, 5L, 
0L, 3L, 5L, 1L, 4L, 6L, 2L, 4L), yday = c(0L, 31L, 60L, 91L, 
121L, 152L, 182L, 213L, 244L, 274L, 305L, 335L), isdst = c(0L, 
0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 0L), zone = c("GMT", 
"GMT", "GMT", "BST", "BST", "BST", "BST", "BST", "BST", "BST", 
"GMT", "GMT"), gmtoff = c(NA_integer_, NA_integer_, NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_)), .Names = c("sec", 
"min", "hour", "mday", "mon", "year", "wday", "yday", "isdst", 
"zone", "gmtoff"), class = c("POSIXlt", "POSIXt"), tzone = c("Europe/London", 
"GMT", "BST")), Year = c(6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L), tcol = c(1461, 259.5, 191.2, 151.5, 119.6, 1372.5, 
954, 1784, 1369, 6077, 1638, 3308)), .Names = c("Month", "Year", 
"tcol"), row.names = c(NA, -12L), class = "data.frame")

我可以复制你的图表。尝试更改时区

attr(try1$Month, "tzone") <- "UTC"

然后重新绘制。


更新。我想知道为什么将时区更改为“UTC”有效。事实证明,date_format() 采用默认为“UTC”的tz 参数。见?date_format。这意味着除了将Month 的时区更改为“UTC”之外,您还可以通过将date_format() 中的tz 参数更改为Month 的原始时区来解决您的问题,您可以通过以下方式进行检查attr(try1$Month, "tzone").

【讨论】:

  • 确实是时区问题。我用我的系统时区绘制了上面的数据并得到了正确的图表。 Link of the plot
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-22
  • 1970-01-01
  • 2013-06-10
  • 1970-01-01
  • 2015-10-30
  • 1970-01-01
  • 2021-12-02
相关资源
最近更新 更多