删除时间序列之间的链接,并在ggplot中的x_axis上添加次要日期刻度

时间:2020-11-01 12:22:09

标签: r date ggplot2

我试图绘制一个由每周一次的间隔组成的时间序列。这是我获得的剧情:

[每周平均值A] [1]:https://i.stack.imgur.com/XMGMs.png

如您所见,时间序列并不能完全涵盖所有年份,因此,当我没有数据时,ggplot会链接随后的两年。我认为我必须以某种方式对数据进行分组,但是我不知道如何。这是代码:

df4 <- data.frame(df$Date, df$A)
colnames(df4)<- c("date","A")
df4$date <- as.Date(df4$date,"%Y/%m/%d")
df4$week_day <- as.numeric(format(df4$date, format='%w'))
df4$endofweek <- df4$date + (6 - df4$week_day)

week_aveA <- df4 %>%
  group_by(endofweek) %>%
  summarise_all(list(mean=mean), na.rm=TRUE) %>%
  na.omit()

g1 = ggplot() + 
  geom_step(data=week_aveA, aes(group = 1, x = (endofweek), y = (A_mean)), colour="gray25") +
  scale_y_continuous(expand = c(0, 0), limits = c(0, 2500))  +
  scale_x_date(breaks="year", labels=date_format("%Y")) +
  labs(y = expression(A~ ~index), 
       x = NULL) +
  theme(axis.text.x = element_text(size=10), 
        axis.title = element_text(size=10))

以下是数据集的提取结果(前三年):

    endofweek   date_mean   A_mean  week_day_mean
1   20/03/2010  17/03/2010  939,2533437 3
2   27/03/2010  24/03/2010  867,3620121 3
3   03/04/2010  31/03/2010  1426,791222 3
4   10/04/2010  07/04/2010  358,5698314 3
5   17/04/2010  13/04/2010  301,1815352 2
6   24/04/2010  21/04/2010  273,4922895 3,333333333
7   01/05/2010  28/04/2010  128,5989633 3
8   08/05/2010  05/05/2010  447,8858881 3
9   15/05/2010  12/05/2010  387,9828891 3
10  22/05/2010  19/05/2010  138,0770986 3
11  29/05/2010  26/05/2010  370,2147933 3
12  05/06/2010  02/06/2010  139,0451791 3
13  12/06/2010  09/06/2010  217,1286356 3
14  19/06/2010  16/06/2010  72,36972411 3
15  26/06/2010  23/06/2010  282,2911902 3
16  03/07/2010  30/06/2010  324,3215936 3
17  10/07/2010  07/07/2010  210,568691  3
18  17/07/2010  14/07/2010  91,76930829 3
19  24/07/2010  21/07/2010  36,4211218  3,666666667
20  31/07/2010  28/07/2010  37,53981103 3
21  07/08/2010  04/08/2010  91,33282642 3
22  14/08/2010  11/08/2010  28,38587352 3
23  21/08/2010  18/08/2010  58,72836406 3
24  28/08/2010  24/08/2010  102,1050612 2,5
25  04/09/2010  02/09/2010  13,45357513 4,5
26  11/09/2010  08/09/2010  51,24017212 3
27  18/09/2010  15/09/2010  159,7395663 3
28  25/09/2010  21/09/2010  62,71136678 2
29  02/04/2011  31/03/2011  1484,661164 4
30  09/04/2011  06/04/2011  656,1827964 3
31  16/04/2011  13/04/2011  315,3097313 3
32  23/04/2011  20/04/2011  293,2904042 3
33  30/04/2011  26/04/2011  255,7517519 2,4
34  07/05/2011  04/05/2011  360,7035289 3
35  14/05/2011  11/05/2011  342,0902797 3
36  21/05/2011  18/05/2011  386,1380421 3
37  28/05/2011  24/05/2011  418,9624807 2,833333333
38  04/06/2011  01/06/2011  112,7568    3
39  11/06/2011  08/06/2011  85,17855619 3,2
40  18/06/2011  15/06/2011  351,8714638 3
41  25/06/2011  22/06/2011  139,7936898 3
42  02/07/2011  29/06/2011  68,57716191 3,6
43  09/07/2011  06/07/2011  62,31823822 3
44  16/07/2011  13/07/2011  80,7328917  3
45  23/07/2011  20/07/2011  114,9475331 3
46  30/07/2011  27/07/2011  90,13118758 3
47  06/08/2011  03/08/2011  43,29372258 3
48  13/08/2011  10/08/2011  49,39935204 3
49  20/08/2011  16/08/2011  133,746822  2
50  03/09/2011  31/08/2011  76,03928942 3
51  10/09/2011  05/09/2011  27,99834637 1
52  24/03/2012  23/03/2012  366,2625797 5,5
53  31/03/2012  28/03/2012  878,8535513 3
54  07/04/2012  04/04/2012  1029,909052 3
55  14/04/2012  11/04/2012  892,9163416 3
56  21/04/2012  18/04/2012  534,8278693 3
57  28/04/2012  25/04/2012  255,1177585 3
58  05/05/2012  02/05/2012  564,5280546 3
59  12/05/2012  09/05/2012  767,5018168 3
60  19/05/2012  16/05/2012  516,2680148 3
61  26/05/2012  23/05/2012  241,2113073 3
62  02/06/2012  30/05/2012  863,6123397 3
63  09/06/2012  06/06/2012  201,2019288 3
64  16/06/2012  13/06/2012  222,9955486 3
65  23/06/2012  20/06/2012  91,14166632 3
66  30/06/2012  27/06/2012  26,93145693 3
67  07/07/2012  04/07/2012  67,32183278 3
68  14/07/2012  11/07/2012  46,25297513 3
69  21/07/2012  18/07/2012  81,34359825 3,666666667
70  28/07/2012  25/07/2012  49,59130851 3
71  04/08/2012  01/08/2012  44,13438077 3
72  11/08/2012  08/08/2012  30,15773151 3
73  18/08/2012  15/08/2012  57,47256772 3
74  25/08/2012  22/08/2012  31,9109555  3
75  01/09/2012  29/08/2012  52,71058484 3
76  08/09/2012  04/09/2012  24,52495229 2
77  06/04/2013  01/04/2013  1344,388042 1,5
78  13/04/2013  10/04/2013  1304,838687 3
79  20/04/2013  17/04/2013  892,620141  3
80  27/04/2013  24/04/2013  400,1720434 3
81  04/05/2013  01/05/2013  424,8473083 3
82  11/05/2013  08/05/2013  269,2380208 3
83  18/05/2013  15/05/2013  238,9993749 3
84  25/05/2013  22/05/2013  128,4096151 3
85  01/06/2013  29/05/2013  158,5576121 3
86  08/06/2013  05/06/2013  175,2036942 3
87  15/06/2013  12/06/2013  79,20250839 3
88  22/06/2013  19/06/2013  126,9065428 3
89  29/06/2013  26/06/2013  133,7480108 3
90  06/07/2013  03/07/2013  218,0092943 3
91  13/07/2013  10/07/2013  54,08460936 3
92  20/07/2013  17/07/2013  91,54285041 3
93  27/07/2013  24/07/2013  44,64567928 3
94  03/08/2013  31/07/2013  229,5067999 3
95  10/08/2013  07/08/2013  49,70729373 3
96  17/08/2013  14/08/2013  53,38618335 3
97  24/08/2013  21/08/2013  217,2800997 3
98  31/08/2013  28/08/2013  49,43590136 3
99  07/09/2013  04/09/2013  64,88783029 3
100 14/09/2013  11/09/2013  11,04300773 3

