我正在使用R中的Leaflet制作地图。我的数据存储为x,y坐标,现在转换为lng,lat值。
数据集:Dambrug2019(我自己的数据集,以避免在下一个代码示例中引起混淆)。
Name Status x y
1 Point_1 0 482670 6217698
转换:
Locations <- st_as_sf(Dambrug2019, coords=c("x", "y"))
%>% st_set_crs(23032) %>% st_transform(4326)
Name Status geometry
1 Point_1 1 POINT (8.720061 56.10223)
使用全部标记制作相同的地图时,一切工作正常。
Map <- leaflet() %>% addTiles() %>% addMarkers(data=Locations, popup=Locations$Name)
然后我要根据状态为标记着色。
#Color the markers depending on the status.
#Want 0=green, 1=red, 2=orange.
> Color_status <- function(Locations) {sapply(Locations$Status,
function(Status) {if(Status==0){"green"} else if(Status==1)
{"red"} else{"orange"} })}
> Status_Icons <- awesomeIcons(icon='circle', iconColor=
'black', library='ion', markerColor=Color_status(Locations))
> leaflet(Locations) %>% addTiles() %>%
addAwesomeMarkers(c(Locations$geometry), icon=icons,
label=~as.character(Name))
Error in validateCoords(lng, lat, funcName) :
addAwesomeMarkers requires numeric longitude values
因此,看来addAwesomeMarkers无法处理我的积分设置方式。是否有简单/快速的方式将这些点转换为函数可以使用的东西?
或者您是否知道另一种方法来创建可以与我的当前数据配合使用的不同颜色的标记?
尝试过的解决方案:
as.numeric(Locations$geometry)
Error: (list) object cannot be coerced to type 'double'
打印结果(Locations $ gemometry):
> print(Locations$geometry)
Geometry set for 53 features
geometry type: POINT
dimension: XY
bbox: xmin: 8.269083 ymin: 54.89636 xmax: 15.13583 ymax: 56.99576
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
First 5 geometries:
POINT (8.720061 56.10223)
POINT (9.611723 56.67828)
POINT (8.633127 55.69979)
POINT (9.671523 56.99576)
POINT (15.13583 55.05837)
非常感谢您的提前帮助。 :)
答案 0 :(得分:0)
我对leaflet
和sf
软件包不是很熟悉,但是可以尝试一下:
library(st)
library(leaflet)
lng = as.numeric(st_coordinates(Locations$geometry)[,1])
lat = as.numeric(st_coordinates(Locations$geometry)[,2])
leaflet(Locations) %>% addTiles() %>%
addAwesomeMarkers(lng = lng, lat = lat, icon=icons,
label=~as.character(Name))