用“r和leaflet”绘制几个圆形标记层

时间:2016-01-14 15:59:58

标签: r leaflet

我正在尝试用$1$包绘制几层addCircleMarkers,我需要一个更短的方法来实现它

现在我这样做:

leaflet

我需要为200列

做这件事

我试图这样做,但它没有工作

data_stack <- data.frame(lat=rnorm(10),lng=rnorm(10)  ,v1=rnorm(10,10), v2=rnorm(10,20) , v3=rnorm(10,20), v4=rnorm(10,20), v5=rnorm(10,20) )

map_test_global <- leaflet(data_stack) %>%
addTiles() %>%  # Add default OpenStreetMap map tiles
addCircleMarkers(radius = ~ v1,fill = T,fillColor ="red",popup =~as.character(v1) ,group="1"  )%>%
addCircleMarkers(radius = ~ v2,  fill = T , fillColor ="green" ,popup = ~ as.character(v2) ,group="2"  ) %>%
addCircleMarkers(radius = ~ v3,  fill = T , fillColor ="blue" ,popup = ~ as.character(v3) ,group="3"  ) %>%
addCircleMarkers(radius = ~ v4,  fill = T , fillColor ="DarkOrange" ,popup = ~ as.character(v4) ,group="4"  ) %>%
addCircleMarkers(radius = ~ v5,  fill = T , fillColor ="DarkOrange" ,popup = ~ as.character(v5) ,group="5"  ) %>%
addLayersControl( baseGroups = c("1", "2","3","4","5"),
                options = layersControlOptions(collapsed = FALSE))

它在不同的窗口中绘制每个地图,最后一个是空的(仅显示图层控件)

我知道map_test_global <- leaflet(data_stack) %>% addTiles() %>% # Add default OpenStreetMap map tiles lapply( list_var ,function(x) addCircleMarkers(map_test_global ,radius= reformulate(x),popup = reformulate(x) ,group=x ) ) addLayersControl(map_test_global , baseGroups = list_var, options = layersControlOptions(collapsed = T) ) 无效,我需要popup数据帧才能

谢谢

1 个答案:

答案 0 :(得分:2)

您未在申请中更改map_test_global

你可以尝试循环:

map_test_global <- leaflet(data_stack) %>%
  addTiles() 

for(x in 1:5){
  map_test_global <- map_test_global %>% addCircleMarkers(radius = as.formula(paste0("~v",x)),fill = T,fillColor ="red",popup =as.formula(paste0("~as.character(v",x,")")) ,group=as.character(x))
}


map_test_global %>% addLayersControl( baseGroups = as.character(1:5),
                                      options = layersControlOptions(collapsed = FALSE))