相互绘制多个年度时间序列

时间:2018-08-25 20:03:18

标签: r ggplot2

我有一个数据框,其中包含31个站点的各种气候测量值的每日观测值,这是因素。每个站都有许多年的日常观测值,并且有效地,每个站都有唯一的记录年数和唯一的观测值。

例如数据,我将其细分为13个站,每个唯一水年观测一次。

    NAME                        DATE         PRCP calendar_year month   day water_year water_date
    <fct>                       <date>      <dbl> <fct>         <int> <int> <fct>      <date>    
102 FLORENCE 0.2 SSE, OR US     2007-12-05  0     2007             12     5 2007       2006-12-05
103 FLORENCE 0.2 SSE, OR US     2008-10-01  0     2008             10     1 2008       2007-10-01
104 FLORENCE 0.2 SSE, OR US     2009-12-16  0.9   2009             12    16 2009       2008-12-16
105 FLORENCE 0.2 SSE, OR US     2010-10-19  0     2010             10    19 2010       2009-10-19
106 FLORENCE 0.2 SSE, OR US     2012-07-10  0     2012              7    10 2012       2012-07-10
107 FLORENCE 0.5 NE, OR US      2007-12-12  0     2007             12    12 2007       2006-12-12
108 FLORENCE 0.5 NE, OR US      2008-01-01  0     2008              1     1 2008       2008-01-01
109 FLORENCE 0.6 E, OR US       2008-01-01  0     2008              1     1 2008       2008-01-01
110 FLORENCE 0.9 NW, OR US      2007-12-22  0.09  2007             12    22 2007       2006-12-22
111 FLORENCE 0.9 NW, OR US      2008-10-01  0     2008             10     1 2008       2007-10-01
112 FLORENCE 0.9 NW, OR US      2009-10-01  0.02  2009             10     1 2009       2008-10-01
113 FLORENCE 0.9 NW, OR US      2010-10-01  0.03  2010             10     1 2010       2009-10-01
114 FLORENCE 0.9 NW, OR US      2011-10-01  0.02  2011             10     1 2011       2010-10-01
115 FLORENCE 0.9 NW, OR US      2012-10-01  0     2012             10     1 2012       2011-10-01
116 FLORENCE 0.9 NW, OR US      2013-10-01  0.92  2013             10     1 2013       2012-10-01
117 FLORENCE 0.9 NW, OR US      2014-10-01  0.01  2014             10     1 2014       2013-10-01
118 FLORENCE 0.9 NW, OR US      2015-10-01  0     2015             10     1 2015       2014-10-01
119 FLORENCE 0.9 NW, OR US      2016-10-01  0.15  2016             10     1 2016       2015-10-01
120 FLORENCE 0.9 NW, OR US      2017-10-01  0.2   2017             10     1 2017       2016-10-01
121 FLORENCE 0.9 NW, OR US      2018-01-01  0     2018              1     1 2018       2018-01-01
122 FLORENCE 1.8 NW, OR US      2007-12-14  0     2007             12    14 2007       2006-12-14
123 FLORENCE 1.8 NW, OR US      2008-10-01  0     2008             10     1 2008       2007-10-01
124 FLORENCE 1.8 NW, OR US      2009-10-25  0     2009             10    25 2009       2008-10-25
125 FLORENCE 1.8 NW, OR US      2010-10-05  0.01  2010             10     5 2010       2009-10-05
126 FLORENCE 1.8 NW, OR US      2011-10-01  0.02  2011             10     1 2011       2010-10-01
127 FLORENCE 1.8 NW, OR US      2012-10-02  0     2012             10     2 2012       2011-10-02
128 FLORENCE 1.8 NW, OR US      2013-10-01  0.570 2013             10     1 2013       2012-10-01
129 FLORENCE 1.8 NW, OR US      2014-10-01  0.02  2014             10     1 2014       2013-10-01
130 FLORENCE 1.8 NW, OR US      2015-10-01  0.02  2015             10     1 2015       2014-10-01
131 FLORENCE 1.8 NW, OR US      2016-10-01  0.08  2016             10     1 2016       2015-10-01
132 FLORENCE 1.8 NW, OR US      2017-10-01  0.23  2017             10     1 2017       2016-10-01
133 FLORENCE 1.8 NW, OR US      2018-01-01  0.01  2018              1     1 2018       2018-01-01
134 FLORENCE 2.1 NNW, OR US     2007-12-17  0.96  2007             12    17 2007       2006-12-17
135 FLORENCE 2.1 NNW, OR US     2008-10-01  0     2008             10     1 2008       2007-10-01
136 FLORENCE 2.1 NNW, OR US     2009-10-01  0     2009             10     1 2009       2008-10-01
137 FLORENCE 2.1 NNW, OR US     2010-10-01  0.03  2010             10     1 2010       2009-10-01
138 FLORENCE 2.1 NNW, OR US     2011-10-01  0     2011             10     1 2011       2010-10-01
139 FLORENCE 2.1 NNW, OR US     2012-10-01  0     2012             10     1 2012       2011-10-01
140 FLORENCE 2.1 NNW, OR US     2013-12-26  0     2013             12    26 2013       2012-12-26
141 FLORENCE 2.1 NNW, OR US     2014-10-07  0     2014             10     7 2014       2013-10-07
142 FLORENCE 2.1 NNW, OR US     2016-05-21  0     2016              5    21 2016       2016-05-21
143 FLORENCE 2.1 NNW, OR US     2017-12-26  0     2017             12    26 2017       2016-12-26
144 FLORENCE 2.9 NNW, OR US     2007-12-16  0.07  2007             12    16 2007       2006-12-16
145 FLORENCE 2.9 NNW, OR US     2008-10-01  0     2008             10     1 2008       2007-10-01
146 FLORENCE 2.9 NNW, OR US     2009-10-01  0.03  2009             10     1 2009       2008-10-01
147 FLORENCE 2.9 NNW, OR US     2010-10-02  0.05  2010             10     2 2010       2009-10-02
148 FLORENCE 2.9 NNW, OR US     2011-10-01  0.02  2011             10     1 2011       2010-10-01
149 FLORENCE 2.9 NNW, OR US     2012-10-02  0     2012             10     2 2012       2011-10-02
150 FLORENCE 2.9 NNW, OR US     2013-10-01  0.580 2013             10     1 2013       2012-10-01
151 FLORENCE 2.9 NNW, OR US     2014-10-01  0.02  2014             10     1 2014       2013-10-01
152 FLORENCE 2.9 NNW, OR US     2015-10-01  0     2015             10     1 2015       2014-10-01
153 FLORENCE 2.9 NNW, OR US     2016-10-04  0.580 2016             10     4 2016       2015-10-04
154 FLORENCE 2.9 NNW, OR US     2017-10-01  0.2   2017             10     1 2017       2016-10-01
155 FLORENCE 2.9 NNW, OR US     2018-01-01  0     2018              1     1 2018       2018-01-01
156 FLORENCE 5.4 N, OR US       2007-12-22  0.03  2007             12    22 2007       2006-12-22
157 FLORENCE 5.4 N, OR US       2008-10-01  0     2008             10     1 2008       2007-10-01
158 FLORENCE 5.4 N, OR US       2009-10-01  0.07  2009             10     1 2009       2008-10-01
159 FLORENCE 5.4 N, OR US       2010-10-01  0.03  2010             10     1 2010       2009-10-01
160 FLORENCE 5.4 N, OR US       2011-10-03  0.65  2011             10     3 2011       2010-10-03
161 FLORENCE 5.4 N, OR US       2012-10-01  0     2012             10     1 2012       2011-10-01
162 FLORENCE 5.4 N, OR US       2013-10-01  0.6   2013             10     1 2013       2012-10-01
163 FLORENCE 5.4 N, OR US       2014-10-01  0     2014             10     1 2014       2013-10-01
164 FLORENCE 5.4 N, OR US       2015-10-01  0     2015             10     1 2015       2014-10-01
165 FLORENCE 5.4 N, OR US       2016-11-01  0.21  2016             11     1 2016       2015-11-01
166 FLORENCE 5.4 N, OR US       2017-11-11  0.9   2017             11    11 2017       2016-11-11
167 FLORENCE 5.4 N, OR US       2018-01-01  0     2018              1     1 2018       2018-01-01
168 FLORENCE 5.4 S, OR US       2007-12-08  0.42  2007             12     8 2007       2006-12-08
169 FLORENCE 5.4 S, OR US       2008-10-01  0     2008             10     1 2008       2007-10-01
170 FLORENCE 5.4 S, OR US       2009-10-01  0     2009             10     1 2009       2008-10-01
171 FLORENCE 5.4 S, OR US       2010-10-01  0.03  2010             10     1 2010       2009-10-01
172 FLORENCE 5.4 S, OR US       2011-10-01  0     2011             10     1 2011       2010-10-01
173 FLORENCE 5.4 S, OR US       2012-10-01  0     2012             10     1 2012       2011-10-01
174 FLORENCE 5.4 S, OR US       2013-10-01  0.6   2013             10     1 2013       2012-10-01
175 FLORENCE 5.4 S, OR US       2014-10-02  0     2014             10     2 2014       2013-10-02
176 FLORENCE 5.4 S, OR US       2015-01-01  0     2015              1     1 2015       2015-01-01
177 FLORENCE 5.8 S, OR US       2007-12-01  0.02  2007             12     1 2007       2006-12-01
178 FLORENCE 5.8 S, OR US       2008-10-01  0     2008             10     1 2008       2007-10-01
179 FLORENCE 5.8 S, OR US       2009-10-01  0.02  2009             10     1 2009       2008-10-01
180 FLORENCE 5.8 S, OR US       2010-10-01  0.01  2010             10     1 2010       2009-10-01
181 FLORENCE 5.8 S, OR US       2011-10-01  0     2011             10     1 2011       2010-10-01
182 FLORENCE 5.8 S, OR US       2012-10-01  0     2012             10     1 2012       2011-10-01
183 FLORENCE 5.8 S, OR US       2013-10-01  0.75  2013             10     1 2013       2012-10-01
184 FLORENCE 5.8 S, OR US       2014-01-01  0     2014              1     1 2014       2014-01-01
185 FLORENCE 5.9 NNE, OR US     2007-11-29  0.41  2007             11    29 2007       2006-11-29
186 FLORENCE 5.9 NNE, OR US     2008-10-03  0.39  2008             10     3 2008       2007-10-03
187 FLORENCE 5.9 NNE, OR US     2009-10-01  0.01  2009             10     1 2009       2008-10-01
188 FLORENCE 5.9 NNE, OR US     2010-10-01  0.05  2010             10     1 2010       2009-10-01
189 FLORENCE 5.9 NNE, OR US     2011-10-01  0.02  2011             10     1 2011       2010-10-01
190 FLORENCE 5.9 NNE, OR US     2012-10-01  0     2012             10     1 2012       2011-10-01
191 FLORENCE 5.9 NNE, OR US     2013-10-01  0.43  2013             10     1 2013       2012-10-01
192 FLORENCE 5.9 NNE, OR US     2014-10-01  0     2014             10     1 2014       2013-10-01
193 FLORENCE 5.9 NNE, OR US     2015-10-10  0.69  2015             10    10 2015       2014-10-10
194 FLORENCE 5.9 NNE, OR US     2016-10-01  0.11  2016             10     1 2016       2015-10-01
195 FLORENCE 5.9 NNE, OR US     2017-01-01  0.24  2017              1     1 2017       2017-01-01
196 FLORENCE 6 N, OR US         2007-11-19  0.04  2007             11    19 2007       2006-11-19
197 FLORENCE 6 N, OR US         2008-10-01  0     2008             10     1 2008       2007-10-01
198 FLORENCE 6 N, OR US         2009-10-01  0     2009             10     1 2009       2008-10-01
199 FLORENCE 6 N, OR US         2010-01-01  0.7   2010              1     1 2010       2010-01-01
200 FLORENCE NUMBER 2, OR US    2006-10-01  0     2006             10     1 2006       2005-10-01
201 FLORENCE NUMBER 2, OR US    2007-10-01  0     2007             10     1 2007       2006-10-01
202 FLORENCE NUMBER 2, OR US    2008-10-01  0     2008             10     1 2008       2007-10-01
203 FLORENCE NUMBER 2, OR US    2009-10-01  0     2009             10     1 2009       2008-10-01
204 FLORENCE NUMBER 2, OR US    2010-10-01  0.04  2010             10     1 2010       2009-10-01
205 FLORENCE NUMBER 2, OR US    2011-10-01  0.9   2011             10     1 2011       2010-10-01
206 FLORENCE NUMBER 2, OR US    2012-10-01  0     2012             10     1 2012       2011-10-01
207 FLORENCE NUMBER 2, OR US    2013-10-01  0.46  2013             10     1 2013       2012-10-01
208 FLORENCE NUMBER 2, OR US    2014-10-01  0     2014             10     1 2014       2013-10-01
209 FLORENCE NUMBER 2, OR US    2015-10-01  0     2015             10     1 2015       2014-10-01
210 FLORENCE NUMBER 2, OR US    2016-10-01  0.77  2016             10     1 2016       2015-10-01
211 FLORENCE NUMBER 2, OR US    2017-10-01  0.06  2017             10     1 2017       2016-10-01
212 FLORENCE NUMBER 2, OR US    2018-01-01  0     2018              1     1 2018       2018-01-01
213 FLORENCE, OR US             1909-10-01  0.580 1909             10     1 1909       1908-10-01
214 FLORENCE, OR US             1910-10-01  0.49  1910             10     1 1910       1909-10-01
215 FLORENCE, OR US             1911-10-01  0.03  1911             10     1 1911       1910-10-01
216 FLORENCE, OR US             1912-10-01  0.07  1912             10     1 1912       1911-10-01
217 FLORENCE, OR US             1913-10-01  0     1913             10     1 1913       1912-10-01
218 FLORENCE, OR US             1914-10-01  0.24  1914             10     1 1914       1913-10-01
219 FLORENCE, OR US             1915-10-01  0.25  1915             10     1 1915       1914-10-01
220 FLORENCE, OR US             1916-10-01  0.03  1916             10     1 1916       1915-10-01
221 FLORENCE, OR US             1917-10-01  0     1917             10     1 1917       1916-10-01
222 FLORENCE, OR US             1918-10-01  0     1918             10     1 1918       1917-10-01
223 FLORENCE, OR US             1919-10-01  0.6   1919             10     1 1919       1918-10-01
224 FLORENCE, OR US             1920-10-01  1.22  1920             10     1 1920       1919-10-01
225 FLORENCE, OR US             1921-10-01  0     1921             10     1 1921       1920-10-01
226 FLORENCE, OR US             1922-10-01  0.03  1922             10     1 1922       1921-10-01
227 FLORENCE, OR US             1949-12-08  0     1949             12     8 1949       1948-12-08
228 FLORENCE, OR US             1950-10-01  0     1950             10     1 1950       1949-10-01
229 FLORENCE, OR US             1951-01-01  0.32  1951              1     1 1951       1951-01-01
230 FLORENCE, OR US             2004-10-01  0     2004             10     1 2004       2003-10-01
231 FLORENCE, OR US             2005-10-01  0.88  2005             10     1 2005       2004-10-01
232 FLORENCE, OR US             2006-10-01  0     2006             10     1 2006       2005-10-01
233 FLORENCE, OR US             2007-10-01  0.33  2007             10     1 2007       2006-10-01
234 FLORENCE, OR US             2008-10-01  0     2008             10     1 2008       2007-10-01
235 FLORENCE, OR US             2009-10-01  0     2009             10     1 2009       2008-10-01
236 FLORENCE, OR US             2010-10-01  0.04  2010             10     1 2010       2009-10-01
237 FLORENCE, OR US             2011-10-01  0.75  2011             10     1 2011       2010-10-01
238 FLORENCE, OR US             2012-10-02  0     2012             10     2 2012       2011-10-02
239 FLORENCE, OR US             2013-10-01  0.63  2013             10     1 2013       2012-10-01
240 FLORENCE, OR US             2014-10-01  0     2014             10     1 2014       2013-10-01
241 FLORENCE, OR US             2015-10-01  0     2015             10     1 2015       2014-10-01
242 FLORENCE, OR US             2016-10-01  0.16  2016             10     1 2016       2015-10-01
243 FLORENCE, OR US             2017-01-01  0.53  2017              1     1 2017       2017-01-01

