Skip to content

Instantly share code, notes, and snippets.

View datagistips's full-sized avatar

mathieu rajerison datagistips

View GitHub Profile
library(RCurl)
library(rjson)
whatGoogleSuggestsFor <- function(question, domain="fr") {
# Domain : here, just France or USA
# You could add URLs for some other countries
if (domain == "fr") {
baseU <- "https://www.google.fr/s?gs_rn=45&sclient=psy-ab"
} else {
@datagistips
datagistips / election_choropleth.R
Created June 2, 2012 07:51
Elections Simple Choropleth
library(rgdal)
library(maptools)
library(classInt)
setwd("F:/METHODES/1205_CAMPAGNE_PRES")
#########################
## READING SOURCE DATA ##
#########################
# on intègre les contours des communes GEOFLA
@datagistips
datagistips / elections_distance_coast.R
Created June 2, 2012 07:59
Elections : votes and distance to the coast
## ATTENTION !!: l'utilisation de ce code nécessite d'avoir
# lancé le code précédent (https://gist.github.com/2857180) ,
# en gros d'avoir créé l'objet spatial comm2 avec les résultats du vote.
library(rgdal)
library(spatstat)
library(maptools)
## COERCING DATA for SPATSTAT ##
# on lit le trait de côte téléchargé depuis
@datagistips
datagistips / Isarithmic_Choropleth-election_results.R
Created June 4, 2012 12:40
Isarithmic Choropleth - election results
library(rgdal)
# DATA
# http://professionnels.ign.fr/ficheProduitCMS.do?idDoc=6185461
cities <- readOGR("C:/DATAS/GEOFLA/COMMUNES/COMMUNE.SHP", "COMMUNE")
# http://www.data-publica.com/opendata/13495--resultats-du-deuxieme-tour-de-l-election-presidentielle-2012
votes <- read.csv2("IN/votesResults2012.csv",sep=",", quote="\"")
# VOTES TO SPATIAL DATA "
votes$pc.sar <- votes$results.sarkozy / votes$results.exprimes
@datagistips
datagistips / heatmap_candidats_profession.R
Created June 13, 2012 05:43
HeatMap Profession des candidats aux législatives et Parti
library(gdata)
library(reshape)
library(classInt)
###############
# INTEGRATING #
###############
f <- read.xls("IN/Leg 2012 Candidatures T1 31 05 2012.xls", sheet=1)
nuances <- read.xls("IN/Leg 2012 Candidatures T1 31 05 2012.xls", sheet=2, skip=2, header=FALSE)
@datagistips
datagistips / res_leg_2nd_T.R
Created June 20, 2012 16:27
Fichier des gagnants aux élections législatives 2nd Tour d'après le fichier de Data Publica
library(gdata)
library(stringr)
#Fichier source : http://www.data-publica.com/data/13622--resultat-par-circonscription-du-2nd-tour-des-elections-legislatives-17-juin-2012
res <- read.xls("IN/Leg 2012 Résultats CirLG FE T2.xls") # résultats législatifs
###########################
# RECUPERATION DU GAGNANT #
###########################
@datagistips
datagistips / gist:3315054
Created August 10, 2012 15:29
Sextante QGIS - R script for analysing neighbord relationships below a certain distance threshold
##[datagistips]=group
##polygons=vector
##distance=number 10
##output=output vector
library("spdep")
nb = poly2nb(polygons, snap=distance)
polygons[["nNeigh"]] = card(nb)
@datagistips
datagistips / neighbor.R
Created August 10, 2012 15:38
Neighbord relationships below a certain distance threshold
library(rgdal)
library(spdep)
pol <- readOGR(".", "buildings") # read the data using rgdal
nb <- poly2nb(pol, snap=50) # create a graph. Two buildings are connected if they are 50 meters apart from one another.
pol$nNeigh <- card(nb) # card(nb) gives the neighbor counts
pol$neighGroup <- n.comp.nb(nb)$comp.id # n.comp.nb contains the informations about the disconnected compontents
@datagistips
datagistips / city_names_wordcloud.R
Created September 16, 2012 21:04
city names wordcloud
library(rgdal)
library(wordcloud)
library(reshape)
library(maptools)
library(classInt)
library(FactoMineR)
library(FNN)
### LOAD DATA ###
f <- readOGR(".", "COMMUNE")
creerGrille = function(pol, size=50000) {
r = raster(extend(extent(pol), size)); res(r) = size; r[]=1
grille = rasterToPolygons(r)
grille = grille[which(gIntersects(grille, pol, byid=T)), ]
grille = grille[-which(gTouches(grille, reg, byid=T)), ]
return(grille)
}
extractFrequencies = function(grille, r, values) {