-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplotly-fcn.R
More file actions
159 lines (119 loc) · 6.85 KB
/
Copy pathplotly-fcn.R
File metadata and controls
159 lines (119 loc) · 6.85 KB
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
# works on ftp data. created list of plotly objects. filters by parameter provided,
# loops though each STATION_NAME in data and creates a plotly object coloured by instrument.
# parameter<-"O3"
plotly_fcn<-function(data, # hourly data
all_data, # hourly and daily data in one tidy tibble
parameter){
library(tidyverse)
#loop through each STATION_NAME (should only be one)
purrr::map((data |>
dplyr::filter(PARAMETER %in% parameter) |>
dplyr::distinct(STATION_NAME) |>
dplyr::arrange(STATION_NAME))$STATION_NAME,
function(station){
# FOR TESTING #
# station<-(data |>
# filter(PARAMETER %in% parameter) |>
# distinct(STATION_NAME) |>
# arrange(STATION_NAME))$STATION_NAME
# station<-"Kitimat Riverlodge"
# parameter<-"PM25"
# END TESTING
if(parameter!="O3"){
#### for all parameters except ozone:####
plot_data<-all_data |>
dplyr::filter(PARAMETER %in% parameter &
STATION_NAME %in% station) |>
dplyr::mutate(TIME_AVG=factor(TIME_AVG,
levels = c("Hourly",
"24-HR Running Ave",
"Daily")))
p<- ggplot2::ggplot(plot_data,
ggplot2::aes(x=DATE_PST,
y=RAW_VALUE,
color=TIME_AVG,
linetype=INSTRUMENT))+
ggplot2::geom_line(alpha=1.0) +
ggplot2::geom_point(alpha=0.8,
pch=21)+
ggplot2::labs(title=paste(station,
"Hourly and Daily",
parameter),
x="",
y=parameter) +
ggplot2::scale_color_brewer(palette = "Set1")+
# scale_color_viridis_d(option = "D") +
# scale_linetype_manual(values=c("solid",
# "dotted")) +
ggplot2::scale_y_continuous(breaks = seq(0,
max(plot_data$RAW_VALUE,
na.rm = TRUE)*1.05,
20)#,
# minor_breaks = seq(10,
# max(plot_data$RAW_VALUE,
# na.rm = TRUE)*1.05,
# 20)
)+
ggplot2::scale_x_datetime(date_breaks = "2 weeks",
date_labels = "%b %d")+
ggplot2::theme_bw()+
ggplot2::theme(axis.text.x = element_text(angle = 90))
} else{
#### for ozone (8-hour running average): ####
plot_data<-data |>
dplyr::filter(PARAMETER %in% parameter &
STATION_NAME %in% station) |>
# rename to date for openair
dplyr::rename(date=DATE_PST) %>%
dplyr::group_by(STATION_NAME,PARAMETER,INSTRUMENT) %>%
dplyr::group_modify(~ openair::rollingMean(.x,
pollutant="RAW_VALUE",
width=8, #8 hour rolling mean
new.name="ROLL_MEAN_8HR", #column heading
data.thresh=75, #>=18 hr. for rolling mean
align="right")
) %>%
ungroup %>%
dplyr::rename(DATE_PST=date)
p<- ggplot2::ggplot(plot_data,
ggplot2::aes(x=DATE_PST,
y=ROLL_MEAN_8HR,
# color=TIME_AVG,
linetype=INSTRUMENT))+
ggplot2::geom_line(alpha=1.0,
color="orange") +
ggplot2::geom_point(alpha=0.8,
pch=21,
color="orange")+
ggplot2::labs(title=paste(station,
"Rolling 8-hour",
parameter),
x="",
y=parameter) +
ggplot2::scale_color_brewer(palette = "Set1")+
# scale_color_viridis_d(option = "D") +
# scale_linetype_manual(values=c("solid",
# "dotted")) +
ggplot2::scale_y_continuous(breaks = seq(0,
max(plot_data$ROLL_MEAN_8HR,
na.rm = TRUE)*1.05,
20)#,
# minor_breaks = seq(10,
# max(plot_data$ROLL_MEAN_8HR,
# na.rm = TRUE)*1.05,
# 20)
)+
ggplot2::scale_x_datetime(date_breaks = "2 weeks",
date_labels = "%b %d")+
ggplot2::theme_bw()+
ggplot2::theme(axis.text.x = element_text(angle = 90))
}
plotly::ggplotly(p) |>
plotly::layout(legend = list(orientation = 'h',y = -0.5))
}#STATION LOOP
)#map
}
# plotly_fcn(data=data,
# all_data=all_data,
# parameter="PM25"
# )