| aemet_forecast_tidy | R Documentation |
Helpers for
aemet_forecast_daily()
and aemet_forecast_hourly():
aemet_forecast_vars_available() extracts the values available in
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 dataset extracted with |
var |
Name of the desired variable to extract. |
A character vector from aemet_forecast_vars_available() or a
tibble from aemet_forecast_tidy().
Forecast functions:
aemet_forecast_beaches(),
aemet_forecast_daily(),
aemet_forecast_fires()
# Hourly values
hourly <- aemet_forecast_hourly(c("15030", "28079"))
# Variables available.
aemet_forecast_vars_available(hourly)
# Get temperature
temp <- aemet_forecast_tidy(hourly, "temperatura")
library(dplyr)
# Create a forecast time. This needs lubridate to adjust time zones.
temp_end <- temp |>
mutate(
forecast_time = lubridate::force_tz(
as.POSIXct(fecha) + hora,
tz = "Europe/Madrid"
)
)
# Add 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 = 0.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.