【问题标题】:date_breaks {scales} shifts date scale in ggplotdate_breaks {scales} 改变 ggplot 中的日期比例
【发布时间】:2015-10-26 14:32:19
【问题描述】:

如下面的示例数据集所示,日期介于 2015-01-01 和 2015-08-01 之间。但是,当我使用 date_breaks 函数时,x 轴上的月份标签会发生偏移。您能提出解决方案吗?

dfn <- read.table(header=T, text='
supp p_date length
OJ  2015-01-01  13.23
OJ  2015-03-01  22.70
OJ  2015-08-01  26.06
VC  2015-01-01   7.98
VC  2015-03-01  16.77
VC  2015-08-01  26.14
                  ')

dfn$p_date <- as.Date(dfn$p_date, "%Y-%m-%d")

library(ggplot2)
library(scales)
ggplot(dfn, aes(as.POSIXct(p_date), length, colour = factor(supp))) + 
    geom_line(size=1.3) +
    labs(colour="Lines :", x = "", y = "") +
    guides(colour = guide_legend(override.aes = list(size=5))) +  
    scale_x_datetime(breaks = date_breaks("1 months"),labels = date_format("%m/%y"))

这是我的会话信息

> sessionInfo()
R version 3.2.2 (2015-08-14)
Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

locale:
[1] LC_COLLATE=Greek_Greece.1253  LC_CTYPE=Greek_Greece.1253    LC_MONETARY=Greek_Greece.1253 LC_NUMERIC=C                  LC_TIME=Greek_Greece.1253    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] scales_0.3.0  ggplot2_1.0.1

loaded via a namespace (and not attached):
 [1] labeling_0.3     MASS_7.3-44      colorspace_1.2-6 magrittr_1.5     plyr_1.8.3       tools_3.2.2      gtable_0.1.2     reshape2_1.4.1  
 [9] Rcpp_0.12.1      stringi_0.5-5    grid_3.2.2       stringr_1.0.0    digest_0.6.8     proto_0.3-10     munsell_0.4.2  

【问题讨论】:

  • 如果我复制/粘贴你的代码,那不是我得到的情节。你确定这是你看到的输出吗?
  • 是的,我是......(我编辑包括库)
  • as.POSIXct(dfn$p_date) 为您返回什么?
  • [1] "2015-01-01 02:00:00 EET" "2015-03-01 02:00:00 EET" [3] "2015-08-01 03:00: 00 EEST" "2015-01-01 02:00:00 EET" [5] "2015-03-01 02:00:00 EET" "2015-08-01 03:00:00 EEST"
  • 似乎更改为 date_format("%m/%Y", tz="EET") 提供了正确的标签,但我不知道它会有多强大(GMT 和 BST 仍然显示出意外的(对我而言)结果)

标签: r datetime ggplot2


【解决方案1】:

我注意到日期格式存在类似(但不相同)的问题,这似乎是由于在使用format 时使用了时区date_format()

你的基本情节

library(ggplot2)
library(scales)

p <- ggplot(dfn, aes(as.POSIXct(p_date), length, colour = factor(supp))) + 
          geom_point(size=3) +
          geom_line(size=1.3) +
          labs(colour="Lines :", x = "", y = "") +
          guides(colour = guide_legend(override.aes = list(size=5))) +
          theme(axis.text.x=element_text(size=15))

p + scale_x_datetime(breaks = date_breaks("1 month"), 
                                           labels = date_format("%m/%Y"))

哪个产生

请注意,已添加了两次 March,这似乎是由于在 format 函数中使用了时区信息

date_format()(date_breaks()(as.POSIXct(dfn$p_date[c(1,3)])))
#[1] "2015-01-01" "2015-02-01" "2015-03-01" "2015-03-31" "2015-04-30" "2015-05-31"
#[7] "2015-06-30" "2015-07-31" "2015-08-31"

您可以编写自己的格式函数,使其不包含任何时区信息。

my_format <- function (format = "%Y-%m-%d") {
                        function(x) format(x, format)
                 }

还有情节

p + scale_x_datetime(breaks = date_breaks("1 month"), 
                                           labels = my_format("%m/%Y"))

这种格式的行为更像我的预期

my_format()(date_breaks()(as.POSIXct(dfn$date2[c(1,3)])))
#[1] "2015-01-01" "2015-02-01" "2015-03-01" "2015-04-01" "2015-05-01" "2015-06-01"
#[7] "2015-07-01" "2015-08-01" "2015-09-01"

所以并不是真正的解释,因为我对 POSIX 函数的了解很少,但确实建议解决方法。

> sessionInfo()
R version 3.2.2 (2015-08-14)
Platform: i686-pc-linux-gnu (32-bit)
Running under: Ubuntu 14.04.3 LTS

locale:
 [1] LC_CTYPE=en_GB.UTF-8       LC_NUMERIC=C               LC_TIME=en_GB.UTF-8       
 [4] LC_COLLATE=en_GB.UTF-8     LC_MONETARY=en_GB.UTF-8    LC_MESSAGES=en_GB.UTF-8   
 [7] LC_PAPER=en_GB.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
[10] LC_TELEPHONE=C             LC_MEASUREMENT=en_GB.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] datasets  utils     stats     graphics  grDevices grid      methods   base     

other attached packages:
[1] scales_0.3.0       data.table_1.9.6   Hmisc_3.17-0       Formula_1.2-1      survival_2.38-3   
[6] lattice_0.20-33    MASS_7.3-44        gridExtra_2.1.0    ggplot2_1.0.1.9003

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.1         digest_0.6.8        chron_2.3-47        plyr_1.8.3          gtable_0.1.2       
 [6] acepack_1.3-3.3     latticeExtra_0.6-26 rpart_4.1-10        labeling_0.3        proto_0.3-10       
[11] splines_3.2.2       RColorBrewer_1.1-2  tools_3.2.2         foreign_0.8-66      munsell_0.4.2      
[16] colorspace_1.2-6    cluster_2.0.3       nnet_7.3-11  

【讨论】:

  • 我认为您对时区问题的看法是正确的。在我发现时区问题之前,我在图表数据中有类似的奇怪时间变化。用法是date_format(format = "%Y-%m-%d", tz = "UTC"),因此除非您覆盖 TZ,否则日期将始终以 UTC 时区显示。
【解决方案2】:

只需使用scale_x_date 而不是scale_x_datetime

ggplot(dfn, aes(p_date, length, colour = factor(supp))) + 
  geom_line(size=1.3) +
  labs(colour="Lines :", x = "", y = "") +
  guides(colour = guide_legend(override.aes = list(size=5))) +  
  scale_x_date(breaks = date_breaks("1 months"),labels = date_format("%m/%y"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多