R语言 修正传单图例,使其显示线条而不是方框

ekqde3dh  于 2023-04-27  发布在  其他
关注(0)|答案(1)|浏览(157)

我正在用传单制作一张Map,上面显示了2000年的街区,以及2000年和2010年的大片。现在,我无法让图例正确工作:它显示彩色框而不是彩色线:

az_2000 <- tracts(state = "Arizona", year = 2000) %>% 
  select(c(CTIDFP00)) %>% rename("geoid" = "CTIDFP00")
az_2000 <- st_transform(az_2000, 4326)

az_bg2000 <- blocks(state = "Arizona", year = 2000) %>% 
  select(c(BLKIDFP00)) %>% rename("geoid" = "BLKIDFP00")
az_bg2000 <- st_transform(az_bg2000)

az_2010 <- tracts(state = "Arizona", year = 2010) %>% 
  select(c(GEOID10)) %>% rename("geoid" = "GEOID10")
az_2010 <- st_transform(az_2010, 4326)
    

# Create a map with the three layers
map <- leaflet() %>%
  addTiles() %>%
  addPolygons(data = az_bg2000,
              fill = FALSE,
              color = "#000000",
              dashArray = "2",
              weight = 0.5,
              label = "Blocks 2000") %>%
  addPolygons(data = az_2000, 
              fill = FALSE, 
              color = "#0000FF", 
              weight = 3,
              label = "Tracts 2000") %>%
  addPolygons(data = az_2010, 
              fill = FALSE, 
              color = "#FF0000",
              dashArray = "5,5",
              weight = 3,
              label = "Tracts 2010") %>%
  addLegend(position = "bottomright", 
            colors = c("#000000", "#0000FF", "#FF0000"), 
            labels = c("Blocks 2000", "Tracts 2000", "Tracts 2010"), 
            title = "Legend")

我该怎么解决这个问题?

hgc7kmma

hgc7kmma1#

也许这是一个小技巧,但你可以使用一个onRender函数来应用一些css到图例元素,使它们看起来像你想要的。

# Create a map with the three layers
leaflet() %>%
  addTiles() %>%
  addLegend(position = "bottomright", 
            colors = c("#000000", "#0000FF", "#FF0000"), 
            labels = c("Blocks 2000", "Tracts 2000", "Tracts 2010"), 
            title = "Legend") %>%
  htmlwidgets::onRender("
    function(el, x) {
      let legend = document.getElementsByClassName('info legend leaflet-control')[0].getElementsByTagName('i')
      for (let i of legend){
        i.style.transform = 'scale(1, 0.2)'
      }
    }")

相关问题