0

I have been trying to convert the latitude and longitude into multipolygon object so that that I can use tmap library to plot it, but I am not able to that. Converting useing st_as_sf is not wroking, can some help me? I am attaching sample data set.

coor<-structure(list(Type = c("Registry", "Registry", "Registry", "Registry", "Platform", "Registry"),`Location of coordinating center` = c("USA","USA", "USA", "USA", "United Kingdom", "United Kingdom"),`3ISO code` = c("USA", "USA", "USA", "USA", "GBR", "GBR"), `WHO region code` = c("AMR","AMR", "AMR", "AMR", "EUR", "EUR"), city = c("Philadelphia","Chicago", "Washington", "Alexandria", "London", "Manchester"), lat = c(32.7761, 41.8373, 38.9047, 38.8185, 51.50853, 53.4794), lng = c(-89.1221, -87.6862, -77.0163, -77.0861, -0.12574, -2.2453)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"))

Peter
  • 11,500
  • 5
  • 21
  • 31
  • Please include the code you have tried and if possible your expected output, a sketch may suffice. – Peter Nov 27 '22 at 15:59

1 Answers1

0

Two comments to this:

  • you are unlikely to convert your data to multiopolygon object, since you have only a single coordinate pair for each city / that spells points to me, not polygons
  • you were on a right track with sf::st_as_sf()

What you need to do in your st_as_sf call to make it work properly is

  1. specify columns in your data frame that have coordinate information and
  2. give a meaning to the figures (are they degrees? meters? or has an American been feeling patriotic and encoded the data in feet?) you use a coordinate reference system (crs) for that.

For long lat data EPSG:4326 is usually a good default.

library(sf)
library(tmap)

sf_coords <- coor %>% 
  st_as_sf(coords = c("lng", "lat"), crs = 4326)

tmap_mode("view")

tm_shape(sf_coords) +
  tm_bubbles(size = 5, col = "red", id = "city") +
  tm_basemap(leaflet::providers$Stamen.Watercolor)

enter image description here

Jindra Lacko
  • 7,814
  • 3
  • 22
  • 44