Elections cantonales 2008

Nous récupérons les tables sur https://www.data.gouv.fr/fr/datasets/elections-cantonales-2008-resultats-572051/.

Nous importons les deux tables qui correspondent chacune à un tour.

cantonale2008_tour1 <- read.csv2("canton_2008_tour1.csv", sep=";", header = TRUE )
cantonale2008_tour2 <- read.csv2("canton_2008_tour2.csv", sep=";", header = TRUE )

On enlève le DOM-TOM

# Pour le premier tour

cantonale2008_tour1 <- cantonale2008_tour1[-c((which(cantonale2008_tour1[,1]=="ZA")),
                                (which(cantonale2008_tour1[,1]=="ZB")),
                                (which(cantonale2008_tour1[,1]=="ZC")),
                                (which(cantonale2008_tour1[,1]=="ZD")),
                                (which(cantonale2008_tour1[,1]=="ZM"))),]


# Pour le deuxième tour

cantonale2008_tour2 <- cantonale2008_tour2[-c((which(cantonale2008_tour2[,1]=="ZA")),
                                (which(cantonale2008_tour2[,1]=="ZB")),
                                (which(cantonale2008_tour2[,1]=="ZC")),
                                (which(cantonale2008_tour2[,1]=="ZD")),
                                (which(cantonale2008_tour2[,1]=="ZM"))),]

On met le bon nombre de chiffres pour le code département et le code commune

# Pour le premier tour

cantonale2008_tour1[,1] <- as.character(cantonale2008_tour1[,1])
cantonale2008_tour1[,5] <- as.character(cantonale2008_tour1[,5])

for (i in 1:length(cantonale2008_tour1[,1])){
  if (nchar(cantonale2008_tour1[i,1])==1){
    cantonale2008_tour1[i,1] <- paste(c("0"), cantonale2008_tour1[i,1], sep="")
  }
  if (nchar(cantonale2008_tour1[i,5])==1){
    cantonale2008_tour1[i,5] <- paste(c("00"), cantonale2008_tour1[i,5], sep="")
  }
  if (nchar(cantonale2008_tour1[i,5])==2){
    cantonale2008_tour1[i,5] <- paste(c("0"), cantonale2008_tour1[i,5], sep="")
  }
}


# Pour le deuxième tour

cantonale2008_tour2[,1] <- as.character(cantonale2008_tour2[,1])
cantonale2008_tour2[,5] <- as.character(cantonale2008_tour2[,5])

for (i in 1:length(cantonale2008_tour2[,1])){
  if (nchar(cantonale2008_tour2[i,1])==1){
    cantonale2008_tour2[i,1] <- paste(c("0"), cantonale2008_tour2[i,1], sep="")
  }
  if (nchar(cantonale2008_tour2[i,5])==1){
    cantonale2008_tour2[i,5] <- paste(c("00"), cantonale2008_tour2[i,5], sep="")
  }
  if (nchar(cantonale2008_tour2[i,5])==2){
    cantonale2008_tour2[i,5] <- paste(c("0"), cantonale2008_tour2[i,5], sep="")
  }
}

Nous créons le code INSEE avec le code commune et le code département pour chaque commune

# Pour le premier tour

cantonale_tour1 <- data.frame("INSEE_COM"=paste(cantonale2008_tour1[,1],cantonale2008_tour1[,5], sep=""), "Abstention" = cantonale2008_tour1[,9])

# On enlève les doublons
cantonale_tour1 <- cantonale_tour1[!duplicated(cantonale_tour1[,1]),]

# Pour le deuxième tour

cantonale_tour2 <- data.frame("INSEE_COM"=paste(cantonale2008_tour2[,1],cantonale2008_tour2[,5], sep=""), "Abstention" = cantonale2008_tour2[,9])

# On enlève les doublons
cantonale_tour2 <- cantonale_tour2[!duplicated(cantonale_tour2[,1]),]

Affichage du taux d’abstention par commune

library(maps)
library(rgdal)  # Lire et reprojeter les cartes
library(sp)
library(plotrix)   # Créer des échelles de couleurs
library(classInt)  # Affecter ces couleurs aux données

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


# Pour le premier tour

# Jointure entre cantonale_tour1 et commune pour récupérer les coordonnées géographiques pour le premier tour
communes <- merge(commune,cantonale_tour1, by.x='INSEE_COM',by.y='INSEE_COM')


# Lecture des limites des communes
frontiere_commune <- readOGR(dsn="F:/M2 MIMSE/Semestre 2/Projet informatique/COMMUNE", layer="LIMITE_COMMUNE")
frontiere <- frontiere_commune[frontiere_commune$NATURE %in% 'Limite de commune',]

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",]


col <- findColours(classIntervals(
            communes@data[,19], 100, style="quantile"),
            smoothColors("white",98,"red"))

# Légende
leg <- findColours(classIntervals(
            round(communes@data[,19]),6,style="quantile"),
            smoothColors("white",3,"red"),
            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(communes,col=col, border=col,lwd=.1, add=TRUE)
title(main = "Taux d'abstention par commune pour le premier tour", sub= "Elections cantonales 2008")

# Affichage de la légende
legend("bottomleft",fill=attr(leg, "palette"), cex=0.8,
    legend=gsub("\\.",",",names(attr(leg,"table"))),
    title = "Taux d'abstention :")


# Pour le deuxième tour

# Jointure entre cantonale_tour2 et commune pour récupérer les coordonnées géographiques pour le deuxième tour
communes2 <- merge(commune,cantonale_tour2, by.x='INSEE_COM',by.y='INSEE_COM')

col2 <- findColours(classIntervals(
            communes2@data[,19], 100, style="quantile"),
            smoothColors("white",98,"red"))

# Légende
leg2 <- findColours(classIntervals(
            round(communes2@data[,19]),6,style="quantile"),
            smoothColors("white",3,"red"),
            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(communes2,col=col2, border=col2,lwd=.1, add=TRUE)
title(main = "Taux d'abstention par commune pour le deuxième tour", sub= "Elections cantonales 2008")

# Affichage de la légende
legend("bottomleft",fill=attr(leg2, "palette"), cex=0.8,
    legend=gsub("\\.",",",names(attr(leg2,"table"))),
    title = "Taux d'abstention :")

Vérification de la corrélation entre les élections cantonales et les élections municipales

# Test statistique

Cant_tour1 <- read.csv2("Cant_tour1.csv",sep=";",header=TRUE)
Cant_tour1 <- Cant_tour1[,-1]
View(Cant_tour1)

tour1bis <- read.csv2(file = 'Tour1.csv',sep=",", header=TRUE)
tour1bis <- tour1bis[,-1]

abstention_par_com <- data.frame("INSEE_COM"=tour1bis[,3],"abstent"=tour1bis[,20])

corr_abstentions <- merge(Cant_tour1,abstention_par_com,intersect(names(Cant_tour1),names(abstention_par_com)))
View(corr_abstentions)

names(corr_abstentions)[2:3] <- c("Abst_cant","Abst_munic") 



corr_abstentions[,3] <- as.numeric(as.character(corr_abstentions[,3]))
cor(corr_abstentions[,2],corr_abstentions[,3])
correlation <- cor.test(corr_abstentions[,2],corr_abstentions[,3])
capture.output(correlation, file = "test_cantonale.txt")


# Régression linéaire

dev.off()
plot(corr_abstentions[,2]~corr_abstentions[,3],xlab="Absention aux municipales",ylab="Abstention aux cantonales",main="Régression linéaire",sub="Lien entre municipales et cantonales 2008")
abline(lm(corr_abstentions[,2]~corr_abstentions[,3]),col=2, lwd=2)