使用tmap绘制sf数据时出现“二元运算符的非数字参数”错误

问题描述 投票:0回答:1

当我尝试使用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)

...我得到了一个绘制了三个点的mapview,但是在一个小于一米的范围内,总的范围,所以由于某种原因,coords没有被处理为lat和lon。

我很乐意努力解决问题,但我认为我真的被困在这里,因为我不知道如何处理这些错误消息。

我期待三个位置的tmap图作为重叠在底图上的点(点/符号)。实际结果:错误消息并且没有渲染的地图。

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

投影/ ESPG的事情是有帮助的,因为我还没有真正理解那些,并且基本上猜测该怎么做。所以我也在那里学到了一些东西。 **

r sf tmap
1个回答
1
投票

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

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

考虑这段代码,稍作修改(删除引号并将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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.