Skip to content

Instantly share code, notes, and snippets.

#load the libraries
library(ggplot2)
library(grid)
library(dplyr)
#load the data
#https://public.opendatasoft.com/explore/dataset/us-zip-code-latitude-and-longitude/export/
zips <-
read.csv("us-zip-code-latitude-and-longitude.csv",
header = TRUE, stringsAsFactors = TRUE,sep=";")
@cavedave
cavedave / Martin-Quinn.r
Created November 15, 2019 19:32
Martin-Quinn scores for justices, 1937-2018. The r package ggplot2 code is very slightly modified version of https://rud.is/b/2016/06/28/making-time-rivers-in-r/ by https://twitter.com/hrbrmstr data from https://mqscores.lsa.umich.edu/measures.php This made the reddit front page in a slightl different version https://www.reddit.com/r/dataisbeaut…
```{r}
library(dplyr)
library(readr)
library(ggplot2) # devtools::install_github("hadley/ggplot2")
#library(hrbrmisc) # devtools::install_github("hrbrmstr/hrbrmisc")
library(grid)
library(scales)
library(grid)
@cavedave
cavedave / CenterUKVoting.r
Created November 3, 2019 09:37
Make a map of where people in the UK vote
Now start with maps and locations
See if i can get the center of constituencies
data from https://geoportal.statistics.gov.uk/datasets/westminster-parliamentary-constituencies-december-2017-super-generalised-clipped-boundaries-in-the-uk/data
```{r}
locat<-read_csv("Westminster_Parliamentary_Constituencies_December_2017_Super_Generalised_Clipped_Boundaries_in_the_UK.csv")#, sheet = 2, skip=6 , header=TRUE
head(locat)
@cavedave
cavedave / ukelection.Rmd
Last active November 1, 2019 22:38
UK election visualisations
---
title: "uk election"
output: html_notebook
---
Inspired by this great graph by Alasdair Rae
https://twitter.com/undertheraedar/status/1190192038274310144 i want to make an election visualisation
data from https://commonslibrary.parliament.uk/parliament-and-elections/elections-elections/constituency-data-election-results/
```{r}
yyyy mm tmax tmin af rain sun
1853 1 NA NA NA 57.3 NA
1853 2 NA NA NA 32.3 NA
1853 3 NA NA NA 65.5 NA
1853 4 NA NA NA 46.2 NA
1853 5 NA NA NA 13.2 NA
1853 6 NA NA NA 53.3 NA
1853 7 NA NA NA 78.0 NA
1853 8 NA NA NA 56.6 NA
1853 9 NA NA NA 24.5 NA
library(tidyverse)
money <- read.csv("NS_Table_3_1a_1617.csv",skip = 5, header = T, nrows = 100, dec=",")
#, nrows = 1,
money <- money[-1,]
money<-select (money,-c(X,X.1,X.2,X.3,X.4,X.5,X.6,X.7,X.8,X2008.09..a.))
#money<-as.numeric(sub(",", ".", money))
money$Percentile.point <- as.integer(money$Percentile.point)
#money <- money[order(money$Percentile.point), ]
head(money)
#make an id row
@cavedave
cavedave / Elections.r
Created October 5, 2019 13:49
Visualisation of UK elections. I keep seeing 'the referendum, we saw the biggest democratic exercise in our history' so I looked the the number of votes/turnout numbers and the EU referendum isn't https://fullfact.org/europe/eu-referendum-not-largest-democratic-exercise/
elect <- read.csv("ElectionsUk.csv", header = TRUE)
#turn this format into a date
library(lubridate)
elect$date2 <- dmy(elect$date)
p <- ggplot(elect,aes(x = date2, y = Millions.of.Votes)) +#, colour = elect$Type
geom_point(aes(colour = Type),
shape=21,
fill= "White",
@cavedave
cavedave / age15to64.r
Last active July 10, 2022 13:40
Sex Ratio in Europe based on code from https://jschoeley.github.io/2018/07/03/bubble-grid_vs_choropleth.html Norway has a missing spot in future combine Nord Trøndelag and Sort Trønderlag to fill this out.
library(eurostat) # eurostat data
library(rnaturalearth) # worldwide map data
library(tidyverse) # tidy data transformation
library(lubridate) # date and time support
library(sf) # simple features GIS
library(RColorBrewer)
euro_pop <-
get_eurostat('demo_r_pjanaggr3', stringsAsFactors = FALSE) %>%
@cavedave
cavedave / data.py
Created September 30, 2019 14:26
Where hurricances became cat since 1967 when satellites could see them in the sea reliably. This is a copy of this map https://twitter.com/philklotzbach/status/1178137123687284737 by Philip Klotzbach but I couldn't find his code so I made my own. Data from https://www.nhc.noaa.gov/data/hurdat/hurdat2-1851-2018-051019.txt
#The start of this file is from https://www.reddit.com/r/CodingHelp/comments/9jguwj/help_with_python_data_analsyis/
#Specifically David Kopp http://www.cis.umassd.edu/~dkoop/ where he ingets the noaa data
import os
from urllib.request import urlretrieve
# download the data if we don't have it locally
url = "https://www.nhc.noaa.gov/data/hurdat/hurdat2-1851-2017-050118.txt"
local_fname = "hurdat2.txt"
if not os.path.exists("hurdat2.txt"):
urlretrieve(url, local_fname)
@cavedave
cavedave / getposition.r
Last active January 1, 2020 09:22
European Centers of population Data from
eur<-read_csv('GEOSTAT_grid_POP_1K_2011_V2_0_1.csv')
#two ways to extract east and north
eur$North <-str_match(eur$GRD_ID, "1kmN(\\d\\d\\d\\d)E(\\d\\d\\d\\d)")[,2]
eur$East <-str_match(eur$GRD_ID, "1kmN(\\d\\d\\d\\d)E(\\d\\d\\d\\d)")[,3]
eur<-eur%>%
mutate(
lat = as.numeric(gsub('.*N([0-9]+)[EW].*', '\\1', GRD_ID))/100,
lng = as.numeric(gsub('.*[EW]([0-9]+)', '\\1', GRD_ID)) * ifelse(gsub('.*([EW]).*', '\\1', GRD_ID) == 'W', -1, 1) / 100
)