将传单悬停在R中时弹出?
我的传单地图如下所示:将传单悬停在R中时弹出?,r,leaflet,r-leaflet,R,Leaflet,R Leaflet,我的传单地图如下所示: 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
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)
库(sp)
图书馆(单张)
circleFun%
addPolygons(数据=Circle.Town,颜色='green',fillOpacity=.7)%
addCircleMarkers(数据=df1,纬度=~lat,液化天然气=~long,
半径=~VAM,弹出=~as.character(X),
fillColor=~pal(VAM),
笔划=假,填充不透明度=0.8,
clusterOptions=markerClusterOptions())%>%
addLegend(position=“topright”,
pal=pal,值=df1$VAM,
title=“VAM”,
不透明度=1
) %>%
设置视图(lng=1,lat=-1,zoom=8)
现在,当我点击其中一个圆圈时,会弹出一个窗口。当我将鼠标悬停而不是单击时,是否可以获取信息?理想情况下,我想要类似的东西
谢谢 这里有一个替代方案:
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")
保存并重新打开yourmap.html文件。悬停在你的一个点上 自一年前提出此问题以来,此问题可能已添加到传单包中,但这可以通过
标签
参数完成。我使用的是传单R包版本1.1.0
按上述步骤读取数据:
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
)
本SO问题的答案中描述了该方法:
传单文档中的标签中有更多关于HTML的信息:
我用传单地图尝试了这个解决方案,但在使用saveWidget功能时,没有得到“yourmap”文件夹。我得到的只是.html文件。当我在括号(我选择的文本编辑器)中打开它时,
var popup=…
不存在。有什么建议吗?@Lauren,很抱歉,我无法访问我的R代码;我再也没有电脑了!但是当你点击html文件时,你有传单地图吗?当您运行代码时,是否有任何警告?是的,当我单击.html文件时,我的地图会显示出来。如果我用我的文本编辑器打开它,会有一些超长的(看起来)乱七八糟的字符串,大量的坐标,然后是弹出的信息。由于我在Javascript中使用传单的经验相对有限,我并不真正认识到JS的语法。当我打开它时没有警告@劳伦。我不知道你是否解决了你的问题,但我只是在我的新电脑上重新运行了代码,它工作了。我相信我发现了问题!在saveWidget
函数中,为了访问JS文件,我必须将参数“selfcontained”设置为false。现在再次尝试解决方案!
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
)
labs <- as.list(df1$X)
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)