Skip to content

Instantly share code, notes, and snippets.

@mroutley
Last active October 3, 2021 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mroutley/2e067448ed9315ea8e316cc8486f32fd to your computer and use it in GitHub Desktop.
Save mroutley/2e067448ed9315ea8e316cc8486f32fd to your computer and use it in GitHub Desktop.
Consumer Price Index charts
library(tidyverse)
library(magrittr)
library(cansim)
# For filtering later, here are the locations and products that we'll want
keep_GEO <- c("Canada", "Toronto, Ontario", "Ontario")
keep_products <- c("All-items", "Alcoholic beverages", "Apples",
"Transportation", "Public transportation", "Parking fees")
cansim_table <- "18-10-0004"
cpi <- cansim::get_cansim(cansim_table) %>%
dplyr::filter(GEO %in% keep_GEO,
`Products and product groups` %in% keep_products) %>%
dplyr::rename(Product = `Products and product groups`) %>%
dplyr::select(Date, GEO, Product, val_norm)
# Some example plots that could be refactored into something more versatile
# All items since 2020
ggplot2::ggplot(data = dplyr::filter(cpi,
Product == "All-items",
lubridate::year(Date) > 2009),
ggplot2::aes(x = Date, y = val_norm, colour = GEO)) +
ggplot2::geom_line() +
ggplot2::ylab("Consumer Price Index (2002=100)") +
ggplot2::xlab("") +
ggplot2::guides(colour = ggplot2::guide_legend(title = "Geography"))
# All items
ggplot2::ggplot(data = dplyr::filter(cpi, Product == "All-items"),
ggplot2::aes(x = Date, y = val_norm, colour = GEO)) +
ggplot2::geom_line() +
ggplot2::ylab("Consumer Price Index (2002=100)") +
ggplot2::xlab("") +
ggplot2::guides(colour = ggplot2::guide_legend(title = "Geography"))
# Just apples and alcoholic beverages
ggplot2::ggplot(data = dplyr::filter(cpi,
GEO == "Canada",
Product %in% c("Alcoholic beverages", "Apples")),
ggplot2::aes(x = Date, y = val_norm, colour = Product)) +
ggplot2::geom_line() +
ggplot2::ylab("Consumer Price Index (2002=100)") +
ggplot2::xlab("") +
ggplot2::guides(colour = ggplot2::guide_legend(title = "Product"))
# Transportation focused products
ggplot2::ggplot(data = dplyr::filter(cpi,
GEO == "Canada",
lubridate::year(Date) > 1999,
Product %in% c("All-items", "Transportation",
"Public transportation", "Parking fees")),
ggplot2::aes(x = Date, y = val_norm, colour = Product)) +
ggplot2::geom_line() +
ggplot2::ylab("Consumer Price Index (2002=100)") +
ggplot2::xlab("") +
ggplot2::guides(colour = ggplot2::guide_legend(title = "Product"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment