s.brunel
s.brunel

Reputation: 1043

plot several circle marker layer with "r and leaflet"

i'm tryng to plot several layer of addCircleMarkers with leaflet package and i need a shorter method to do it

now i'm doing this :

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))

and i need to do it for 200 column

im trying to do it like this but it'isn't working

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)
)

it plot each map in different window andthe last one is empty (only layercontrol showing)

i know popup isn't working i need to as.character dataframe before

Thank you

Upvotes: 1

Views: 1026

Answers (1)

NicE
NicE

Reputation: 21425

You are not changing map_test_global in your apply.

You could try a loop:

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))

Upvotes: 2

Related Questions