0

When I try to display point data using tm_symbols, I get the error message: non-numeric argument to binary operator.

I have stripped my code back to try to pinpoint the problem, and of course I have searched the tmap and other documentation.

Some links to other people doing something like what I am trying to do:

See also:

Here's my reprex:

library(sf)
library(tmap)
library(leaflet)

item_data <- data.frame(
    name=c("Epping Forest District Citizens Advice (Epping)","Epping Forest District Citizens Advice (Loughton)","Epping Forest District Citizens Advice (Waltham Abbey)"),
    latitude=c("51.696921", "51.649158", "51.687181"),
    longitude=c("0.110474", "0.05899", "-0.004736"),
    stringsAsFactors = FALSE
)
items_sf <- st_as_sf(item_data, coords=c("longitude", "latitude"), crs=3857)

tmap_mode("view")
epmap <- tm_basemap(leaflet::providers$Stamen.TonerBackground) +
  tm_shape(items_sf, name="CA Locations") +
  tm_symbols(shape=21)
epmap

This gives me:

## Error in b[3:4] - b[1:2] : non-numeric argument to binary operator

I'm trying to use tmap as I have seen it recommended, but I thought I'd also try different ways of generating the map instead... If I do:

plot(items_sf)

...it gives the error:

## Error in r[i1] - r[-length(r):-(length(r) - lag + 1L)] : non-numeric argument to binary operator

and if I do:

library(mapview)
mapview(items_sf)

... I get a mapview with the three points plotted but at a scale of less than a metre, total extent, so the coords are not being processed as lat and lon for some reason.

I am happy to work on solving problems but I think I am really stuck here because I have no idea what to do with these error messages.

I am expecting a tmap plot of the three locations as points (dots/symbols) overlaid onto the basemap. Actual results: error messages and no map rendered.

** Edit : well, the quoting numerics error was pretty stupid of me, good spot by the respondent. Caused by me typing out the data frame rather than simply copying from the one I was actually using. Once that was fixed I still had some other errors with my script but i got them fixed eventually.

The projection/ESPG thing was helpful because I don't really understand those yet and was basically guessing what to do. So I learned something there, too. **

Francis Barton
  • 129
  • 2
  • 16

1 Answers1

1

There seem to be two issues with your code:

  • your coordinates are stored as text (i.e. not numbers)
  • you use a metric CRS (3857) with coordinates that make more sense when viewed as decimal degrees (angular units)

Consider this code, with slight modifications (removed quotation marks and changed CRS from 3857 to 4326 + changed the color)

library(sf)
library(tmap)
library(leaflet)

item_data <- data.frame(
  name=c("Epping Forest District Citizens Advice (Epping)","Epping Forest District Citizens Advice (Loughton)","Epping Forest District Citizens Advice (Waltham Abbey)"),
  latitude=c(51.696921, 51.649158, 51.687181),
  longitude=c(0.110474, 0.05899, -0.004736),
  stringsAsFactors = FALSE
)

items_sf <- st_as_sf(item_data, coords = c("longitude", "latitude"), crs = 4326)

tmap_mode("view")
epmap <- tm_shape(items_sf, name="CA Locations") + tm_symbols(shape = 21, col = "red") +
  tm_basemap(leaflet::providers$Stamen.TonerBackground)

epmap

enter image description here

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