1

I have a dataset with three variables related to coordinates: UTMX, UTMY and HUSO.

I'd like to create a map where all the points were shown.

Here an example of the dataframe:

    x       y       huso
1   474731  4539196 30  
2   518127  4539351 30  
3   488067  4521530 30  
4   459943  4528829 30  
5   497732  4539816 30  
6   502623  4531443 30  

I have tried to plot the coordenates:

coordenadas_UTM <- SpatialPointsDataFrame(coords=coordenadas,
                                         data=coordenadas,
                                         proj4string = CRS("+proj=utm +zone=30 +north +ellps=WGS84 +units=m +no_defs"))


m <- leaflet(coordenadas) %>%  setView(lat  = 40.416775, lng = -3.703790, zoom = 5) %>% 
   addProviderTiles(providers$CartoDB.Positron)

m %>% addMarkers(lng=coordenadas_UTM@data$x, lat=coordenadas_UTM@data$y )

I have two things that I think they are not ok. Transformation from UTM to lat/lng...I tried some packages but I didn't get it working and I can't download any map from Google or OpenStreetMaps.

Does anyone build a map with any packages (ggmap, ggplot, leaflet...) with UMT coordenates?

Thank you in advanced.

1 Answer 1

2

You need to find out what the EPSG code of the CRS the data is in then transform it to WGS84 which has an EPSG code of 4326. You can use the sf package for this:

library(sf)
library(dplyr)
library(leaflet)

data <- data.frame(id= c(1,2,3,4,5,6), x= c(474731,518127,488067,459943,497732,502623), 
                                       y=c(4539196,4539351,4521530,4528829,4539816,4531443)) %>%
  st_as_sf(coords=c("x", "y"), crs=32630) %>%
  st_transform(4326) 
  
data %>%
  leaflet() %>%
  addTiles() %>%
  addMarkers()
1
  • 1
    It worked perfectly! Thank you, I read abour this function st_as_sf but I didn't know the crs value. Thank you again!
    – Enrique
    Commented Apr 26, 2023 at 21:22

Not the answer you're looking for? Browse other questions tagged or ask your own question.