Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lucasgautheron/2fe997e668ae284ab6dd894f9100e30f to your computer and use it in GitHub Desktop.
Save lucasgautheron/2fe997e668ae284ab6dd894f9100e30f to your computer and use it in GitHub Desktop.
args<-commandArgs(TRUE)
list.of.packages <- c("sp", "rgdal", "RColorBrewer", "classInt", "ggplot2", "ggmap", "maptools", "raster", "rgeos", "foreign", "plyr", "dplyr", "rgeos", "utils")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages, repos="https://cloud.r-project.org/", type="source")
# install.packages("sp")
# install.packages("spatialEco")
# install.packages("rgdal")
# install.packages("RColorBrewer")
# install.packages("classInt")
# install.packages("ggplot2")
# install.packages("ggmap")
# install.packages("maptools")
# install.packages("raster")
# install.packages("rgeos")
# install.packages("foreign")
# install.packages("dplyr")
# install.packages("rgeos")
# install.packages("utils")
library("sp")
library("rgdal")
library("RColorBrewer")
library("classInt")
library("ggplot2")
library("ggmap")
library("maptools")
library("raster")
library("rgeos")
library("foreign")
library("plyr")
library("dplyr")
library("rgeos")
library("utils")
##### choose the number of bins #####
nobins=10
##### import shapefile #####
path= "maps/departements"
shapefile <- readOGR(dsn = path, layer="departements-20140306-100m", stringsAsFactors=FALSE)
shapefile <- spTransform(shapefile, CRS( "+init=epsg:4326" ) )
shapefile <- shapefile[nchar(shapefile@data$code_insee)==2,]
shapefile@data$dpt <- shapefile@data$code_insee
##### import csv #####
csv <- read.csv("tce.csv", stringsAsFactors = F)
##### merge avec les scores #####
shapefile@data <- left_join(shapefile@data, csv, by="dpt")
##### dep pourri ######
# shapefile@data[is.na(shapefile@data )] <- 0
shapefile <- shapefile[!(shapefile@data$code_insee)=="61",]
##### selectionner les attributes #####
#shapefile@data$attributes <- shapefile@data[[args[1]]]
shapefile@data$attributes <- 100*shapefile@data$SCORE_MELE_PRES_2017
###### choisir les bins ####
shapefile@data$bin <- cut(shapefile@data$attributes,
breaks=c(quantile(shapefile@data$attributes, probs = seq(0, 1, by = 1/nobins))),
include.lowest = TRUE,
dig.lab = 2)
# labels for the legend
lev = levels(shapefile@data$bin)
lev2 <- gsub("\\,", " à ", lev)
lev3 <- gsub("\\]$", "", lev2)
lev4 <- gsub("\\(|\\)", " ", lev3)
lev5 <- gsub("^\\[", " ", lev4)
my.levels <- lev5
# Create a function to generate a continuous color palette
rbPal <- colorRampPalette(c('white', args[2]))
shapefile@data$Col <- rbPal(nobins)[as.numeric(cut(shapefile@data$attributes,
breaks=c(quantile(shapefile@data$attributes, probs = seq(0, 1, by = 1/nobins))),
include.lowest = TRUE
))]
# Plot
pdf(paste('tce/', args[1], ".pdf", sep=''),width=15,height=10,paper='special')
plot(shapefile, col = shapefile@data$Col)
legend("topleft", fill = rbPal(nobins),
legend = my.levels,
col=rbPal(nobins),
cex=1.75
)
dev.off()
#View(shapefile@data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment