Taux d’abstentions par département

Nous allons afficher le taux d’abstentions par département

# Nous reprenons la table tour1bis qui affichait le premier tour des élections municipales
tour1bis <- read.csv2(file = 'Tour1.csv',sep=",", header=TRUE)

abs_dep <- tour1bis[,c(3,19,20)]

abs_dep[,1]<- as.factor(abs_dep[,1])

# Nous mettons les données en fonction des départements et non en fonction des communes
abstention_par_dep <- matrix(rep(0,length(levels(abs_dep[,1]))*3),nrow=length(levels(abs_dep[,1])))

for(i in 1:length(levels(abs_dep[,1]))){
  sub <- matrix()
  sub <- subset(abs_dep,Code.Département==levels(abs_dep[,1])[i])
  abstention_par_dep[i,] <- c(levels(abs_dep[,1])[i],apply(sub[,2:3],2,sum))
}

# Nous calculons le taux d'abstentions par département
abstention_par_dep <- data.frame(abstention_par_dep,"Taux d'abstention"=(as.numeric(abstention_par_dep[,3])/as.numeric(abstention_par_dep[,2]))*100)

names(abstention_par_dep)[1:3] <-c("DEP","NbInscrits","NbAbsten")

# Nous rajoutons des 0 sur les codes départements à un chiffre
abstention_par_dep$DEP <- as.character(abstention_par_dep$DEP)
for (i in 1:length(abstention_par_dep[,1])){
  if(nchar(abstention_par_dep[i,1])==1){
    abstention_par_dep[i,1]=paste(c("0"),abstention_par_dep[i,1],sep="")
  }
}

Tracé du taux d’abstentions par département

library(maps)
library(rgdal)
library(sp)
library(plotrix)   # Créer des échelles de couleurs
library(classInt)

# Lecture des communes
departement <- readOGR(dsn="F:/M2 MIMSE/Semestre 2/Projet informatique/DEPARTEMENT", layer="DEPARTEMENT")

# Jointure entre chomage_par_dep et departement pour récupérer les coordonnées géographiques
names(abstention_par_dep)[1] <- c("CODE_DEPT")
departement <- merge(departement,abstention_par_dep[,c(1,4)], by.x='CODE_DEPT',by.y='CODE_DEPT')

# Lecture des limites des communes
frontiere_departement <- readOGR(dsn="F:/M2 MIMSE/Semestre 2/Projet informatique/DEPARTEMENT", layer="LIMITE_DEPARTEMENT")

frontiere <- frontiere_departement[frontiere_departement$NATURE %in% 'Limite de département',]

europe <- readOGR(dsn="F:/M2 MIMSE/Semestre 2/Projet informatique/ne_110m_admin_0_countries", layer="ne_110m_admin_0_countries")
europe <- europe[europe$region_un=="Europe",]

# coloration 
col <- findColours(classIntervals(
  departement@data[,12], 100, style="quantile"),
  smoothColors("white",98,"orange"))

# Légende
leg <- findColours(classIntervals(
  round(departement@data[,12],2),6,style="quantile"),
  smoothColors("white",3,"orange"),
  under="moins de", over="plus de", between="–",
  cutlabels=FALSE)

# Projection en Lambert 93
europe <- spTransform(europe, CRS("+init=epsg:2154"))

# Traçage de la carte
plot(frontiere,  col="#FFFFFF")
plot(europe,  col="#E6E6E6", border="#AAAAAA",lwd=1, add=TRUE)
plot(frontiere,  col="#D8D6D4", lwd=6, add=TRUE)
plot(departement,col=col, border=col,lwd=.1, add=TRUE)
title(main="Taux d'abstentions par département")

legend("topright",fill=attr(leg,"palette"),legend=names(attr(leg,"table")),
       title="Abstention",cex=0.8)