###ESTABLECIENDO LAS LIBRERIAS PARA MAPAS
library(sf)
## Warning: package 'sf' was built under R version 4.0.3
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(purrr)
## Warning: package 'purrr' was built under R version 4.0.3
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.0.3
## -- Attaching packages --------------------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.3 v dplyr 1.0.4
## v tibble 3.0.3 v stringr 1.4.0
## v tidyr 1.1.1 v forcats 0.5.0
## v readr 1.3.1
## Warning: package 'ggplot2' was built under R version 4.0.3
## Warning: package 'dplyr' was built under R version 4.0.3
## -- Conflicts ------------------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(ggplot2)
library(ggrepel)
## Warning: package 'ggrepel' was built under R version 4.0.3
library(readxl)
##Estableciendo y cargando la data
##darse cuenta que las comillas van solo al inicio y al final y los "/" van hacia la derecha
##ver que es un shapefile
peru_d <- st_read ("D:/PRACTICANDO R STUDIO/DATA PARA PRACTICAR/Data para mapa/DEPARTAMENTOS.shp")
## Reading layer `DEPARTAMENTOS' from data source `D:\PRACTICANDO R STUDIO\DATA PARA PRACTICAR\Data para mapa\DEPARTAMENTOS.shp' using driver `ESRI Shapefile'
## Simple feature collection with 25 features and 4 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -81.32823 ymin: -18.35093 xmax: -68.65228 ymax: -0.03860597
## geographic CRS: WGS 84
peru_d
## Simple feature collection with 25 features and 4 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: -81.32823 ymin: -18.35093 xmax: -68.65228 ymax: -0.03860597
## geographic CRS: WGS 84
## First 10 features:
## IDDPTO DEPARTAMEN CAPITAL FUENTE geometry
## 1 01 AMAZONAS CHACHAPOYAS INEI MULTIPOLYGON (((-77.81211 -...
## 2 02 ANCASH HUARAZ INEI MULTIPOLYGON (((-77.64692 -...
## 3 03 APURIMAC ABANCAY INEI MULTIPOLYGON (((-73.74632 -...
## 4 04 AREQUIPA AREQUIPA INEI MULTIPOLYGON (((-71.98109 -...
## 5 05 AYACUCHO AYACUCHO INEI MULTIPOLYGON (((-74.34843 -...
## 6 06 CAJAMARCA CAJAMARCA INEI MULTIPOLYGON (((-78.70034 -...
## 7 07 CALLAO CALLAO INEI MULTIPOLYGON (((-77.13521 -...
## 8 08 CUSCO CUSCO INEI MULTIPOLYGON (((-72.9728 -1...
## 9 09 HUANCAVELICA HUANCAVELICA INEI MULTIPOLYGON (((-74.57118 -...
## 10 10 HUANUCO HUANUCO INEI MULTIPOLYGON (((-76.00486 -...
names(peru_d)
## [1] "IDDPTO" "DEPARTAMEN" "CAPITAL" "FUENTE" "geometry"
##Primer mapa: solo muestra los lÃmites departamentales
ggplot(data = peru_d) +
geom_sf() ##geoms_sf () ya indica que una variable de geometrÃa y eso hay en nuestra data

##Graficar solo un departamento: graficaremos Arequipa
ggplot(data = peru_d %>% ##de la data peru_d %>%
filter(DEPARTAMEN == "LAMBAYEQUE")) + #filtrame el departamento de Lambayeque
geom_sf()

#ggplot(data = peru_d %>%
#filter(IDDPTO == "06")) +
#geom_sf()
###SEGUNDA DATA PARA UNIR
data <- read_excel("D:/PRACTICANDO R STUDIO/DATA PARA PRACTICAR/Ejemplo de departamentos para mapa.xlsx")
##Creando un data frame con un elemento en común
##MERGE UNE LAS DATAS, by.x es la variable de la primera data en común y by.y es de la segunda data
mapa_final <- merge(peru_d, data, by.x = "DEPARTAMEN", by.y = "Departamentos", all.x = TRUE)
ggplot(mapa_final) +
geom_sf(aes(fill = Frecuencia ))+
labs(title = "TITULO",
caption = "Fuente: Enaho (2018)
Elaboración propia",
x="Longitud",
y="Latitud")+
scale_fill_continuous(guide_legend(title = "TITULO DE LA LEYENDA"))

ggplot(mapa_final) +
geom_sf(aes(fill = Frecuencia ))+
labs(title = "TITULO",
caption = "Fuente: Enaho (2018)
Elaboración propia",
x="Longitud",
y="Latitud")+
scale_fill_continuous(guide_legend(title = "TITULO DE LA LEYENDA")) -> ejem1
library(plotly)
## Warning: package 'plotly' was built under R version 4.0.3
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
ggplotly(ejem1)