Mapa de ocurrencia de especies - adamianpz/ocurrence-species GitHub Wiki
Paso 1: Cargar las librerias
library(ggmap)
library(ggplot2)
library(raster)
library(maptools)
library("rnaturalearth")
library("rnaturalearthdata")
library("sf")
library(readxl)
library(ggspatial)
Paso 2: Cargar coordenadas necesarias
crosso<-read_excel(file.choose(),sheet = "crosso") #croso es el nombre que asignare a mis datos en R y "crosso" es la hoja dentro del excel que leere.
crosso2<-st_as_sf(crosso,coords=c("longitude","latitude"),crs=4326,agr="constant")
Paso 3: Cargar los mapas
mapa <- borders("world", regions = c("Brazil", "Uruguay", "Argentina", "French Guiana", "Suriname", "Colombia", "Venezuela",`` ``"Bolivia", "Ecuador", "Chile", "Paraguay", "Peru", "Guyana", "Panama", "Costa Rica", `` ``"Nicaragua", "Honduras", "El Salvador", "Belize", "Guatemala", "Mexico", "Trinidad and Tobago",`` ``"Caribe", "Puerto Rico", "Dominican Republic", "Haiti", "Jamaica", "Cuba", "Bahamas", "Antiles",`` ``"Dominica", "Saba"), `` ``fill = "white", colour = "black") #nos cagara un mapa con las fronteras de los paises de la lista (America)
peru<- ne_states(country = "peru",returnclass = "sf") #nos cargara las provincias de Peru
Podemos visualizar el mapa con:
ggplot() + mapa + theme_bw() + xlab("Longitude (decimals)") + ylab("Latitude (decimals)") +
theme(panel.border = element_blank(), panel.grid.major = element_line(colour = "grey80"), panel.grid.minor = element_blank())
Paso 4: Ploteamos nuestros puntos en los mapas
ggplot(data=peru) + mapa +scale_shape_manual(values = c(15,19,17,23))+geom_sf(fill="white") +geom_sf(data=crosso2,size=5,aes(shape=species),colour="black",fill="white",stroke=2)+coord_sf(xlim = c(-82, -60), ylim = c(-19, -0), expand = FALSE)+annotation_scale(location = "bl", width_hint = 0.5) + annotation_north_arrow(location = "bl", which_north = "true", pad_x = unit(0.75, "in"), pad_y = unit(0.5, "in"), style = north_arrow_fancy_orienteering)+theme(panel.grid.major = element_line(color = gray(.5), linetype = "dashed", size = 0.5), panel.background = element_rect(fill = "white"))+theme_bw()+ theme(legend.position=c(.95,.95),legend.justification=c("right","top"),legend.box.just="right",legend.margin=margin(6,6,6,6),legend.box.background=element_rect(color="black",size = 2), legend.key=element_rect(fill="white",size = 1),legend.text=element_text(size = 20,colour = "black",face="italic"),legend.title=element_blank())
- #data=peru carga el mapa con las provincias peruanas
- #mapa jala el mapa de America
- #scale_shape_manual nos permite escoger entre diferentes iconos para cada especie ploteada ver enlace
- #geom_sf suma los puntos de nuestra base de datos "crosso2" y nos permite modificar diferentes atributos de color, tamaño, etc
- #coord_sf nos permite mover el mapa, los limites colocados funcionan bien para posicionar el mapa en Peru
- #annotation scale y annotation_north_arrow nos permite incluir en el mapa una flecha de norte y la escala
- #theme nos permite modificar diferentes atributos de la forma del mapa incluyendo el background y la leyenda, aqui se pueden modificar desde la posicion de la leyenda hasta el color y tamaño del texto.
Referencias/paginas utiles
- https://ggplot2.tidyverse.org/articles/ggplot2-specs.html#colour-and-fill-3
- https://ggplot2.tidyverse.org/reference/geom_point.html
- https://www.r-spatial.org/r/2018/10/25/ggplot2-sf.html
- https://cran.r-project.org/web/packages/rnaturalearth/vignettes/rnaturalearth.html
- https://ropensci.org/tutorials/rnaturalearth_tutorial/
- https://eriqande.github.io/rep-res-web/lectures/making-maps-with-R.html
- https://github.com/nacmarino/Maps/blob/master/Scripts/Latin%20America%20Map.R
- https://www.r-graph-gallery.com/239-custom-layout-legend-ggplot2.html
- https://www.datanovia.com/en/blog/ggplot-colors-best-tricks-you-will-love/