"non-numeric argument to binary operator" 使用 tmap 绘制 sf 数据时出错

"non-numeric argument to binary operator" error when using tmap to plot sf data

当我尝试使用 tm_symbols 显示点数据时,我收到错误消息: non-numeric argument to binary operator.

我已经剥离了我的代码以试图找出问题所在,当然我已经搜索了 tmap 和其他文档。

一些链接到其他人做我正在尝试做的事情:

另请参阅:

这是我的代表:

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

这给了我:

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

我正在尝试使用 tmap,正如我所看到的那样,但我想我也会尝试不同的方法来生成地图...如果我这样做:

plot(items_sf)

...它给出了错误:

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

如果我这样做:

library(mapview)
mapview(items_sf)

...我得到了一个绘制了三个点的地图视图,但总范围小于一米,因此由于某种原因,坐标没有被处理为纬度和经度。

我很高兴致力于解决问题,但我想我真的被困在这里了,因为我不知道如何处理这些错误消息。

我期待三个位置的 tmap 图作为覆盖在底图上的点 (dots/symbols)。实际结果:错误消息且未呈现地图。

** 编辑: 好吧,引用数字错误对我来说很愚蠢,受访者的好地方。由于我输入了数据框,而不是简单地从我实际使用的数据框复制。 修复后我的脚本仍然有一些其他错误,但我最终修复了它们。

projection/ESPG 这件事很有帮助,因为我还不是很了解那些,基本上是在猜测该怎么做。所以我也在那里学到了一些东西。 **

您的代码似乎有两个问题:

  • 您的坐标存储为文本(即不是数字)
  • 您使用公制 CRS (3857),其坐标在以十进制度(angular 单位)查看时更有意义

考虑此代码,稍作修改(删除引号并将 CRS 从 3857 更改为 4326 + 更改颜色)

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