This vignette walks through a realistic analysis using ibger: downloading IPCA (Índice Nacional de Preços ao Consumidor Amplo) inflation data and preparing it for visualization and reporting.
IPCA is Brazil’s official consumer price index, calculated monthly by IBGE. The data is available through aggregate 7060.
Key information from the metadata:
Let’s get the monthly variation for the last 24 months:
ipca_br <- ibge_variables(
aggregate = 7060,
variable = 63,
periods = -24,
localities = "BR"
)
ipca_brThe value column is character because of the API’s
special values (see ?parse_ibge_value). Use
parse_ibge_value() to convert:
ipca_br <- ipca_br |>
mutate(
value = parse_ibge_value(value),
date = as.Date(paste0(period, "01"), format = "%Y%m%d")
)Get all three variation variables at once:
ipca_vars <- ibge_variables(
aggregate = 7060,
variable = c(63, 69, 2265),
periods = -12,
localities = "BR"
)
ipca_vars <- ipca_vars |>
mutate(
value = parse_ibge_value(value),
date = as.Date(paste0(period, "01"), format = "%Y%m%d")
)
ggplot(ipca_vars, aes(date, value, colour = variable_name)) +
geom_line(linewidth = 0.8) +
geom_point(size = 1.5) +
labs(
title = "IPCA — Three measures of variation",
x = NULL, y = "%", colour = NULL,
caption = "Source: IBGE via ibger"
) +
theme_minimal() +
theme(legend.position = "bottom")Classification 315 organizes IPCA items hierarchically. The top-level groups (level 1 categories) are things like Food and beverages (7170), Housing (7445), Transportation (7486), etc.
First, find the category IDs you need:
cats <- tidyr::unnest(meta$classifications, categories)
# Level-1 groups (just below the general index)
cats |> filter(category_level == "1")Now query specific groups:
groups <- ibge_variables(
aggregate = 7060,
variable = 63,
periods = -12,
localities = "BR",
classification = list("315" = c(7170, 7445, 7486, 7558, 7625, 7660, 7712, 7766, 7786))
)
groups <- groups |>
mutate(
value = parse_ibge_value(value),
date = as.Date(paste0(period, "01"), format = "%Y%m%d")
)
ggplot(groups, aes(date, value, fill = classification_315)) +
geom_col(position = "dodge", alpha = 0.85) +
labs(
title = "IPCA by product group — Monthly variation",
x = NULL, y = "%", fill = NULL,
caption = "Source: IBGE via ibger"
) +
theme_minimal() +
theme(legend.position = "bottom", legend.text = element_text(size = 7))Aggregate 7060 is available at level N7 (metropolitan areas). Compare inflation across major cities:
Pick a few and compare:
ipca_metros <- ibge_variables(
aggregate = 7060,
variable = 2265,
periods = -12,
localities = list(N7 = c(3501, 3301, 2901, 4101, 1501))
)
ipca_metros <- ipca_metros |>
mutate(
value = parse_ibge_value(value),
date = as.Date(paste0(period, "01"), format = "%Y%m%d")
)
ggplot(ipca_metros, aes(date, value, colour = locality_name)) +
geom_line(linewidth = 0.8) +
labs(
title = "IPCA — 12-month cumulative by metro area",
x = NULL, y = "%", colour = NULL,
caption = "Source: IBGE via ibger"
) +
theme_minimal() +
theme(legend.position = "bottom")For a more complete analysis, combine multiple queries. For instance, download the general index for all metro areas and reshape:
all_metros <- ibge_variables(
aggregate = 7060,
variable = c(63, 69, 2265),
periods = -12,
localities = "N7",
classification = list("315" = 7169)
)
all_metros <- all_metros |>
mutate(value = parse_ibge_value(value)) |>
select(variable_name, locality_name, period, value) |>
pivot_wider(names_from = variable_name, values_from = value)
all_metrosIPCA has 365 categories in classification 315. Querying all categories for all periods and all metro areas can easily exceed the 100,000-value limit. Strategies:
-1 or
-3 instead of -12dplyr::bind_rows()The value column returned by
ibge_variables() is always character, because the IBGE API
uses special codes for certain data conditions. Use
parse_ibge_value() to convert to numeric in one step:
The function handles all IBGE conventions:
| Code | Becomes | Meaning |
|---|---|---|
- |
0 |
Numeric zero (not from rounding) |
.. |
NA |
Not applicable |
... |
NA |
Data not available |
X |
NA |
Suppressed to protect confidentiality |