Related
I have this map in leaflet/r:
library(leaflet)
library(leaflet.extras)
library(dplyr)
# using the same reproducible data from the question/example
cities <- na.omit(read.csv(
textConnection("City,Lat,Long,Pop, term1, term2
Boston,42.3601,-71.0589,645966, AAA, BBB
Hartford,41.7627,-72.6743,125017, CCC, DDD
New York City,40.7127,-74.0059,8406000, EEE, FFF
Philadelphia,39.9500,-75.1667,1553000, GGG, HHH
Pittsburgh,40.4397,-79.9764,305841, III, JJJ
Providence,41.8236,-71.4222,177994, JJJ, LLL
")))
# leaf-green.png
#https://leafletjs.com/examples/custom-icons/leaf-green.png
leaflet(cities) %>%
addProviderTiles(providers$OpenStreetMap) %>%
addMarkers( clusterOptions = markerClusterOptions()) %>%
addResetMapButton() %>%
# these markers will be "invisible" on the map:
addMarkers(
data = cities, lng = ~Long, lat = ~Lat, label = cities$City,
group = 'cities', # this is the group to use in addSearchFeatures()
# make custom icon that is so small you can't see it:
icon = makeIcon(
iconUrl = "https://leafletjs.com/examples/custom-icons/leaf-green.png",
iconWidth = 1, iconHeight = 1
)
) %>%
addSearchFeatures(
targetGroups = 'cities', # group should match addMarkers() group
options = searchFeaturesOptions(
zoom=12, openPopup = TRUE, firstTipSubmit = TRUE,
autoCollapse = TRUE, hideMarkerOnCollapse = TRUE
)
)
Using this map, I am able to "search" for a city using the search bar:
I would like to modify this code so that I can search based on "city", "term1" or "term2".
I tried this code over here:
leaflet(cities) %>%
addProviderTiles(providers$OpenStreetMap) %>%
addMarkers( clusterOptions = markerClusterOptions()) %>%
addResetMapButton() %>%
# these markers will be "invisible" on the map:
addMarkers(
data = cities, lng = ~Long, lat = ~Lat, label = cities$City,
group = 'cities', # this is the group to use in addSearchFeatures()
# make custom icon that is so small you can't see it:
icon = makeIcon(
iconUrl = "https://leafletjs.com/examples/custom-icons/leaf-green.png",
iconWidth = 1, iconHeight = 1
)
) %>%
addSearchFeatures(
targetGroups = c('cities', 'term1', 'term2'), # group should match addMarkers() group
options = searchFeaturesOptions(
zoom=12, openPopup = TRUE, firstTipSubmit = TRUE,
autoCollapse = TRUE, hideMarkerOnCollapse = TRUE
)
)
This code runs without error, but I can not search using "term1" or "term2":
According to the documentation (https://www.rdocumentation.org/packages/leaflet.extras/versions/1.0.0/topics/addSearchFeatures), "addSearchFeatures" should accept a "vector of group names of groups whose features need to be searched". I was under the impression that vectors in R are specified using c('arg1', 'arg2', 'arg3') - but apparently in this function, this is not the case?
Could someone please show me how to fix this?
Thank you!
For the element to be a search term in addSearchFeatures, I'm pretty sure that it has to be a group element. Check it out:
leaflet(cities) %>%
addProviderTiles(providers$OpenStreetMap) %>%
addMarkers(clusterOptions = markerClusterOptions()) %>%
addResetMapButton() %>%
# these markers will be "invisible" on the map:
addMarkers(
data = cities, lng = ~Long, lat = ~Lat, label = cities$City,
group = 'cities',# this is the group to use in addSearchFeatures()
# make custom icon that is so small you can't see it:
icon = makeIcon(
iconUrl = "https://leafletjs.com/examples/custom-icons/leaf-green.png",
iconWidth = 1, iconHeight = 1
)) %>%
addMarkers(data = cities, lng = ~Long, lat = ~Lat,
label = cities$term1, group = 'term1') %>%
addMarkers(data = cities, lng = ~Long, lat = ~Lat,
label = cities$term2, group = 'term2') %>%
addSearchFeatures(
targetGroups = c('cities', 'term1', 'term2'), # group should match addMarkers() group
options = searchFeaturesOptions(
zoom=12, openPopup = TRUE, firstTipSubmit = TRUE,
autoCollapse = TRUE, hideMarkerOnCollapse = TRUE
)
)
I have a code below, and I would like to forloop the overlayGroup by each cluster. Many thanks in advance.
library(dplyr); library(leaflet); library(raster);library(htmltools)
cluster <- c("1st.C", "2nd.C", "3rd.C")
lat <- c(40.8518, 42.6611, 37.3089)
long <- c(14.2681, 13.6987, 13.5858)
data <- data.frame(cluster, lat, long); data
adm <- raster::getData('GADM', country= "italy" , level=1)
map_layers <- function() {
#number of groups
k <- n_distinct(data$cluster)
#base map
map <- leaflet() %>%
addProviderTiles(providers$CartoDB.Positron)
#loop through all groups and add a layer one at a time
for (i in 1:k) {
map <- map %>%
addCircleMarkers(
data = data %>% filter(cluster == i), group = as.character(i),
radius = 3, lng = ~long, lat = ~lat
)
}
#create layer control
map %>%
addLayersControl(
overlayGroups = c(1:k),
options = layersControlOptions(collapsed = FALSE)) %>%
hideGroup(as.character(c(2:k))) #hide all groups except the 1st one
}
#plot the map
map_layers()
i think this is what you are trying to achieve:
leaflet() %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addCircleMarkers(data = data,
radius = 3, lng = ~long, lat = ~lat, group = cluster) %>%
addLayersControl(overlayGroups = cluster,
options = layersControlOptions(collapsed = FALSE)) %>%
hideGroup(group = data$cluster[2:3])
I have a database of medical consultations by cities. I use the leaflet and state_popup function to display the results on a map, as described below:
pal <- colorBin("Blues",domain = DATA$QUANTITY_MEDICAL,bins = c(1, 1000, 5000, 10000, 50000,100000,300000),na.color=NA)
state_popup <- paste0("<strong>CITY: </strong>",
DATA$CITY,
"<br><strong> QUANTITY OF MEDICAL CONSULTATION: </strong>",
DATA$QUANTITY_MEDICAL)
leaflet(data = DATA) %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(fillColor = ~pal(QUANTITY_MEDICAL),
fillOpacity = 0.7,
color = "#BDBDC3",
weight = 1,
popup = state_popup) %>%
addLegend("topright","bottomright", pal = pal, values = ~ DATA$QUANTITY_MEDICAL,
title = " QUANTITY OF MEDICAL CONSULTATION ",
opacity = 1)
This script only shows one result at a time (medical consultations). I would like to include information about other medical procedures that I want to include, such as exams or hospitalizations.
I want for each procedure (medical consultation, exams or hospitalization) to include a button that would change the result. In my database (DATA) I already have the columns that separate the procedures I quoted. Is it possible to include this button to change the results on the map?
You are going to have to define groups it seems to me. There is a good link from RStudio on leaflet that may help you.
https://rstudio.github.io/leaflet/showhide.html
Here is some sample code from that site:
quakes <- quakes %>%
dplyr::mutate(mag.level = cut(mag,c(3,4,5,6),
labels = c('>3 & <=4', '>4 & <=5', '>5 & <=6')))
quakes.df <- split(quakes, quakes$mag.level)
l <- leaflet() %>% addTiles()
names(quakes.df) %>%
purrr::walk( function(df) {
l <<- l %>%
addMarkers(data=quakes.df[[df]],
lng=~long, lat=~lat,
label=~as.character(mag),
popup=~as.character(mag),
group = df,
clusterOptions = markerClusterOptions(removeOutsideVisibleBounds = F),
labelOptions = labelOptions(noHide = F,
direction = 'auto'))
})
l %>%
addLayersControl(
overlayGroups = names(quakes.df),
options = layersControlOptions(collapsed = FALSE)
)
I have a map I update monthly that has layers of information and I get it like so:
lsl <- unique(origAddress$LIHN_Line) # Gets unique service lines
# Create color palette
lihnpal <- colorFactor(
palette = 'Dark2'
, domain = origAddress$LIHN_Line
)
# create initial leaflet
LIHNMap <- leaflet() %>%
setView(lng = sv_lng, lat = sv_lat, zoom = sv_zoom) %>%
addTiles(group = "OSM (default)") %>%
addProviderTiles(providers$Stamen.Toner, group = "Toner") %>%
addProviderTiles(providers$Stamen.TonerLite, group = "Toner Lite") %>%
addControl("LIHN Service Line Point Map", position = "topright")
# for loop to cycle through adding layers
for(i in 1:length(lsl)){
LIHNMap <- LIHNMap %>%
addCircles(
data = subset(origAddress, origAddress$LIHN_Line == lsl[i])
, group = lsl[i]
, lat = ~lat
, lng = ~lon
, radius = 3
, fillOpacity = 1
, color = ~lihnpal(LIHN_Line)
, label = ~htmlEscape(LIHN_Line)
, popup = ~as.character(
paste(
"<strong>Hospitalist/Private: </strong>"
, hosim
, "<br><strong>Address: </strong>"
, FullAddress
, "<br><strong>Service Line: </strong>"
, LIHN_Line
, "<br><strong>LOS: </strong>"
, LOS
, "<br><strong>SOI: </strong>"
, SOI
, "<br><strong>Encounter: </strong>"
, pt_id
, "<br><strong>Payer Group:</strong>"
, pyr_group2
)
)
)
}
# add layercontrol
LIHNMap <- LIHNMap %>%
addLayersControl(
baseGroups = c("OSM (default)", "Toner", "Toner Lite"),
overlayGroups = lsl,
options = layersControlOptions(
collapsed = TRUE
, position = "topright"
)
)
LIHNMap <- LIHNMap %>%
addAwesomeMarkers(
lng = sv_lng
, lat = sv_lat
, icon = hospMarker
, label = ""
, popup = HospPopup
)
# print map
LIHNMap
I have a couple of columns with categorical data. I would like to create layers from these categorical data and plot them against a base map in R with leaflet. Each of these columns has 4 or more categories.
I am completely lost on how to attack this problem. I tried to do one category from one column and I got all points on the map back and the layer controlled all points which was not the expected behavior.
Here is what I have:
lihn_map <- leaflet(origAddress) %>%
setView(lng = sv_lng, lat = sv_lat, zoom = sv_zoom) %>%
addTiles(group = "OSM (default)") %>%
addCircleMarkers(data = origAddress
, radius = 3
, fillOpacity = 1
, group = "MI"
) %>%
addProviderTiles(providers$Stamen.Toner, group = "Toner") %>%
addProviderTiles(providers$Stamen.TonerLite, group = "Toner Lite") %>%
# Overlay groups
addCircles(~lat, ~lon, group = "MI") %>%
addLayersControl(
baseGroups = c("OSM (default)", "Toner", "Toner Lite"),
overlayGroups = "MI",
options = layersControlOptions(collapsed = FALSE)
)
lihn_map
I am trying this loop, it adds the groups to the layer control but the selection does not change the map:
lsl <- unique(origAddress$LIHN_Line)
mt <- leaflet() %>%
addTiles(group = "OSM (default)") %>%
addProviderTiles(providers$Stamen.Toner, group = "Toner") %>%
addProviderTiles(providers$Stamen.TonerLite, group = "Toner Lite")
for(i in 1:length(lsl)){
l <- lsl[i]
mt <- mt %>%
addCircleMarkers(
# I have also tried
data = subset(origAddress, LIHN_Line = lsl[i])
data = origAddress
, group = lsl[i]
, radius = 3
, fillOpacity = 0.6)
}
mt <- mt %>%
addLayersControl(
baseGroups = c("OSM (default)", "Toner", "Toner Lite"),
overlayGroups = lsl,
options = layersControlOptions(collapsed = FALSE
, position = "bottomright")
)
mt
My expectation is that this layer would only control the subset of data where the group is equal to MI Maybe I have to create subset data.frames? This seems inefficient if so and I'm sure is not the answer.
I performed the following and it worked:
# for loop to cycle through adding layers
for(i in 1:length(lsl)){
l <- lsl[i]
mt <- mt %>%
addCircles(
data = subset(origAddress, origAddress$LIHN_Line == lsl[i])
#data = origAddress
, group = lsl[i]
, radius = 3
, fillOpacity = 0.6)
}
using subset worked brilliantly
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
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))