在传单图例中创建自定义标签

Create Custom Labels in Leaflet Legend

Map Image

我在传单中创建了一张地图。图例当前编号为 0、2、4、6、8。但是,我只想使用两个标签。一种说法 'Less Dense' 另一种说法 'More Dense'.

我目前正在使用合并到每个国家/地区记录的人口密度列的形状文件。

在此先感谢您的帮助。

我的地图是这样的,见附图。

这是我目前用来创建地图的代码

library(here) #create wkd
library(utils) #unzip file
library(rgdal) #read the shape file
library(dplyr) #clean the data
library(leaflet) #creates interactive graph
library(RColorBrewer) #modifies colour pallette with custom bins
library(viridis) #allows for additional colour pallettes

# Create a color palette for the map:

# Call the color function (colorBin) to create a new palette function. Domain allows the colouring of continuous data

mypalettepop <- colorNumeric( palette="Purples", domain=worldCountries@data$pop_density_log)

# Pass the palette function a data vector to get the corresponding colors
mypalettepop(c(45,43))

#create a highlight
highlight <- highlightOptions(
  weight = 2,
  color = "white",
  fillOpacity = 1.0,
  opacity = 1.0,
  bringToFront = TRUE)

labels_pop <- c("Less Dense", "", "", "", "More Dense") # added labels to over ride the 5 previous labels

# Final Map
pop_map <- leaflet(worldCountries) %>% 
  addTiles()  %>% 
  setView( lat=10, lng=0 , zoom=2) %>%
  addPolygons( 
    fillColor = ~mypalettepop(pop_density_log), 
    stroke=TRUE, 
    fillOpacity = 0.9, 
    color="white", 
    weight=0.7,
    label = mytext,
    labelOptions = label,
    highlightOptions = highlight  
  )%>% 
  addLegend( 
    pal=mypalettepop, 
    values=~pop_density_log, 
    opacity=0.9, 
    title = "Population Density per KM2", 
    position = "bottomleft",
    labels = labels_pop #added labels but still will not change
  ) 

pop_map

labels 参数是图例中与 colors 参数相对应的文本标签向量,而不是 addLegend() 中的 pal 参数

library(rgdal)

# From http://data.okfn.org/data/datasets/geo-boundaries-world-110m
countries <- readOGR("https://rstudio.github.io/leaflet/json/countries.geojson")
map <- leaflet(countries) %>% addTiles()
pal <- colorNumeric(
  palette = "YlGnBu",
  domain = countries$gdp_md_est
)
map %>%
  addPolygons(stroke = FALSE, smoothFactor = 0.2, fillOpacity = 1,
              color = ~pal(gdp_md_est)
  ) %>%
  addLegend("bottomright", colors = c('red', 'orange', 'yellow', 'green', 'blue'), values = ~gdp_md_est,
            title = "Est. GDP (2010)",
            labels = c("Less Dense", "", "", "", "More Dense"),
            opacity = 1
  )

此示例使用示例数据,因为我无法访问您的数据