我的目标是:

  • 每个NAME
  • 每现存水年
  • 针对water_date创建所有PRCP值的点/平滑图
  • 按名称合并

因此,得出的图将是y轴上的PRCP,x轴上的water_date以及按每个water_year(可用于该NAME)分组的点/平滑点,它们彼此重叠。总共将有31个地块,每个名称一个。

对于给定名称的PRCP,针对每单个water_year的water_date绘制一个简单的方法是:

ggplot(srb_clean %>% filter(NAME == "made up name" & water_year == "1902") ,aes(water_date, PRCP)) +
  geom_point(na.rm=TRUE)  +
  geom_smooth(colour = "red",size = 1)

此代码将为一年的数据生成图表,而所需的输出将为该NAME的每一年数据提供一个点/平滑组。

enter image description here

我正在寻找一种方法来自动化创建这些图的每个过程,并使用PRCP x water_date(按water_year分组)为每个名称输出一个图。

做这样的R的最优雅或最标准的方法是什么?我是一名编程新手,并且对如何以编程方式解决此问题有些困惑,尤其是在R中。


UPDATE#1(改进的示例数据和问题)


更新#3(解决方案)

Parfait的解决方案效果很好。可以将其与类似于上面的代码一起使用,以输出类似于以下内容的图:

multiple water years overplot

