【发布时间】: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 仍然显示出意外的(对我而言)结果)