Fuente: kontur-population-dataset

Referencia:https://github.com/gkaramanis/30DayMapChallenge/tree/main/2022 Datos https://data.humdata.org/dataset/kontur-population-dataset Tierra: https://www.naturalearthdata.com/downloads/10m-physical-vectors/

library(tidyverse)
library(sf)        # for manipulation of simple features objects
library(rgdal)
library(dplyr)
library(glue)
library(raster)
sf::sf_use_s2(FALSE)

rutacam<-"D:/Documents/Claudia/Midropbox/Investigacion y escritos/mapasnacionales/"
limitesbox<-read.csv(paste0(rutacam,"limites.txt"),header=TRUE, sep=",",encoding="latin")

names(limitesbox)<-c("long","lat")

sps <- limitesbox %>%
  st_as_sf(coords = c("long", "lat")) %>%
  summarize(geometry = st_union(geometry)) %>%
  st_convex_hull()

st_crs(sps) <- CRS("+proj=longlat +datum=WGS84 +no_defs")

nomfil<-"kontur_population.gpkg"

pop <- read_sf(nomfil, query = "select * from population where population >= 1000")

tierra<-read_sf("D:/Documents/Claudia/Midropbox/Investigacion y escritos/mapasnacionales/ne_10m_land/ne_10m_land.shp")
st_crs(tierra) <- CRS("+proj=longlat +datum=WGS84 +no_defs")
pop_ortho <- pop %>% 
  st_transform(4326)
sps_ortho <- sps %>% 
  st_transform(4326)
tierra_ortho2<-na.omit(tierra)

tierra_ortho <- tierra_ortho2 %>% 
  st_transform(4326)



recorteosmlayer <-pop_ortho[sps_ortho , ] 
recortetierralayer <-tierra_ortho[sps_ortho , ] 
lonmin<-min(limitesbox$long)
lonmax<-max(limitesbox$long)
latmin<-min(limitesbox$lat)
latmax<-max(limitesbox$lat)
xlim = c(lonmin, lonmax)
ylim = c(latmin, latmax)




mapamx <- ggplot()  + 
   geom_sf(data = recortetierralayer,aes(fill=scalerank),show.legend = FALSE)+
  labs(title = "", x = "", y = "")+
  coord_sf(xlim, ylim)+
paletteer::scale_color_paletteer_c("grDevices::RdYlGn")+
  ggnewscale::new_scale_color() +
  geom_sf(data = recorteosmlayer,aes(color=population),show.legend = FALSE)+
  labs(title = "", x = "", y = "")+
paletteer::scale_color_paletteer_c("grDevices::Spectral")+
  theme(
    panel.background = element_rect(fill = "transparent"), # bg of the panel
    plot.background = element_rect(fill = "white", color = NA), # bg of the plot
    panel.grid.major = element_blank(), # get rid of major grid
    panel.grid.minor = element_blank(), # get rid of minor grid
    legend.background = element_rect(fill = "transparent"), # get rid of legend bg
    legend.box.background = element_rect(fill = "transparent"), # get rid of legend panel bg
    legend.key = element_rect(fill = "transparent", colour = NA), # get rid of key legend fill, and of the surrounding
    axis.line = element_line(colour = "transparent"),
    axis.text.x=element_blank(), axis.ticks.x = element_blank(),
  axis.text.y = element_blank(), axis.ticks.y = element_blank())+ coord_sf(xlim, ylim)

mapamx

ggsave("kontour_map.png", width = 400, height = 400, units = "mm", dpi = 300,limitsize = FALSE)

#ggsave("kontour_map.png", width = 1000, height = 1000, units = "mm", dpi = "retina",limitsize = FALSE,bg = "white")