0

I am working on geospatial data :

 place_name    id region_country     x     y
    <chr>      <dbl> <chr>          <dbl> <dbl>
     1 Barcelona  10102 Spain           41.4  2.18
     2 Bilbao     10103 Spain           43.2 -2.97
     3 Cadiz      10104 Spain           36.5 -6.30
     4 Gibraltar  10106 Spain           36.1 -5.35
     5 La Coruña  10107 Spain           43.4 -8.38

I want to plot this cities over the World basemap of tmap.

library(sf)
library(tmap)
data("World")

Here are my data :

x <- c(41.38333, 43.25000, 36.53361, 36.13333, 43.36667)
y <- c(2.18333, -2.96667, -6.29944, -5.35000, -8.38333)

Creating a data.frame object :

 geocode <- data.frame(x,y)

Converting to sf object :

geocode2 <- st_as_sf(geocode, coords = c("x", "y"))

Building the map :

map1 <- tm_shape(World) +  # basemap
        tm_fill() +
        tm_borders() +
        tm_shape(geocode2)+      # dots shape
        tm_dots()

map1

The error :

Error in if (!tmaptools::is_projected(shp)) { : valeur manquante là où TRUE / FALSE est requis
1

If I try your code I get a warning rather than an error:

> map1
Warning message:
Currect projection of shape geocode2 unknown. Long-lat (WGS84) is assumed. 

but the reason might be the same:

geocode2 <- st_as_sf(geocode, coords = c("x", "y"))

creates an object without a coordinate system reference, so tmap doesn't know for sure how to convert it the same coordinate system as the World map.

Assuming your coordinates are WGS84 Lat-long coordinates, then specifying the CRS as 4326 thus should work (code 4326 is the EPSG code for the usual WGS84 lat-long system)

geocode2 <- st_as_sf(geocode, coords = c("x", "y"),crs=4326)

You might also want to do tm_dots(size=2) in order to see any dots.

The difference between your error and my warning might be due to the tmap version. I am on:

> packageVersion("tmap")
[1] ‘2.3.2’
> packageVersion("tmaptools")
[1] ‘2.0.2’
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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