Skip to content

Instantly share code, notes, and snippets.

View aaronschiff's full-sized avatar

Aaron Schiff aaronschiff

View GitHub Profile
@aaronschiff
aaronschiff / fixed-geom-chart.R
Created June 10, 2022 01:02
Create geom_tile chart with tiles of fixed size
# The objective is to make a faceted ggplot 'heatmap' with geom_tile where the
# rendered tiles in the output PNG file have a fixed width and height,
# regardless of whatever else is in the chart.
# When ggplot charts are built, the height and width of all components are
# predetermined *except* for the facet panels. Thus the approach here is to
# render an initial version of the chart to determine the height and width
# of the predetermined elements. Then the overall height and width of the
# output PNG are calculated based on what is needed to give the rendered
# tiles the desired size.
@aaronschiff
aaronschiff / business-confidence.R
Last active August 7, 2019 22:08
GDP vs business confidence correlation
# Setup
library(conflicted)
library(tidyverse)
library(lubridate)
library(readxl)
conflict_prefer("filter", "dplyr")
conflict_prefer("lag", "dplyr")
conflict_prefer("lead", "dplyr")
@aaronschiff
aaronschiff / gather-unite-spread.R
Created January 10, 2019 00:03
Example of gather-unite-spread data reshaping in R
library(tidyverse)
example_dat <- tribble(
~year, ~group, ~revenues, ~costs,
2015, "Apple", 1000, 500,
2016, "Apple", 1500, 600,
2017, "Apple", 1200, 550,
2015, "Pear", 2000, 1100,
2016, "Pear", 2500, 1300,
2017, "Pear", 2900, 1500,
@aaronschiff
aaronschiff / make-timetable-better.R
Created April 18, 2018 03:15
Make a Japanese-style timetable using AT timetable data (better version)
# Make a Japanese-style timetable using AT public transport data (better version)
# You'll need to download AT's GTFS data file first from https://at.govt.nz/about-us/at-data-sources/google-transit-feed/
library(magrittr)
library(tidyverse)
library(hms)
library(lubridate)
library(stringr)
library(ggthemes)
@aaronschiff
aaronschiff / make-timetable.R
Created April 18, 2018 00:35
Make a Japanese-style timetable using AT public transport data
# Make a Japanese-style timetable using AT public transport data
# You'll need to download AT's GTFS data file first from https://at.govt.nz/about-us/at-data-sources/google-transit-feed/
library(magrittr)
library(tidyverse)
library(hms)
library(lubridate)
library(stringr)
library(ggthemes)
@aaronschiff
aaronschiff / read-chc-data.R
Created August 24, 2017 04:39
Download all data from Christchurch City Council's property API
# -----------------------------------------------------------------------------
# Setup
rm(list = ls())
library(magrittr)
library(jsonlite)
library(httr)
library(lubridate)
library(tidyverse)
library(scales)
source("clean-ggplot-theme.R")
@aaronschiff
aaronschiff / map-chc-property.R
Created August 24, 2017 04:38
Simple graphs and maps of Christchurch property sales data
# Map Christchurch property data
# -----------------------------------------------------------------------------
# Setup
rm(list = ls())
library(magrittr)
library(lubridate)
library(tidyverse)
library(scales)
library(sf)
@aaronschiff
aaronschiff / jaggy.R
Created August 15, 2017 01:20
Jaggy label in ggplot
library(ggplot2)
dat <- data.frame(x = 1:10, y = runif(10))
chart <- ggplot(dat) +
geom_bar(aes(x = x, y = y), stat = "identity") +
geom_text(aes(x = 2, y = 1.5, label = "This text is jaggy"), size = 6)
print(chart)
@aaronschiff
aaronschiff / analysis-mbie-mfe-data.R
Created August 10, 2017 03:46
Make a simple chart of property prices from MBIE/MfE urban development capacity data
# -----------------------------------------------------------------------------
# Setup
library(magrittr)
library(lubridate)
library(tidyverse)
library(ggplot2)
library(scales)
source("clean-ggplot-theme.R")
# -----------------------------------------------------------------------------
@aaronschiff
aaronschiff / read-infoshare.R
Last active July 12, 2017 05:54
R function to read CSV files exported from Statistics New Zealand's Infoshare system
# Helper function to read csv files exported from Infoshare
# Categories should be a list of names of the categorical variables to be created
read_infoshare <- function(filename, categories) {
num_categories <- length(categories)
dat <- read_csv(filename)
names(dat)[1] <- "date"
# Drop rows where all columns except the first column are NA, to remove junk at the bottom of the file
junk_rows <- apply(dat[, -1], 1, function(x) prod(is.na(x))) == 1
dat <- dat[!junk_rows, ]