-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01NO2detrend.R
207 lines (199 loc) · 8.37 KB
/
01NO2detrend.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
library(tidyverse)
library(lubridate)
library(readxl)
load("~/Documents/Luftqualitaet/Daten/BW_dat/Stations_data.RData")
# Overview of data
dir()
head(my_stations,1) # names data model
head(my_stations$names,3) #[1] "Odw.no2" "Sws.no2" "Alb.no2"
head(my_stations$data$Odw.no2,3) #Station datetime NO2 residuals
head(my_stations$model$Odw.no2,1) # $coefficients
# (Intercept) datetime
#1.639619e+01 -4.958935e-09
NROW(my_stations$model$Odw.no2$residuals) #91990 Rows Time difference 96422 hours
# 4432 missing datasets
my_stations$model$Odw.no2$fitted.values %>% head() # 1 2 3 4 5 6
#11.70162 11.70160 11.70159 11.70157 11.70155 11.70153
list_names <- my_stations$names
#=========================================================
## Grafische Darstellungen
plot.fun <- function( df) {df %>%
as_tibble() %>%
ggplot(aes(x = datetime,y = NO2))+
geom_point(size = 0.1,alpha = 0.1)+
geom_smooth(data = df, method = "auto",col = "red",span = 0.01)+
labs ( x = "", y = "NO2 [ug/m3]")+
ggtitle(paste("Station",df[1,1],nm),
subtitle = "Trend (Mehr- Jahresmittelwerte) rote Linie")
}
plots <- vector ("list",length = length(list_names))
dfr <- vector ("list",length = length(list_names))
names(plots) <- list_names
names(dfr) <- list_names
for (nm in list_names) {
dfr[[nm]] <- my_stations$data[[nm]]
plots[[nm]] <- plot.fun(dfr[[nm]])
}
plots %>% walk(print)
# Speichern im WD mit Namen "trend.png"
# Graph im WD/ Abbldg/ speichern
save.path <- paste0(getwd(),"/Abbldg/")
if(!dir.exists(save.path)){
dir.create(save.path)
}
for (nm in list_names){
ggsave(filename = paste0(save.path,nm,"trend.png"),plot = plots[[nm]])
}
#===============================================================
# Darstellung der Regressionsgeraden
nm
ggplot(my_stations$data[[nm]], aes (x = datetime, y = NO2)) +
geom_point(size = 0.1, alpha = 0.1)+
geom_abline( slope =my_stations$model[[nm]]$coefficients[2],
intercept = my_stations$model[[nm]]$coefficients[1],
col = "red")+
ggtitle( paste("NO2 (12 Jahres - Trend) ", my_stations$data[[nm]][1,1]),
subtitle = "Regressionsgerade rote Linie") +
labs( x = "year", y = "NO2 ")
plot.fun2 <- function( data,par) {
slp = par[[2]]
intcp <- par[[1]]
data %>%
ggplot(aes(x = datetime,y = NO2))+
geom_point(size = 0.1, alpha = 0.1 ,shape = 3)+
geom_abline( slope =slp,
intercept = intcp,
col = "red")+
labs ( x = "", y = "NO2 [ug/m3]")+
ggtitle(paste("Station",nm),
subtitle = "Regressionsgerade rote Linie")
}
reg.plots <- vector ("list",length = length(list_names))
parms <- vector ( "list", length = length(list_names))
names(reg.plots) <- list_names
names(parms) <- list_names
for(nm in list_names) {
dfr[[nm]] <- my_stations$data[[nm]]
parms[[nm]] <- my_stations$model[[nm]]$coefficients
reg.plots[[nm]] <- plot.fun2(dfr[[nm]],parms[[nm]])
}
reg.plots %>% walk(print)
# =====================================================================================
#====Vergleich der Jahreswerte
my_stations$data$Odw.no2 %>% head()
nm <- "Odw.no2"
my_stations.year <- vector("list", length= length(list_names))
names(my_stations.year) <- list_names
# Kalenderjahre als Faktor
for (nm in list_names) {
my_stations$data[[nm]]$fullyear <- my_stations$data[[nm]]$datetime %>%
floor_date("year") %>%
str_extract("^.{4}") %>% as.numeric()
}
# Jahreswerte als Mittelwerte
for (nm in list_names) {
my_stations.year[[nm]] <- my_stations$data[[nm]] %>%
group_by(fullyear) %>%
summarise(NO2.year.mean = mean(NO2), residual.year.mean = mean(residuals),n())
}
my_stations.year[[nm]] %>% head()
# Darstellung der Jahrewerte
reg_year.plot <- vector ( "list", length = length (list_names))
names(reg_year.plot) <- list_names
df <- vector("list", length = length(list_names))
names(df) <- list_names
for (nm in list_names) {
#df[[nm]] <-select(my_stations.year[[nm]],fullyear , NO2 = "NO2.year.mean")
reg_year.plot[[nm]]<- ggplot(my_stations.year[[nm]], aes (x = fullyear , y = NO2.year.mean) ) +
geom_point()+
geom_smooth(col = "red", se = FALSE)+
geom_smooth(method = "lm", col = "blue")+
labs ( x = "Kalenderjahr", y = "NO2 Jahresmittel")+
ggtitle(paste( nm, "Jahresmittel" ),
subtitle = "Trend und Regressionsgerade")
}
reg_year.plot %>% walk(print)
for (nm in list_names){
ggsave(filename = paste0(save.path,nm,"Jahrestrend.png"),plot = reg_year.plot[[nm]])
}
# Daten aus Internet sind unter Daten/BRD/NO2_Emissionen_BRD.xls abgelegt
emissions <- read_excel("~/Documents/Luftqualitaet/Daten/BRD/NO2_Emissionen_BRD.xls")
names(emissions)
# Darstllung der Jahrestonnen
ggplot(emissions, aes(Jahr,D_Gesamt))+
geom_smooth(aes(Jahr,D_Gesamt))+
geom_smooth(aes(Jahr,BWGesamt))+
geom_smooth(aes(x = Jahr, y = D_Verkehr),col = "red")+
geom_smooth(method = "lm",aes(x = Jahr, y = D_Verkehr),col = "red",linetype = 5 )+
geom_smooth(method = "lm",aes(x = Jahr, y = BWVerkehr),col = "red" ,linetype = 5 )+
labs( y = "NO2 [Tausend t]")+
ggtitle (" Entwicklung der NO2 Emissionen BRD und BW",
subtitle = " Gesamt (blau) Verkehr (rot)
gestrichelt = Regressionsgerade")
ggplot(emissions, aes(Jahr,BWGesamt))+
geom_smooth(aes(Jahr,BWGesamt))+
geom_smooth(aes(x = Jahr, y = BWVerkehr),col = "red" ,linetype = 5 )+
geom_smooth(aes(x = Jahr, y = BW_Diesel_Pkw),col = "black" ,linetype = 5)+
labs( y = "NO2 [Tausend t]")+
ggtitle (" Entwicklung der NO2 Emissionen BW",
subtitle = " Gesamt (blau)
Verkehr (rot)
Pkw Diesel gestrichelt (schwarz)")
# Auswahl der Variablen
selemissions <- emissions %>% dplyr::select(D_Gesamt,D_Verkehr,BWGesamt,BWVerkehr)
# Prozent Darstellung
proz <- function(vec) {vec/vec[1]*100}
selemissions.pr <- selemissions %>% map_dfr(proz)
selemissions.pr$Jahr <- emissions$Jahr
head(selemissions,1)
ggplot(selemissions.pr, aes(x = Jahr, y = D_Gesamt))+
geom_smooth()+
geom_smooth(aes(x=Jahr, y = D_Verkehr),col = "red")+
geom_smooth(aes(y= BWGesamt), col = "green")+
geom_smooth(aes(y = BWVerkehr), col ="purple")+
labs( y = " Prozent von 1990")+
ggtitle(" NO2 Gesamt- und Verkehrsemissionen
Relative Entwicklung 1990 bis 2017",
subtitle = " BRD: Gesamt (blau) Verkehr (rot)
BW: Gesamt (gruen) Verkehr (violet)")
ggplot(selemissions.pr, aes(x = Jahr, y = D_Gesamt))+
geom_point(col = "blue",shape = 4)+
geom_smooth(method = "lm",mapping = aes(x = Jahr, y = D_Gesamt),col = "blue")+
geom_smooth(method ="lm",mapping = aes(x=Jahr, y = D_Verkehr),col = "red")+
geom_smooth(method ="lm",mapping =aes(x = Jahr,y= BWGesamt), col = "green")+
geom_smooth(method ="lm",mapping =aes(x = Jahr, y = BWVerkehr), col ="purple")+
labs( y = " Prozent von 1990")+
ggtitle(" NO2 Gesamt- und Verkehrsemissionen
Relative Entwicklung 1990 bis 2017",
subtitle = " Regressionsgeraden
BRD: Gesamt (blau) Verkehr (rot)
BW: Gesamt (gruen) Verkehr (violet)")
# Vergleich Trend Verkehrs Emissionen BW Immissionen
emissions2000 <- emissions %>% filter(Jahr >= 2000)
stplot <- vector ("list", length = length (list_names))
names(stplot)<- list_names
for (nm in list_names) {
stplot[[nm]] <- reg_year.plot[[nm]] +
geom_smooth(data = emissions2000, method = "lm",aes(x= Jahr,y = BWVerkehr),col = "red", linetype = 4)+
ggtitle (paste("NO2 Trends: Verkehrs - Emissionen BW /
Immissionen",nm))+
labs (x= "Kalenderjahr", y= "NO2 [ug/m3] | [kt]")
}
for (nm in list_names){
stplot[[nm]] <- stplot[[nm]]+coord_cartesian(xlim= range(my_stations.year[[nm]]$fullyear))
}
stplot %>% walk(print)
# Gesamtemissionen - Verkehrsemissionen
head(emissions)
relative_emissions <- emissions %>%
filter (emissions$Jahr>=2000) %>%
mutate(D_differ = D_Gesamt-D_Verkehr,BW_differ = BWGesamt-BWVerkehr)%>%
map_dfr(proz)
relative_emissions$Jahr <- 2000:2017
head(relative_emissions)
ggplot(relative_emissions, aes( x = Jahr, y = D_Gesamt))+
geom_point(aes( x = Jahr, y = D_differ))+
geom_smooth(aes( x = Jahr, y = D_differ),col = "red")+
geom_smooth(method = "lm", aes( x = Jahr, y = D_differ),col = "red", linetype = 4, se = F)+
ggtitle(" NO2 BRD (Trend in %) : Gesamt-minus Verkehrsemissionen")+
labs( y = "NO2% von 2000 ")