1 个答案:

答案 0 :(得分:1)

由于您需要在一年中使用相同的x轴日期,因此请考虑将 water_date 中的所有年份更新为当前没有行保留的普通年份,例如2099-2100。

然后使用by(通过一个或多个因子将数据帧切成较小的子集的功能)为每个不同的 NAME 生成图的列表。要忽略 2099 ,请使用scales库来绘制月份和日期:%b-%d(月份名称)%m-%d(月份编号)。另外,将 water_year 作为图例系列的填充因子。

library(ggplot2)
library(scales)
...

# TEMP HELPER VARIABLE
df$wt_date_char <- as.character(df$water_date)

# REPLACE EVERY YEAR FOR 2099 OR 2100
# CONDITIONALLY UPDATE YEAR BY MONTH NUMEBR
df$pseudo_water_date <- ifelse(substr(df$wt_date_char, 6, 7) %in% paste0("0", as.character(seq(1,9))),
                               gsub("^(.*?)\\-", "2099-", df$wt_date_char),
                               gsub("^(.*?)\\-", "2100-", df$wt_date_char)
                        ) 

df$pseudo_water_date <- as.Date(df$pseudo_water_date, origin="1970-01-01")
df$wt_date_char <- NULL

# BUILD PLOT LIST
plot_list <- by(srb_clean, srb_clean$NAME, function(sub)
               ggplot(sub, aes(pseudo_water_date, PRCP, fill=factor(water_year))) +
                  geom_point(na.rm=TRUE)  + 
                  geom_smooth(colour = "red", size = 1) +
                  ggtitle(sub$NAME[[1]]) + 
                  labs(title="Water Year", x="Water Date", y="Precipitation") +
                  theme(plot.title = element_text(hjust = 0.5)) +
                  scale_x_date(labels = date_format("%b-%d"))
             ) 

# OUTPUT INDIVIDUAL PLOTS
plot_list[[1]]
plot_list[[2]]
plot_list[[3]]
...

# OUTPUT ALL PLOTS
plot_list