How put base map in ggplot2 like can make in leaflet?

Hi community,

I'm want to make a map with ggplot2 for print on a sheet of paper. But I don't now how get the correct base map.

I'm try to reproduce the situation with leaflet but I don't now if is possible make this map in ggplot2. I'm try with ggmap but was necessary pay to access the API I don't like this.

NEW_MEX <- structure(list(County = c("Luna", "Luna", "Luna", "Luna", "Luna", 
"Luna", "Dona Ana", "Dona Ana", "Dona Ana"), New_latitude = c(32.0375, 
32.0333333333333, 32.0333333333333, 31.175, NA, 32.04444, NA, 
32.22917, 31.8), longitude = c("107.37.30", "107.38.40", "107.38.50", 
"107.36.50", NA, "107.35", NA, "106.34", "107.03"), New_longitude = c(-107.625, 
-107.6444, -107.6472, -107.6139, NA, -107.583333333333, NA, -106.566666666667, 
-107.05)), row.names = c(NA, -9L), class = c("tbl_df", "tbl", 


adme <- geodata::gadm(country = 'USA', level = 2, path = '.') # USA MAP
adme <- st_as_sf(adme)

adme_1 <- adme %>%
  filter(NAME_1 == 'New Mexico' & NAME_2 %in% c('Luna', 'Dona Ana')) # Filter some's regions

popup <- paste0("<strong>Admin. division level 1: </strong>", 

leaflet() %>% 
  addProviderTiles("Esri.WorldTopoMap") |> 
  leaflet::addPolygons(data=adme_1, weight = 1, fillColor = "#FEF800",
            fillOpacity = 0.02, opacity = 1,smoothFactor = 0.1,popup=popup) |>
  leaflet::addMarkers(lat=NEW_MEX$New_latitude, lng=NEW_MEX$New_longitude,data = NEW_MEX,
                      popup =paste("<b> County:</b>",NEW_MEX$County,"<br>",
                                   "<b> Collector_NUM:</b>",NEW_MEX$`Collector and number`,"<br>",
                                   "<b> Latitude:</b>",NEW_MEX$New_latitude,"<br>",
                                   "<b> Longitude:</b>",NEW_MEX$New_longitude,"<br>"))

The idea is make zoom in but with ggplot2 for get a better resolutions of this map.


Check Making Contour Maps in R it may help

1 Like

I'm check this link and is an excellent guide.

But when put my coordinates I get this error in this part:

# Produces an OpenStreetMap object (and caches it)
Base_osm <- OpenStreetMap::openmap(c(bbox[["ymin"]],
#Error in if (ntiles >= minNumTiles) break else zoom <- as.integer(zoom +  : 
#missing value where TRUE/FALSE needed

Im add the zoom T and F, and show this error.

Base_osm <- OpenStreetMap::openmap(c(bbox[["ymin"]],
                                   zoom = F)
# Error in minY:maxY : NA/NaN argument

In addition, I think that is necessary change this initial part with my evaluation zone, I'm not sure. I dont know how get this for my zone New Mexico (USA)

#  A few handy crs codes

googlecrs <- "EPSG:4326"
webcrs <- "EPSG:3857"
localcrs <- "EPSG:26915" # UTM 15N NAD83
localUTM <- "EPSG:32615" # a WGS84 similar to EPSG:26915 - same UTM zone

Generally, the basemap generators expect lat long coordinates (I don't know why they call them X, Y). But if you want to use X-Y data, or lat long data from a different CRS code, it will need to be transformed. Making maps is not simple - it always involves some understanding of geodetics. To find your zone I'd recommend some Googling. For example, if you want to use a UTM projection, most of New Mexico is in UTM 13. But if you have everything in lat long you may not need to transform any coordinates. My example has all those EPSG codes because I needed to convert data to X-Y to do gridding and contouring, and then convert back to lat-long for posting the data.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.