aemet_forecast_daily | R Documentation |
Get a database of daily or hourly weather forecasts for a given municipality.
aemet_forecast_daily(x, verbose = FALSE, extract_metadata = FALSE)
aemet_forecast_hourly(x, verbose = FALSE, extract_metadata = FALSE)
x |
A vector of municipality codes to extract. For convenience,
climaemet provides this data on the dataset aemet_munic
(see |
verbose |
Logical |
extract_metadata |
Logical |
Forecasts format provided by the AEMET API have a complex structure.
Although climaemet returns a tibble
, each forecasted value is
provided as a nested tibble
. aemet_forecast_tidy()
helper function can
unnest these values an provide a single unnested tibble
for the requested
variable.
If extract_metadata = TRUE
a simple tibble
describing the value of
each field of the forecast is returned.
A nested tibble. Forecasted values can be extracted with
aemet_forecast_tidy()
. See also Details
You need to set your API Key globally using aemet_api_key()
.
aemet_munic for municipality codes.
Other aemet_api_data:
aemet_daily_clim()
,
aemet_extremes_clim()
,
aemet_last_obs()
,
aemet_monthly
,
aemet_normal
,
aemet_stations()
Other forecasts:
aemet_forecast_tidy()
# Select a city
data("aemet_munic")
library(dplyr)
munis <- aemet_munic %>%
filter(municipio_nombre %in% c(
"Santiago de Compostela",
"Lugo"
)) %>%
pull(municipio)
daily <- aemet_forecast_daily(munis)
# Metadata
meta <- aemet_forecast_daily(munis, extract_metadata = TRUE)
glimpse(meta$campos)
# Vars available
aemet_forecast_vars_available(daily)
# This is nested
daily %>%
select(municipio, fecha, nombre, temperatura)
# Select and unnest
daily_temp <- aemet_forecast_tidy(daily, "temperatura")
# This is not
daily_temp
# Wrangle and plot
daily_temp_end <- daily_temp %>%
select(
elaborado, fecha, municipio, nombre, temperatura_minima,
temperatura_maxima
) %>%
tidyr::pivot_longer(cols = contains("temperatura"))
# Plot
library(ggplot2)
ggplot(daily_temp_end) +
geom_line(aes(fecha, value, color = name)) +
facet_wrap(~nombre, ncol = 1) +
scale_color_manual(
values = c("red", "blue"),
labels = c("max", "min")
) +
scale_x_date(
labels = scales::label_date_short(),
breaks = "day"
) +
scale_y_continuous(
labels = scales::label_comma(suffix = "º")
) +
theme_minimal() +
labs(
x = "", y = "",
color = "",
title = "Forecast: 7-day temperature",
subtitle = paste(
"Forecast produced on",
format(daily_temp_end$elaborado[1], usetz = TRUE)
)
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.