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

问题描述 投票:0回答:1

地图图像

我在传单中创建了一张地图。图例当前编号为 0、2、4、6、8。但是,我只想要两个标签。一个说“不太密集”,另一个说“更密集”。

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

预先感谢您的帮助。

我的地图看起来像这样,如附图所示。

这是我当前用于创建地图的代码

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
r label legend r-leaflet
1个回答
6
投票

labels 参数是图例中文本标签的向量,对应于颜色参数,而不是 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
  )

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

sample

© www.soinside.com 2019 - 2024. All rights reserved.