aemet_forecast_tidy | R Documentation |
Helpers for
aemet_forecast_daily()
and aemet_forecast_hourly()
:
aemet_forecast_vars_available()
extracts the values available on
the dataset.
aemet_forecast_tidy()
produces a tibble
with the
forecast for var
.
aemet_forecast_tidy(x, var)
aemet_forecast_vars_available(x)
x |
A database extracted with |
var |
Name of the desired var to extract |
A vector of characters (aemet_forecast_vars_available()
)
or a tibble
(aemet_forecast_tidy()
).
Other forecasts:
aemet_forecast_beaches()
,
aemet_forecast_daily()
,
aemet_forecast_fires()
# Hourly values
hourly <- aemet_forecast_hourly(c("15030", "28080"))
# Vars available
aemet_forecast_vars_available(hourly)
# Get temperature
temp <- aemet_forecast_tidy(hourly, "temperatura")
library(dplyr)
# Make hour - Need lubridate to adjust timezones
temp_end <- temp %>%
mutate(
forecast_time = lubridate::force_tz(
as.POSIXct(fecha) + hora,
tz = "Europe/Madrid"
)
)
# Add also sunset and sunrise
suns <- temp_end %>%
select(nombre, fecha, orto, ocaso) %>%
distinct_all() %>%
group_by(nombre) %>%
mutate(
ocaso_end = lubridate::force_tz(
as.POSIXct(fecha) + ocaso,
tz = "Europe/Madrid"
),
orto_end = lubridate::force_tz(
as.POSIXct(fecha) + orto,
tz = "Europe/Madrid"
),
orto_lead = lead(orto_end)
) %>%
tidyr::drop_na()
# Plot
library(ggplot2)
ggplot(temp_end) +
geom_rect(data = suns, aes(
xmin = ocaso_end, xmax = orto_lead,
ymin = min(temp_end$temperatura),
ymax = max(temp_end$temperatura)
), alpha = .4) +
geom_line(aes(forecast_time, temperatura), color = "blue4") +
facet_wrap(~nombre, nrow = 2) +
scale_x_datetime(labels = scales::label_date_short()) +
scale_y_continuous(labels = scales::label_number(suffix = "º")) +
labs(
x = "", y = "",
title = "Forecast: Temperature",
subtitle = paste("Forecast produced on", format(temp_end$elaborado[1],
usetz = TRUE
))
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.