library(httr2)
library(dplyr)
library(purrr)
library(tidyr)
library(stringr)
library(ggplot2)
Evolution of gas prices in Llinars del Vallès
Use this R
libraries:
theme_set(
theme_bw() +
theme(
axis.title = element_blank()
) )
Define function to retrieve information:
<- function(filtro, args) {
get_gas paste0(
"https://sedeaplicaciones.minetur.gob.es",
"/ServiciosRESTCarburantes/PreciosCarburantes/EstacionesTerrestresHist/",
paste(args, collapse = "/")
filtro, %>%
) request() %>%
req_perform() %>%
resp_body_json() %>%
"ListaEESSPrecio"]] %>%
.[[map(\(x) as_tibble(list_flatten(x))) %>%
bind_rows() %>%
::clean_names()
janitor }
Retrieve yesterday’s gas prices in catalan provinces:
<- c("08", "43", "17", "25")
prov
<- map(prov, \(x) get_gas(
gCat filtro = "FiltroProvincia/",
args = c(format(Sys.Date() - 1, "%d-%m-%Y"), x)
%>%
)) bind_rows() %>%
mutate(precio = as.numeric(gsub(",", ".", precio_gasolina_95_e5)))
Summary of gas stations and price statistics per province:1
%>%
gCat summarise(
n = n(),
mean = mean(precio, na.rm = TRUE) %>% round(3),
median = median(precio, na.rm = TRUE) %>% round(3),
sd = sd(precio, na.rm = TRUE) %>% round(3),
.by = provincia
)
# A tibble: 4 × 5
provincia n mean median sd
<chr> <int> <dbl> <dbl> <dbl>
1 BARCELONA 812 1.50 1.54 0.111
2 TARRAGONA 234 1.50 1.56 0.113
3 GIRONA 271 1.52 1.56 0.089
4 LLEIDA 182 1.45 1.49 0.098
Prices from the largest providers in Catalonia, 2025:
%>%
gCat filter(rotulo %in% (
summarise(gCat, .by = rotulo, n = n()) %>%
arrange(n) %>% pull(rotulo) %>% tail(10)
%>%
)) drop_na() %>%
select(rotulo, precio) %>%
ggplot(aes(rotulo, precio)) +
geom_boxplot() +
labs(
title = "Gas prices in Catalonia, 2025",
subtitle = "Euros per litre of gas 95 E5 (10 largest providers)"
)
Narrow down geographical scope: Consider municipalities nearby Llinars del Vallès
<- gCat %>%
muns summarise(n = n(), m = round(mean(precio), 3), .by = c(municipio, id_municipio)) %>%
filter(str_detect(municipio,
"Llinars|Cardedeu|Vilamajor|Palautordera|Sant Celoni"
))
::kable(muns, caption = "Average price and station count per municipality") knitr
municipio | id_municipio | n | m |
---|---|---|---|
Cardedeu | 908 | 4 | 1.586 |
Llinars del Vallès | 973 | 3 | 1.612 |
Sant Antoni de Vilamajor | 1060 | 1 | 1.569 |
Sant Celoni | 1065 | 4 | 1.556 |
Santa Maria de Palautordera | 1123 | 3 | 1.525 |
Retrieve daily prices for Gasolina 95 - E5 at all locations within scope. Define request parameters:
# api end point
<- "FiltroMunicipioProducto/"
fil
<- Sys.time()
t # parameters
<- 1/12
anys <- set_names(muns$id_municipio, muns$municipio)
mun <- set_names("01", "gas95-e5")
prod <- format(Sys.Date() - 1:(anys*365), "%d-%m-%Y")
date <- expand_grid(date, mun, prod)
params
<- apply(params, 1, \(x) mutate(get_gas(filtro = fil, args = x), date = as.Date(x[1]))) %>%
gas bind_rows() %>%
mutate(precio = as.numeric(gsub(",", ".", precio_producto)))
<- Sys.time() - t t
Evolution of average price per municipality:
summarise(gas, .by = c(municipio, date), price = mean(precio), desv = sd(precio)) %>%
ggplot(aes(x = date, y = price, colour = municipio)) +
geom_line() +
labs(colour = "Municipality", title = "Gas prive evolution, last 30 days")
Footnotes
Considering 95 gas - E5↩︎