R 传单标签集方向

R Leaflet Label Set direction

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

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

任何 Idea/solution 将不胜感激。

提前致谢。

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

您好,

我想出了一个解决方法,有 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

我想分享一下我的最新方法。我终于设法根据 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)