因此,最后我主要提出一个问题:如何消除岁月之间的联系? ...和一个美学问题:如何在x_axis上添加较小的刻度线?每6个月至少一次,只是为了使情节易于阅读。 预先感谢您的任何建议!

修改

这是我尝试过的建议代码,也许我输错了一部分。

library(tidyverse)
library(dplyr) 
library(lubridate)

df4 <- data.frame(df$Date, df$A)
colnames(df4)<- c("date","A")
df4$date <- as.Date(df4$date,"%Y/%m/%d")
df4$week_day <- as.numeric(format(df4$date, format='%w'))
df4$endofweek <- df4$date + (6 - df4$week_day)

week_aveA <- df4 %>%
  group_by(endofweek) %>%
  summarise_all(list(mean=mean), na.rm=TRUE) %>%
  na.omit()

week_aveA$endofweek <- as.Date(week_aveA$endofweek,"%d/%m/%Y")
week_aveA$A_mean <- as.numeric(gsub(",", ".", week_aveA$A_mean))
week_aveA$week_day_mean <- as.numeric(gsub(",", ".", week_aveA$week_day_mean))
week_aveA$year <- format(week_aveA$endofweek, "%Y")

library(ggplot2)
library(methods)
library(scales)

mylabel <- function(x) {
  ifelse(grepl("-07-01$", x), "", format(x, "%Y"))
}

ggplot() + 
  geom_step(data=week_aveA, aes(x = endofweek, y = A_mean, group = year), colour="gray25") +
  scale_y_continuous(expand = c(0, 0), limits = c(0, 2500))  +
  scale_x_date(breaks="6 month", labels = mylabel) +
  labs(y = expression(A~ ~index),
       x = NULL) +
  theme(axis.text.x = element_text(size=10), 
        axis.title = element_text(size=10))

1 个答案:

答案 0 :(得分:0)

您必须按year分组:

  1. 将带有year的变量添加到数据集中
  2. 将年份变量映射到group美学上
  3. 关于滴答声。增加休息的次数。如果您只想要刻度线而不想要标签,则可以使用自定义功能来摆脱不需要的标签,例如我在下面的方法将休息时间设置为“ 6个月”,但用一个空字符串替换了年中标签:
    week_aveA$endofweek <- as.Date(week_aveA$endofweek,"%d/%m/%Y")
    week_aveA$A_mean <- as.numeric(gsub(",", ".", week_aveA$A_mean))
    week_aveA$week_day_mean <- as.numeric(gsub(",", ".", week_aveA$week_day_mean))
    week_aveA$year <- format(week_aveA$endofweek, "%Y")

    library(ggplot2)
    
    mylabel <- function(x) {
      ifelse(grepl("-07-01$", x), "", format(x, "%Y"))
    }
    ggplot() + 
      geom_step(data=week_aveA, aes(x = endofweek, y = A_mean, group = year), colour="gray25") +
      scale_y_continuous(expand = c(0, 0), limits = c(0, 2500))  +
      scale_x_date(breaks="6 month", labels = mylabel) +
      labs(y = expression(A~ ~index),
           x = NULL) +
      theme(axis.text.x = element_text(size=10), 
            axis.title = element_text(size=10))

enter image description here