library('rgdal')
library('plotrix')
library('classInt')

Tour1 <- read.csv2('Tables/Tour1_M_B.csv',dec='.',sep=',')

# Appel du fichier qui contient le contour des communes
Communes <- readOGR(dsn = 'COMMUNE',layer = 'COMMUNE')
Communes <- merge(Communes,Tour1,by.x = 'INSEE_COM',by.y = 'CODE_INSEE')
Communes <- merge(Communes,pred_aqui,by.x = 'INSEE_COM',by.y = 'insee')

# Appel du fichier qui contient les frontières françaises et les frontières régionales
Frontieres <- readOGR(dsn="COMMUNE",  layer="LIMITE_COMMUNE")
Frontieres <- Frontieres[Frontieres@data$NATURE %in% c('Frontière internationale','Limite côtière',"Limite de région"),]

# Découpage en couleur par rapport aux taux d'abstention réel
col <- findColours(classIntervals(
  Communes$TX_ABSTENTION[which(Communes@data$CODE_REG == 75)], 100, style="quantile"),
  smoothColors('#EFEBD2',98,"orange"))

leg <- findColours(classIntervals(
  round(Communes$TX_ABSTENTION[which(Communes@data$CODE_REG == 75)],0), 6, style="quantile"),
  smoothColors("#EFEBD2",2,"orange"),
  under="moins de", over="plus de", between="–",
  cutlabels=FALSE)

# Découpage en couleur par rapport aux taux d'abstention prédit
col_pred <- findColours(classIntervals(
  Communes$pred[which(Communes@data$CODE_REG == 75)], 100, style="quantile"),
  smoothColors('#EFEBD2',98,"orange"))

leg_pred <- findColours(classIntervals(
  round(Communes$pred[which(Communes@data$CODE_REG == 75)],0), 6, style="quantile"),
  smoothColors("#EFEBD2",2,"orange"),
  under="moins de", over="plus de", between="–",
  cutlabels=FALSE)

# Affichage de la carte avec les taux réels
dev.new()
plot(Communes[which(Communes@data$CODE_REG == 75),],   col=col, border=col, lwd=0.1)
plot(Frontieres, col="#666666", lwd=1, add=TRUE)
legend('topleft',fill=attr(leg, "palette"),
       legend=gsub("\\.", ",", names(attr(leg,"table"))),
       title = "Taux d'abstention par communes:",bg = 'white',box.col = 0)

# Affichage de la carte avec les taux prédits
dev.new()
plot(Communes[which(Communes@data$CODE_REG == 75),],   col=col_pred, border=col_pred, lwd=0.1)
plot(Frontieres, col="#666666", lwd=1, add=TRUE)
legend('topleft',fill=attr(leg_pred, "palette"),
       legend=gsub("\\.", ",", names(attr(leg_pred,"table"))),
       title = "Taux d'abstention par communes:",bg = 'white',box.col = 0)