我正在创建一些要在Leaflet-R地图上绘制的点。 但是,当尝试使用addCircles绘制点时,出现错误:
Warning: Error in derivePoints: Point data not found; please provide addCircles with data and/or lng/lat arguments
有关空间数据框的信息如下:
Simple feature collection with 3 features and 4 fields
geometry type: POINT
dimension: XY
bbox: xmin: -79.48837 ymin: 43.66537 xmax: -79.29187 ymax: 43.71345
epsg (SRID): 4326
proj4string: +proj=longlat +datum=WGS84 +no_defs
我有另一个有效的空间数据框,除了+ datum = WGS84被+ ellps = WGS84替换之外,所有信息都是相同的。可以吗?如果是这样,我该如何进行更改?
示例代码
library(shiny)
library(leaflet)
ui <- fluidPage(
leafletOutput("mymap")
)
server <- function(input, output, session) {
# Create tree geometries
tree_1g <- st_point(c(-79.2918671415814, 43.6760766531298))
tree_2g <- st_point(c(-79.4883669334101, 43.6653747165064))
tree_3g <- st_point(c(-79.2964680812039, 43.7134458013647))
# Create sfc object with multiple sfg objects
points_sfc <- st_sfc(tree_1g, tree_2g, tree_3g, crs = 4326)
# Create tree attributes
data <- data.frame (
id = c(1, 2, 3),
address = c(10, 20, 30),
street = c("first", "second", "third"),
tname = c("oak", "elm", "birch")
)
tree_data <- st_sf(data, geometry = points_sfc)
output$mymap <- renderLeaflet({
leaflet() %>%
addProviderTiles(providers$Stamen.Watercolor) %>%
# Centre the map in the middle of Toronto
setView(lng = -79.384293,
lat = 43.685, #43.653908,
zoom = 11) %>%
addCircles(tree_data)
})
}
shinyApp(ui, server)
答案 0 :(得分:1)
您需要指定data = tree_data
,因为在addCircles()
调用中,data
参数是最后一个参数。
leaflet() %>%
addProviderTiles(providers$Stamen.Watercolor) %>%
setView(lng = -79.384293,
lat = 43.685,
zoom = 11) %>%
addCircles(data = tree_data)
addCircles()
调用中的第一个参数是map
,它通过%>%
运算符传递。第二个参数是lng
,因此您在通话中所做的等同于
addCircles(lng = tree_data)
将数据发送到addCircles()
函数中的另一种方法是通过leaflet()
调用
leaflet(tree_data) %>%
addProviderTiles(providers$Stamen.Watercolor) %>%
setView(lng = -79.384293,
lat = 43.685,
zoom = 11) %>%
addCircles()