Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R date_在ggplot中打断{scales}移动日期刻度_R_Datetime_Ggplot2 - Fatal编程技术网

R date_在ggplot中打断{scales}移动日期刻度

R date_在ggplot中打断{scales}移动日期刻度,r,datetime,ggplot2,R,Datetime,Ggplot2,如以下样本数据集所示,日期介于2015-01-01和2015-08-01之间。但是,当我使用date\u 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

如以下样本数据集所示,日期介于2015-01-01和2015-08-01之间。但是,当我使用
date\u 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"))

只需使用
缩放x\u日期
而不是
缩放x\u日期时间

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

我注意到日期格式存在类似(但不完全相同)的问题,这似乎是由于在使用
date\u 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"))
您可以编写自己的格式函数,使其不包含任何时区信息

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

这种格式的行为更像我所期望的

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  

如果我复制/粘贴你的代码,那不是我得到的图。你确定这就是你看到的输出吗?是的,我是……(我编辑了以包括库)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 EET”“2015-01-01 02:00:00 EET”“2015-03-03-01 02:00:00 EET”似乎正在更改为日期格式(“%m/%Y”,tz=“EET”)
提供了正确的标签,但我不知道它会有多强大(GMT和BST仍然显示(对我来说)意外的结果)。我认为您在时区问题上是对的。在我发现timzone问题之前,我在图表数据中也有类似的奇怪的时间变化。用法是
date\u格式(format=“%Y-%m-%d”,tz=“UTC”)
,因此除非您覆盖tz,否则日期将始终显示在UTC时区中。
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"
> 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