Is it possible to add a pop-label when the user mouse over a certain point in a leaflet heatmap? For example to see depth and stations from the quakes dataset.
library(leaflet)
leaflet(quakes) %>%
addProviderTiles(providers$CartoDB.DarkMatter) %>%
setView( 178, -20, 5 ) %>%
addHeatmap(
lng = ~long, lat = ~lat, intensity = ~mag,
blur = 20, max = 0.05, radius = 15
)
## for more examples see
# browseURL(system.file("examples/heatmaps.R", package = "leaflet.extras"))
kml <- readr::read_file(
system.file("examples/data/kml/crimes.kml.zip", package = "leaflet.extras")
)
leaflet() %>%
setView(-77.0369, 38.9072, 12) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addKMLHeatmap(kml, radius = 7) %>%
addKML(
kml,
markerType = "circleMarker",
stroke = FALSE, fillColor = "black", fillOpacity = 1,
markerOptions = markerOptions(radius = 1))
I'm not sure this is what you want but you can add marker popups in the usual way:
library(leaflet)
leaflet(quakes) %>%
addProviderTiles(providers$CartoDB.DarkMatter) %>%
setView( 178, -20, 5 ) %>%
addHeatmap(
lng = ~long, lat = ~lat, intensity = ~mag,
blur = 20, max = 0.05, radius = 15
) %>%
addMarkers(lng = quakes$long, lat = quakes$lat,
popup = paste("Depth", quakes$depth, "<br>",
"Stations:", quakes$stations))
if you dont want the dominating markers visible you could add circle markers but set the fillOpacity to zero:
leaflet(quakes) %>%
addProviderTiles(providers$CartoDB.DarkMatter) %>%
setView( 178, -20, 5 ) %>%
addHeatmap(
lng = ~long, lat = ~lat, intensity = ~mag,
blur = 20, max = 0.05, radius = 15
) %>%
addCircleMarkers(lng = quakes$long, lat = quakes$lat,
fillOpacity = 0, weight = 0,
popup = paste("Depth:", quakes$depth, "<br>",
"Stations:", quakes$stations),
labelOptions = labelOptions(noHide = TRUE))
Related
I have 30 years data for 15 stations with average temperature and want to create an animation using r studio (leaflet). I was looking at the some codes but it not working. An explanation on how to create timeseries animation using leaflet in r studio would be helpfull.
# install libraray
library(sp)
library(raster)
library(leaflet)
Temp53Yrs_df <- read.csv("C:/Users/Sownal/Documents/53YearsTemp.csv")
View(Temp53Yrs_df)
#make plots for the points in the map
leaflet() %>%
addTiles() %>%
addCircleMarkers(lat = Temp53Yrs_df$lat,
lng = Temp53Yrs_df$long)
# Add Data for Average Yearly Annual Temperature
col_pal <- colorNumeric(palette = "viridis",
domain = Temp53Yrs_df$X1965, reverse = TRUE)
#make years plot in the map
leaflet() %>%
addTiles() %>%
addCircles(lat = Temp53Yrs_df$lat,
lng = Temp53Yrs_df$long,
color = col_pal(Temp53Yrs_df$X1965),
radius = 20000,
fillOpacity = 0.4,
label = Temp53Yrs_df$X1965) %>%
addLegend(Temp53Yrs_df, position = "bottomleft", pal = col_pal,
values = Temp53Yrs_df$X1965, title = "Average Temperature in Year
1965")
#make years plot in the map
leaflet() %>%
addTiles() %>%
addCircles(lat = Temp53Yrs_df$lat,
lng = Temp53Yrs_df$long,
color = col_pal(Temp53Yrs_df$X1966),
radius = 20000,
fillOpacity = 0.4,
label = Temp53Yrs_df$X1966) %>%
addLegend(Temp53Yrs_df, position = "bottomleft", pal = col_pal,
values = Temp53Yrs_df$X1966, title = "Average Temperature in Year
1966")
#make years plot in the map
leaflet() %>%
addTiles() %>%
addCircles(lat = Temp53Yrs_df$lat,
lng = Temp53Yrs_df$long,
color = col_pal(Temp53Yrs_df$X1967),
radius = 20000,
fillOpacity = 0.4,
label = Temp53Yrs_df$X1967) %>%
addLegend(Temp53Yrs_df, position = "bottomleft", pal = col_pal,
values = Temp53Yrs_df$X1967, title = "Average Temperature in Year
1967")
#make years plot in the map
leaflet() %>%
addTiles() %>%
addCircles(lat = Temp53Yrs_df$lat,
lng = Temp53Yrs_df$long,
color = col_pal(Temp53Yrs_df$X1968),
radius = 20000,
fillOpacity = 0.4,
label = Temp53Yrs_df$X1968) %>%
addLegend(Temp53Yrs_df, position = "bottomleft", pal = col_pal,
values = Temp53Yrs_df$X1968, title = "Average Temperature in Year
1968")
Sample Data is attached below
enter image description hereenter image description here
Here is my code:
m <- leaflet() %>%
addProviderTiles(providers$Stamen.Toner) %>%
setView(lng = -107.9917071, lat = 59.5, zoom = 3.5) %>%
addPolygons(data = plant,
color = "#660000",
weight = 1,
smoothFactor = 0.5) %>%
addCircleMarkers(lng = plant$lon, lat = plant$lat)
m
No matter what I try I get the following error message:
Error in polygonData.default(data) : Don't know how to get path
data from object of class spec_tbl_df
My data frame from which my data comes is a simple 5 row by 3 columns of coordinates and the name of the place.
Thoughts?
You had point data, not polygons.
library(leaflet)
plant <- data.frame(
stringsAsFactors = FALSE,
Name = c("University","University",
"University","University","University"),
lat = c(43.5339923, 49.8091536, 3.52682, 49.2519564, 45.5069177),
lon = c(-80.2244647,-97.1330418,
-113.5244937,-123.2465285,-73.5791163)
)
leaflet() %>%
addProviderTiles(providers$Stamen.Toner) %>%
setView(lng = -107.9917071, lat = 59.5, zoom = 3.5) %>%
# I removed the addPolygons(), you are adding points, not polygons
addCircleMarkers(lng = plant$lon, lat = plant$lat)
Is it possible, to make the black line thinner?
Here's a reproducible example, see also here:
library(leaflet)
leaflet() %>%
addTiles() %>%
addProviderTiles(providers$OpenStreetMap, group = "OSM") %>%
addProviderTiles(providers$Stamen.TonerLite, group = "Toner Lite") %>%
addLayersControl(baseGroups = c("OSM", "Toner Lite")) %>%
leaflet::addCircleMarkers(lat = 0,
lng = 0,
color = "black",
fillColor = "red",
stroke = TRUE,
popup = "hello",
radius = 10,
fillOpacity = 0.7)
You can specifiy the stroke weight with the weight argument:
library(leaflet)
leaflet() %>%
addTiles() %>%
addProviderTiles(providers$OpenStreetMap, group = "OSM") %>%
addCircleMarkers(lat = 0,
lng = 0,
stroke = TRUE,
weight = 1)
leaflet() %>%
addTiles() %>%
addProviderTiles(providers$OpenStreetMap, group = "OSM") %>%
addCircleMarkers(lat = 0,
lng = 0,
stroke = TRUE,
weight = 100)
I am trying to set zoom out maximum in my R Leaflet map. I follow an example of a previous question/answer in Prevent zooming out in leaflet R-Map? , but it doesn't work. The line that should be able to do this is:
options = providerTileOptions(minzoom = 1, maxzoom = 10))
Can you guys can help me to figure out why?
Here is code:
deck_lf_par_map <- leaflet(lpoints) %>%
addPolygons(data = dio, noClip=T,
weight = 4,
dashArray="5, 1",
color = "black",
fillOpacity = .01,
smoothFactor = 0) %>%
setView(lng = mean(lpoints$long), lat = mean(lpoints$lat), zoom = 09) %>%
addProviderTiles("Stamen.TonerLite",
group = "Toner",
options = providerTileOptions(minzoom = 1, maxzoom = 10)) %>%
addTiles(group = "OSM") %>%
addProviderTiles("Esri.WorldTopoMap",
group = "Topo") %>%
addProviderTiles("OpenStreetMap.Mapnik", group = "Mapnik") %>%
addProviderTiles("CartoDB.Positron", group = "CartoDB") %>%
setMaxBounds((dioc#bbox[1,1] - .3),
(dioc#bbox[2,1] - .3),
(dioc#bbox[1,2] + .3),
(dioc#bbox[2,2] + .3)) %>%
addMarkers(lpoints$long,
lpoints$lat,
popup=ppopup,
icon = tec_icon,
group="Parishes",
clusterOptions = markerClusterOptions()) %>%
addLayersControl(baseGroups = c("Toner", "OSM", "Topo", "Mapnik", "CartoDB"),
options = layersControlOptions(collapsed = TRUE))
A couple of points:
It's minZoom and maxZoom (notice the capital Z)
You need the options in each Tile function that you want to set
the zoom levels for.
library(leaflet)
## the first two tiles have a zoom level control - the others don't
leaflet() %>%
setView(lng = 144, lat = -37, zoom = 09) %>%
addProviderTiles("Stamen.TonerLite",
group = "Toner",
options = providerTileOptions(minZoom = 8, maxZoom = 10)) %>%
addTiles(group = "OSM",
options = providerTileOptions(minZoom = 8, maxZoom = 10)) %>%
addProviderTiles("Esri.WorldTopoMap",
group = "Topo") %>%
addProviderTiles("OpenStreetMap.Mapnik", group = "Mapnik") %>%
addProviderTiles("CartoDB.Positron", group = "CartoDB") %>%
addLayersControl(baseGroups = c("Toner", "OSM", "Topo", "Mapnik", "CartoDB"),
options = layersControlOptions(collapsed = TRUE))
My leaflet map looks something like this:
library(sp)
library(leaflet)
circleFun <- function(center = c(0,0),diameter = 1, npoints = 100){
r = diameter / 2
tt <- seq(0,2*pi,length.out = npoints)
xx <- center[1] + r * cos(tt)
yy <- center[2] + r * sin(tt)
Sr1 = Polygon(cbind(xx, yy))
Srs1 = Polygons(list(Sr1), "s1")
SpP = SpatialPolygons(list(Srs1), 1:1)
return(SpP)
}
Circle.Town <- circleFun(c(1,-1),2.3,npoints = 100)
df1 <- data.frame(long=c(0.6,1,1.4), lat=c(-2, -.8, -0.2), other=c('a', 'b', 'c'), VAM=c(10,8,6),
type=c('Public', 'Public', 'Private'), id=c(1:3)) %>%
mutate(X=paste0('<strong>id: </strong>',
id,
'<br><strong>type</strong>: ',
type,
'<br><strong>VAM</strong>: ',
VAM))
# Create a continuous palette function
pal <- colorNumeric(
palette = "RdYlBu",
domain = df1$VAM
)
leaflet(height = "400px") %>%
addTiles() %>%
addPolygons(data = Circle.Town, color = 'green', fillOpacity = .7) %>%
addCircleMarkers(data = df1, lat = ~lat, lng =~long,
radius = ~VAM, popup = ~as.character(X),
fillColor = ~pal(VAM),
stroke = FALSE, fillOpacity = 0.8,
clusterOptions = markerClusterOptions()) %>%
addLegend(position = "topright",
pal = pal, values = df1$VAM,
title = "VAM",
opacity = 1
) %>%
setView(lng = 1, lat = -1, zoom = 8)
Right now, I get a popup when I click one of the circles. Is it possible to get the information when I hover the mouse instead of click? Ideally, I would like something like this.
Thanks!
This may have been added to the leaflet package since this question was posed a year ago, but this can be done via the label argument. I am using leaflet R package version 1.1.0.
Read the data in as above:
library(sp)
library(leaflet)
library(dplyr)
circleFun <- function(center = c(0,0),diameter = 1, npoints = 100){
r = diameter / 2
tt <- seq(0,2*pi,length.out = npoints)
xx <- center[1] + r * cos(tt)
yy <- center[2] + r * sin(tt)
Sr1 = Polygon(cbind(xx, yy))
Srs1 = Polygons(list(Sr1), "s1")
SpP = SpatialPolygons(list(Srs1), 1:1)
return(SpP)
}
Circle.Town <- circleFun(c(1,-1),2.3,npoints = 100)
df1 <- data.frame(long=c(0.6,1,1.4), lat=c(-2, -.8, -0.2), other=c('a', 'b', 'c'), VAM=c(10,8,6),
type=c('Public', 'Public', 'Private'), id=c(1:3)) %>%
mutate(X=paste0('<strong>id: </strong>',
id,
'<br><strong>type</strong>: ',
type,
'<br><strong>VAM</strong>: ',
VAM))
# Create a continuous palette function
pal <- colorNumeric(
palette = "RdYlBu",
domain = df1$VAM
)
But create a list of labels instead of vector:
labs <- as.list(df1$X)
And then lapply the HTML function over that list within the label argument. Note to use label instead of popup.
library(htmltools)
leaflet(height = "400px") %>%
addTiles() %>%
addPolygons(data = Circle.Town, color = 'green', fillOpacity = .7) %>%
addCircleMarkers(data = df1, lat = ~lat, lng =~long,
radius = ~VAM, label = lapply(labs, HTML),
fillColor = ~pal(VAM),
stroke = FALSE, fillOpacity = 0.8,
clusterOptions = markerClusterOptions()) %>%
addLegend(position = "topright",
pal = pal, values = df1$VAM,
title = "VAM",
opacity = 1
) %>%
setView(lng = 1, lat = -1, zoom = 8)
This method is described in an an answer to this SO question: R and Leaflet: How to arrange label text across multiple lines
There is more info on HTML in labels in leaflet documentation:
https://rstudio.github.io/leaflet/popups.html
Here is an alternative:
library(leaflet)
library(htmltools)
library(htmlwidgets)
yourmap <- leaflet(height = "400px") %>%
addTiles() %>%
addPolygons(data = Circle.Town, color = 'green', fillOpacity = .7) %>%
addCircleMarkers(data = df1, lat = ~lat, lng =~long,
radius = ~VAM, popup = ~as.character(X),
fillColor = ~pal(VAM),
stroke = FALSE, fillOpacity = 0.8,
clusterOptions = markerClusterOptions()) %>%
addLegend(position = "topright",
pal = pal, values = df1$VAM,
title = "VAM",
opacity = 1
) %>%
setView(lng = 1, lat = -1, zoom = 8)
setwd("~/Desktop/")
saveWidget(yourmap, file="yourmap.html")
In your desktop, you will have an html and a folder saved under yourmap. Open the leaflet.js file located in /pathTo/yourmap_files/leaflet-binding-1.0.1.9002.
In leaflet.js, scroll down to var popup = df.get(i, 'popup');
and paste just below:
marker.on('mouseover', function (e) {
this.openPopup();
});
marker.on('mouseout', function (e) {
this.closePopup();
});
Save and reopen yourmap.html file. Hover on one of your point!!