【问题标题】:R Leaflet Label Set directionR 传单标签设置方向
【发布时间】:2019-03-14 10:02:22
【问题描述】:

请看下面的示例代码,我想使用存储在df 中的标签方向(MyDirection)在我的地图中拥有不同的标签方向。

我可以将每个标签设置为特定方向,例如 direction = "top",但如果我指定 direction = ~MyDirection,它就无法正常工作。

任何想法/解决方案将不胜感激。

提前致谢。

library(leaflet)

df  <- read.csv(textConnection("
Name,Lat,Long,MyDirection
ANN,51.19,4.46277778,right
BAB,43.26306,-2.94972222,left
BCN,41.29694,2.07833333,top
BCN,41.29694,2.07833333,bottom
"))

#---Create Map----
m <- leaflet(df) %>% 
     addTiles(group = "OSM (default)") %>%
     addCircles(~Long, ~Lat,
                 label = ~htmlEscape(Name), 
                 labelOptions = labelOptions(noHide = T, 
                                              #direction = "top",
                                              #direction = "bottom",
                                              #direction = "left",
                                              #direction = "right",
                                              direction = ~MyDirection))

m

【问题讨论】:

  • 我不确定您是否可以按自己的方式指定方向。您可能只能为方向指定一个值。

标签: r leaflet label r-leaflet


【解决方案1】:

我想分享我的最新方法。我终于设法根据 mydf$MyDirection 设置标签方向。我没有像在前面的示例中那样添加多个图层,而是使用了库“Purrr”。这极大地减少了层数。

最好的问候

#Libraries----
library(leaflet)
library(htmltools)
library(htmlwidgets)
library(purrr)
#---Data Input----
mydf  <- read.csv(textConnection("
Facility,Lat,Long,MyDirection
ANN,51.19,4.46277778,right
BAB,43.26306,-2.94972222,left
BCN,41.29694,2.07833333,top
BCN2,41.29694,2.07833333,bottom
"))

#---Create Vector----
ob_Facility <- mydf %>% 
  split(., .$Facility)
#---Create Map----
m <- leaflet() %>% 
  addTiles() 

#---Purrr Layers----
names(ob_Facility) %>%
  purrr::walk(function(mydf) {
    m <<- m %>%
      addCircleMarkers(data=ob_Facility[[mydf]],
                       lng=~Long, lat=~Lat,
                       group = "Show All",
                       label = ~Facility,
                       labelOptions = labelOptions(noHide = T,direction = ~MyDirection))
  })

#---Layers control----
m %>%
  addLayersControl(
    overlayGroups = "Show All",
    options = layersControlOptions(collapsed = FALSE)  
  )%>%
  #---Save as HTML File----
saveWidget('Example Go Live Date.html', selfcontained = TRUE)

【讨论】:

    【解决方案2】:

    您好,

    我想出了一个 4 层(上、下、左、右)的解决方法,并为每个层附加了相同的组名。

    library(leaflet)
    
    
    df  <- read.csv(textConnection("
    Name,Lat,Long,MyDirection
    ANN,51.19,4.46277778,right
    BAB,43.26306,-2.94972222,left
    BCN,41.29694,2.07833333,top
    BCN,41.29694,2.07833333,bottom
    "))
    
    
    #---Create 4 additional DFs (1 for each dirction)----
    dfLeft = df[df$MyDirection == "left", ]
    dfRight = df[df$MyDirection == "right", ]
    dfTop = df[df$MyDirection == "top", ]
    dfBottom = df[df$MyDirection == "bottom", ]
    
    #---Create Map----
    m <- leaflet(df) %>% 
      addTiles(group = "OSM (default)") %>%
    
      addCircles(~dfLeft$Long, ~dfLeft$Lat, color = '#d40511',
                 label = ~htmlEscape(dfLeft$Name), 
                 labelOptions = labelOptions(noHide = T, 
                                             direction =  "left"),
                 group = "Show All")%>%
    
      addCircles(~dfRight$Long, ~dfRight$Lat, color = '#d40511',
                 label = ~htmlEscape(dfRight$Name), 
                 labelOptions = labelOptions(noHide = T, 
                                             direction =  "right"),
                 group = "Show All")%>%
    
      addCircles(~dfTop$Long, ~dfTop$Lat, color = '#d40511',
                 label = ~htmlEscape(dfTop$Name), 
                 labelOptions = labelOptions(noHide = T, direction = "top",
                                             offset = c(0, -2)),
                 group = "Show All")%>%
    
      addCircles(~dfBottom$Long, ~dfBottom$Lat, color = '#d40511',
                 label = ~htmlEscape(dfBottom$Name), 
                 labelOptions = labelOptions(noHide = T, direction = "bottom",
                                             offset = c(0, 2)),
                 group = "Show All")
    
    m
    

    【讨论】:

    • 我想这是您目前实现目标的唯一方法。我很高兴你找到了自己的方式。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-28
    • 1970-01-01
    • 1970-01-01
    • 2010-10-12
    • 2020-07-25
    • 2021-01-09
    相关资源
    最近更新 更多