2015-06-21 14 views
10

我的單張地圖看起來是這樣的:彈出與R中的傳單時彈出?

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) 

現在,我得到一個彈出,當我點擊了一個社交圈。懸停鼠標而不是點擊時可以獲取信息嗎?理想情況下,我想要像this

謝謝!

回答

6

這可能被添加到單張包,因爲這個問題在一年前提出的,但是這可以通過label參數來完成。我正在使用小冊子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 
) 

但創造,而不是載體的標籤列表:

labs <- as.list(df1$X) 

然後lapplyHTML功能在label參數內的列表。請注意使用label而不是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) 

此方法的一個答案是描述這太問題:R and Leaflet: How to arrange label text across multiple lines

有關於HTML的標籤單張文檔中的詳細信息: https://rstudio.github.io/leaflet/popups.html

0

這裏是一個另類:

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

在你的桌面,你將有一個HTML和下yourmap保存的文件夾。打開位於/pathTo/yourmap_files/leaflet-binding-1.0.1.9002中的leaflet.js文件。 在leaflet.js,向下滾動到var popup = df.get(i, 'popup'); 僅低於粘貼:

  marker.on('mouseover', function (e) { 
    this.openPopup(); 
}); 
marker.on('mouseout', function (e) { 
    this.closePopup(); 
}); 

保存並重新打開yourmap.html文件。懸停在你的一個點!

+0

我試着與我的單張地圖這個解決方案當我使用saveWidget函數時,不會獲得「yourmap」文件夾。我得到的只是.html文件。當我在Brackets(我選擇的文本編輯器)中打開它時,'var popup = ...'不存在。有什麼建議麼? – Lauren

+0

@Lauren,很抱歉,我無法訪問我的R代碼;我沒有電腦了!但是當你點擊html文件時,你有沒有傳單地圖?當你運行代碼時,你有沒有警告? – MLavoie

+0

是的,當我點擊.html文件時,我的地圖確實出現了。如果我用我的文本編輯器打開它,有幾個超長(看起來)的亂碼字符串,大量的座標,然後看起來是彈出信息。由於我在Javascript中使用Leaflet的經驗相對有限,因此我並沒有真正意識到JS的語法。打開時沒有警告! – Lauren