setwd("C:/Users/frank/OneDrive/Escritorio")
library(readxl)
## Warning: package 'readxl' was built under R version 4.1.2
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.1.2
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(rgdal)
## Warning: package 'rgdal' was built under R version 4.1.2
## Loading required package: sp
## Warning: package 'sp' was built under R version 4.1.2
## Please note that rgdal will be retired by the end of 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
## 
## rgdal: version: 1.5-27, (SVN revision 1148)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.2.1, released 2020/12/29
## Path to GDAL shared files: C:/Users/frank/OneDrive/Documentos/R/win-library/4.1/rgdal/gdal
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]
## Path to PROJ shared files: C:/Users/frank/OneDrive/Documentos/R/win-library/4.1/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.4-6
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
## Overwritten PROJ_LIB was C:/Users/frank/OneDrive/Documentos/R/win-library/4.1/rgdal/proj
library(rgeos)
## Warning: package 'rgeos' was built under R version 4.1.2
## rgeos version: 0.5-8, (SVN revision 679)
##  GEOS runtime version: 3.9.1-CAPI-1.14.2 
##  Please note that rgeos will be retired by the end of 2023,
## plan transition to sf functions using GEOS at your earliest convenience.
##  GEOS using OverlayNG
##  Linking to sp version: 1.4-6 
##  Polygon checking: TRUE
library(maptools)
## Warning: package 'maptools' was built under R version 4.1.2
## Checking rgeos availability: TRUE
## Please note that 'maptools' will be retired by the end of 2023,
## plan transition at your earliest convenience;
## some functionality will be moved to 'sp'.
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.1.2
library(sf)
## Warning: package 'sf' was built under R version 4.1.2
## Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1
list.files()
##  [1] "~$allpa_Tesis2.docx"                                                   
##  [2] "~$allpa_TesisAvance2.docx"                                             
##  [3] "~$allpaFrank_Tesis PRIMER CAPITULO.docx"                               
##  [4] "~$allpaFrank_Tesis PRIMER CAPITULO_OBSERVACIONES.docx"                 
##  [5] "~$ELECCIONES2020.xlsx"                                                 
##  [6] "~WRL0005.tmp"                                                          
##  [7] "~WRL2202.tmp"                                                          
##  [8] "1-Link_Geoportales de Perú.pdf"                                        
##  [9] "Capitulo 1"                                                            
## [10] "Capitulo 2"                                                            
## [11] "codigomapas.R"                                                         
## [12] "ComitesUPP.html"                                                       
## [13] "ComitesUPP.nb.html"                                                    
## [14] "ComitesUPP.Rmd"                                                        
## [15] "Dialnet-OrganizacionesPoliticasNoInstitucionalizadasYEstad-5496042.pdf"
## [16] "Diseño Investigación-x"                                                
## [17] "Elecciones_2020.xlsx"                                                  
## [18] "ELECCIONES2006.xlsx"                                                   
## [19] "elecciones2020.html"                                                   
## [20] "elecciones2020.Rmd"                                                    
## [21] "ELECCIONES2020.xlsx"                                                   
## [22] "Entregas"                                                              
## [23] "Entrevistas"                                                           
## [24] "FinalTesis_Huallpa.pdf"                                                
## [25] "Huallpa_TesisAvance2.docx"                                             
## [26] "Limite_departamental.rar"                                              
## [27] "Limite_provincial.rar"                                                 
## [28] "MapaElecciones2006.html"                                               
## [29] "MapaElecciones2006.nb.html"                                            
## [30] "MapaElecciones2006.Rmd"                                                
## [31] "MapaElecciones2011.html"                                               
## [32] "MapaElecciones2011.nb.html"                                            
## [33] "MapaElecciones2011.Rmd"                                                
## [34] "Portada Tesis licenciatura 10-21.docx"                                 
## [35] "Provincias2011.xlsx"                                                   
## [36] "rsconnect"                                                             
## [37] "Seminario 1-x"                                                         
## [38] "SEMINARIO 2"                                                           
## [39] "TESIS FINAL"

Abrimos el archivo

departamentos <- st_read("C:/Users/frank/OneDrive/Escritorio/DEPARTAMENTOS/DEPARTAMENTOS.shp")
## Reading layer `DEPARTAMENTOS' from data source 
##   `C:\Users\frank\OneDrive\Escritorio\DEPARTAMENTOS\DEPARTAMENTOS.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 25 features and 4 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -81.32823 ymin: -18.35093 xmax: -68.65228 ymax: -0.03860597
## Geodetic CRS:  WGS 84
plot(st_geometry(departamentos))

Exportation la data

setwd("C:/Users/frank/OneDrive/Escritorio/PUCP/TESIS")
elecciones2006 <- read_excel("ELECCIONES2020.xlsx")

Merge

ProvinciasMapa <- merge(departamentos,elecciones2006, by.x = "DEPARTAMEN", by.y="REGION")

Creamos Mapa

Mapa <- ggplot(data = ProvinciasMapa) +
  geom_sf(aes(fill=`VOTOS VALIDOS` ), data = ProvinciasMapa, color='black', size=0.3) +
  guides(fill=guide_colorbar(title = "% votos validos (UPP)")) +
  scale_fill_gradient(limits= c(0,100), high = "red", low = "white", guide = "colorbar") +
  ggtitle("Resultados Elecciones Parlamentarias 2020")+
  theme_bw()+
  theme(
    axis.text.x = element_blank(),
    axis.text.y = element_blank(),
    axis.ticks =  element_blank(),
    axis.title = element_blank(),
    panel.border = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank()
  )
Mapa