Skip to content

Instantly share code, notes, and snippets.

@thoughtfulbloke
thoughtfulbloke / NZvsUK.R
Created May 6, 2020 10:39
code for NZ vs UK to show similarity in early progression
library(dplyr)
library(lubridate)
library(ggplot2)
EUcdc <- read.csv("https://opendata.ecdc.europa.eu/covid19/casedistribution/csv",
stringsAsFactors = FALSE, fileEncoding = "UTF-8-BOM")
# NZ level 4 lockdown 26 March
#Boris Handshakes 3 March,
EUcdc %>%
filter(countriesAndTerritories %in% c("United_Kingdom", "New_Zealand")) %>%
@thoughtfulbloke
thoughtfulbloke / Apple_Mobility_example.R
Created May 4, 2020 23:51
Apple Mobility Data example
library(readr)
library(dplyr)
library(tidyr)
library(lubridate)
library(ggplot2)
# from the downloaded apple mobiblity data of the day
mobl <- read_csv("applemobilitytrends-2020-05-03.csv")
# colums added for each day, so get number of coluns in current data
extant <- ncol(mobl)
# graph specific choices
library(dplyr)
library(ggplot2)
library(scales)
library(patchwork)
# random data, ordered
set.seed(2020)
series1_ind <- sort(sample(0:100, 25, replace=TRUE))
series2_ind <- sort(sample(1000:30000, 25, replace=TRUE))
example <- data_frame(step=1:25, series1_ind, series2_ind)
# https://www.mbie.govt.nz/immigration-and-tourism/tourism-research-and-data/tourism-data-releases/monthly-regional-tourism-estimates/regional-tourism-estimates/regional-tourism-estimates-key-pivot-table/
# Regional Tourism Estimates key pivot table
library(readxl)
library(tidyr)
library(dplyr)
library(ggplot2)
library(forcats)
tourism <- read_excel("rte-pivot-table-ye-march-2015.xlsx", sheet="Database")
TA_tour <- tourism %>% filter(YEMar == 2015) %>%
@thoughtfulbloke
thoughtfulbloke / UKelection2019unofficial.csv
Created December 14, 2019 04:42
Scraped Election Results for UK 2019 election, as no official repository exists (source BBC news)
We can't make this file beautiful and searchable because it's too large.
"constituency","party","candidate","attribute","value"
"Aberavon","Labour","Stephen Kinnock","Votes:",17008
"Aberavon","Labour","Stephen Kinnock","Vote share %:",53.8
"Aberavon","Labour","Stephen Kinnock","Vote share change:",-14.3
"Aberavon","Conservative","Charlotte Lang","Votes:",6518
"Aberavon","Conservative","Charlotte Lang","Vote share %:",20.6
"Aberavon","Conservative","Charlotte Lang","Vote share change:",2.9
"Aberavon","The Brexit Party","Glenda Davies","Votes:",3108
"Aberavon","The Brexit Party","Glenda Davies","Vote share %:",9.8
"Aberavon","The Brexit Party","Glenda Davies","Vote share change:",9.8
@thoughtfulbloke
thoughtfulbloke / honest_rents_dec19.R
Created December 9, 2019 05:57
rent change in NZ
library(readr)
geo_mean <- read_csv("https://www.mbie.govt.nz/assets/Data-Files/Building-and-construction/Tenancy-and-housing/Rental-bond-data/Territorial-Authority/ta-geometric-mean.csv",
col_types = cols(
.default = col_double(),
Month = col_date(format = "")
))
library(dplyr)
library(ggplot2)
geo_mean %>% filter(Month >= as.Date("2009-01-01")) %>%
mutate(govt = c(rep("National", 105), rep("Labour",25))) %>%
@thoughtfulbloke
thoughtfulbloke / datacharter_responders.R
Created November 22, 2019 04:15
Code for making graph
# tweets are assumed to be gathered for this
library(stringr)
library(dplyr)
library(tidyr)
library(ggplot2)
library(ggthemes)
library(ggrepel)
library(Rtsne)
disc <- read.csv("discussion.csv", stringsAsFactors = FALSE)
#globe map without countries distorted by polygon points being clipped by the "edge of the world"
#This is my hacking around of the fuller script
#https://github.com/r-spatial/sf/blob/master/demo/twitter.R
#linked to from section 8.1 of https://keen-swartz-3146c4.netlify.com/plotting.html
library(sf)
library(maptools)
library(animation)
data(wrld_simpl)
@thoughtfulbloke
thoughtfulbloke / botypatterns2019.R
Created November 13, 2019 23:40
Voting similarity among Bird of the Year 2019 candidates
library(Rtsne)
library(vroom)
library(dplyr)
library(tidyr)
library(stringr)
library(ggplot2)
library(ggrepel)
library(janitor)
# data from https://www.dragonfly.co.nz/news/2019-11-12-boty.html
@thoughtfulbloke
thoughtfulbloke / breath.R
Created October 31, 2019 00:16
making animated breathing gif in #rstats
library(ggplot2)
library(dplyr)
library(gganimate)
exhale <- 5 # seconds
exhaled_pause <- 3 # seconds
inhale <- 4 # seconds
inhaled_pause <- 4 # seconds
frames_per_second = 16
exh <- data.frame(stage = rep("exhale", exhale*frames_per_second),