Skip to content

Instantly share code, notes, and snippets.

View geotheory's full-sized avatar

Robin Edwards geotheory

View GitHub Profile
@geotheory
geotheory / urban_gender_analysis.R
Last active August 29, 2015 14:02
This script demo's how to process the 2011 UK Census shapefiles and data to produce graphics used in http://geotheory.co.uk/blog/2014/06/23/gender-in-urban-workplaces/
require(maptools)
require(fields)
require(ggplot2)
require(stringr)
bng = "+init=epsg:27700"
workdir = '[change to working directory path]'
setwd(workdir)
R.utils::mkdirs('wz')
@geotheory
geotheory / bng_to_latlons.R
Created June 28, 2014 16:01
Script to convert csv of British National Grid coordinates (OSBG36) to lat-longs (WGS84)
# read in csv file with headings labelled 'e' and 'n' and output latlons
# call script by command-line: 'RScript bng_to_latlon.R {bng_filename.csv}'
require(rgdal)
args=(commandArgs(TRUE))
datain = read.csv(args[1])
bng = datain
coordinates(bng) = ~ e + n
bng@proj4string = CRS("+init=epsg:27700")
latlon = spTransform(bng, CRS("+proj=longlat +datum=WGS84"))
@geotheory
geotheory / server.R
Created September 5, 2014 16:57
Basic Shiny app with Leaflet & ggplot
library(shiny)
library(rCharts)
library(ggplot2)
data(uspop2000, package = 'leaflet')
base_df <- head(uspop2000[which(uspop2000$State == "AL"), ])
shinyServer(function(input, output){
output$myChart <- renderMap({
@geotheory
geotheory / alluvial_ts_example.R
Last active August 29, 2015 14:10
Alluvial_ts example
require(alluvial)
require(reshape2)
d <- read.table(text="Class,1991,2003,2005,2009,2011,2013
1,818,604,601,570,563,556
2,183,147,145,143,142,150
3,40,55,55,55,55,50
4,48,70,81,76,85,99
5,126,140,142,148,155,153
6,396,566,568,566,525,508
@geotheory
geotheory / server.R
Created December 2, 2014 17:12
Shiny PDF output problem
require(shiny)
library(ggplot2)
shinyServer(function(input, output) {
datasetInput <- reactive({
switch(input$dataset,
"rock" = rock,
"pressure" = pressure,
"cars" = cars)
})
library(shiny)
runApp(list(
ui = fluidPage(downloadButton('foo')),
server = function(input, output) {
plotInput = reactive({
plot(1:10)
})
output$foo = downloadHandler(
filename = 'test.pdf',
content = function(file) {
@geotheory
geotheory / server.R
Last active August 29, 2015 14:11
Output (non-ggplot) Shiny plots to PDF
library(shiny)
shinyServer(
function(input, output) {
plotInput <- reactive({
if(input$returnpdf){
pdf("plot.pdf", width=as.numeric(input$w), height=as.numeric(input$h))
plot(rnorm(sample(100:1000,1)))
dev.off()
}
@geotheory
geotheory / server.R
Last active August 29, 2015 14:11
R Shiny stability testing
require(shiny)
# inbuilt dataset
diamonds = ggplot2::diamonds[,c(1,5,7)]
# csv datasets to input via front-end
for(i in 1:3){
dat <- diamonds[sample(1:nrow(diamonds), 200),]
write.table(dat, paste0('dat',i,'.csv'), sep=',',row.names=F, col.names=T)
}
@geotheory
geotheory / gist:e1275221680c668485b0
Created January 1, 2015 23:23
PHP Simple HTML DOM Parser - comma test
<!DOCTYPE html>
<html>
<head>
<title>Comma-URL test</title>
</head>
<body>
<h1>Comma-URL test</h1>
<p>Let's try and parse: http://en.m.wikipedia.org/wiki/National_Register_of_Historic_Places_listings_in_San_Francisco,_California</p>
<?php
@geotheory
geotheory / stacked_geom_bar.R
Last active August 29, 2015 14:13
ggplot geom_bar: stack and center
require(ggplot2); require(tidyr); require(dplyr)
dat = data.frame(grp=letters[1:3], very_bad=c(4,2,1), bad=c(3,4,2), good=c(2,3,4), very_good=c(1,1,3))
(d = dat %>% tidyr::gather(rating, value, very_bad:very_good) %>%
mutate(rating = ordered(rating, levels=c('very_bad','bad','good','very_good'))))
ggplot() +
geom_bar(data = subset(d, rating %in% c('very_bad','bad')),aes(grp, -value, fill=rating), position="stack", stat="identity") +
geom_bar(data = subset(d, !rating %in% c('very_bad','bad')), aes(grp, value, fill=rating), position="stack", stat="identity") +
scale_fill_manual(values=colorRampPalette(c('red','grey','green'))(4)) +