-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomeclimate.rmd
65 lines (48 loc) · 1.82 KB
/
homeclimate.rmd
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
```{r setup}
library(jsonlite)
library(tidyverse)
source("R/keys.r")
```
```{r data_scd30}
# parameters
value_days = 10
# query thingspeak channel
url_base = glue::glue("https://api.thingspeak.com/channels/{id_thingspeak}/feeds.json?")
url_query = glue::glue(url_base, "api_key={key_read}", "&days={value_days}")
data_sensor_raw <- read_json(url_query, simplifyVector = TRUE)
# shape raw data
data_sensor <- tibble(data_sensor_raw$feeds) %>%
mutate(time = lubridate::ymd_hms(created_at, tz = "Europe/Paris")) %>%
mutate(across(field1:field3, as.numeric)) %>%
select(time, co2 = field1, temp = field2, rh = field3) %>%
pivot_longer(cols = co2:rh)
# write_rds(data_sensor, file = glue::glue("data/archive_", format(Sys.time(), "%Y%m%d"),".rds"))
```
```{r plot_scd30}
# style
list_variables = c("co2","rh")
list_colors = c("#2CA02CFF","#1F77B4FF","#FF7F0EFF")
label_variables <- as_labeller(c("co2" = "co2 [ppm]", "rh" = "rh [%]", "temp" = "temp [°C]"))
table_threshold <- tibble(
name = c("co2","rh","temp"),
value = c(800, 50, 19))
# plot selected data
# data_sensor <- readr::read_rds("data/archive_20210206.rds")
plot_sensor <- data_sensor %>%
filter(name %in% list_variables) %>%
ggplot(aes(x = time, y = value, color = name)) +
geom_line(alpha = 0.8) +
stat_smooth (
geom = "line", alpha = 0.3, size = 3,
method = "gam", formula = y ~ s(x, k = 12)) +
geom_hline(
data = table_threshold %>% filter(name %in% list_variables),
aes(yintercept = value), linetype = 2) +
facet_wrap(vars(name), scale = "free_y", labeller = label_variables) +
scale_color_manual(values = list_colors) + labs(x=NULL, y=NULL) +
theme_bw() + theme(legend.position = "none")
# export graphics and data
ggsave(
plot_sensor, file = "R/figures/sensor_scd30.png",
dpi=120, width=210, height=100, scale=1, units="mm")
```