改变leaflet标记的颜色

23

有没有办法根据某个变量的值改变leaflet标记的颜色。例如,在下面的地图中,我希望根据 mag 变量分配标记颜色:

library(leaflet)

data(quakes)

# Show first 20 rows from the `quakes` dataset
leaflet(data = quakes[1:20,]) %>% addTiles() %>%
  addMarkers(~long, ~lat, popup = ~as.character(mag))

我想知道是否有适用于R的Leaflet.awesome-markers插件v2.0版本。 - M.Qasim
5个回答

27

我经常使用圆形标记,因为您可以根据其他变量更改大小和颜色。例如,我使用以下代码从连续变量创建了一个分箱变量:

# first cut the continuous variable into bins
# these bins are now factors
last$BeatHomeLvl <- cut(last$BeatHome, 
                        c(0,.5,1,2,3,5,100), include.lowest = T,
                        labels = c('<.5x', '.5-1x', '1-2x', '2-3x', '3-5x','5x+'))

# then assign a palette to this using colorFactor
# in this case it goes from red for the smaller values to yellow and green
# standard stoplight for bad, good, and best
beatCol <- colorFactor(palette = 'RdYlGn', last$BeatHomeLvl)

当你绘制它时,我使用圆形标记的代码。圆的半径/面积基于因子的实际值,然后根据箱分配颜色。
m1 <- leaflet() %>%
  addTiles() %>%
  addProviderTiles(providers$OpenStreetMap, group = 'Open SM')  %>%
  addProviderTiles(providers$Stamen.Toner, group = 'Toner')  %>%
  addProviderTiles(providers$Esri.NatGeoWorldMap, group = 'NG World') %>%
  setView(lng = -72, lat = 41, zoom = 8) %>%

      addCircleMarkers(data = Jun, lat = ~Lat, lng = ~Lon,
                       color = ~beatCol(BeatHomeLvl), popup = Jun$Popup,
                       radius = ~sqrt(BeatHome*50), group = 'Home - Jun') %>%

在你的代码末尾添加一个图例。我加入了一些格式。
  addLegend('bottomright', pal = beatCol, values = last$BeatHomeLvl,
            title = 'Compare Home<br>Quote Count to<br>3Mos State Avg',
            opacity = 1)

这将为您提供基于变量的彩色编码和大小不同的圆圈,以及一个漂亮的图例。

enter image description here


你从哪里获取了 last$BeatTotalLvl? - user34018
不知道为什么你的答案没有获得最高的赞数。它比其他答案更简单和清晰。 - Marco Fumagalli
last$BeatTotalLv 是数据框 last 中的一个字段;它是通过将一个连续变量转换为平均值比率而创建的。 - Bryan Butler

21
据我所知,您需要为一个图标等级分配一个图像文件。例如,如果在地震数据中有三个级别的大小,您需要创建一个带有三个图像路径的图标列表。然后,您可以在标记中使用三种不同的颜色。至少,以下示例接近您想要的内容。我编辑了一个png文件并创建了三个png文件。制作图标列表时,您需要指定文件的路径。
library(dplyr)
library(leaflet)

mutate(quakes, group = cut(mag, breaks = c(0, 5, 6, Inf), labels = c("blue", "green", "orange"))) -> mydf

### I edit this png file and created my own marker.
### https://raw.githubusercontent.com/lvoogdt/Leaflet.awesome-markers/master/dist/images/markers-soft.png
quakeIcons <- iconList(blue = makeIcon("/Users/jazzurro/Documents/Stack Overflow/blue.png", iconWidth = 24, iconHeight =32),
                       green = makeIcon("/Users/jazzurro/Documents/Stack Overflow/green.png", iconWidth = 24, iconHeight =32),
                       orange = makeIcon("/Users/jazzurro/Documents/Stack Overflow/orange.png", iconWidth = 24, iconHeight =32))


leaflet(data = mydf[1:100,]) %>% 
addTiles() %>%
addMarkers(icon = ~quakeIcons[group])

输入图像描述


谢谢您的回答。我想自定义标记的颜色,而不是圆圈... https://github.com/lvoogdt/Leaflet.awesome-markers - M.Qasim
1
@M.Qasim 我明白了。我知道如何将不同的标记分配给不同级别的变量。但我不知道如何更改图标的颜色。据我所知,一个级别只能分配一个图标图像(png文件)。在使用leaflet之前,您需要修改png文件的颜色。 - jazzurro
2
@M.Qasim 我更新了我的回答。我认为如果我们想要给标记分配不同的颜色,我们基本上需要自己创建标记的图像文件。请看一下。我希望这能让你继续前进。 - jazzurro

15
这个对我有用:
源代码:https://github.com/bhaskarvk/leaflet/blob/master/inst/examples/awesomeMarkers.R
library(leaflet)

icon.glyphicon <- makeAwesomeIcon(icon= 'flag', markerColor = 'blue', iconColor = 'black')
icon.fa <- makeAwesomeIcon(icon = 'flag', markerColor = 'red', library='fa', iconColor = 'black')
icon.ion <- makeAwesomeIcon(icon = 'home', markerColor = 'green', library='ion')

# Marker + Label
leaflet() %>% addTiles() %>%
  addAwesomeMarkers(
    lng=-118.456554, lat=34.078039,
    label='This is a label',
    icon = icon.glyphicon)

leaflet() %>% addTiles() %>%
  addAwesomeMarkers(
    lng=-118.456554, lat=34.078039,
    label='This is a label',
    icon = icon.fa)

leaflet() %>% addTiles() %>%
  addAwesomeMarkers(
    lng=-118.456554, lat=34.078039,
    label='This is a label',
    icon = icon.ion)

# Marker + Static Label using custom label options
leaflet() %>% addTiles() %>%
  addAwesomeMarkers(
    lng=-118.456554, lat=34.078039,
    label='This is a static label',
    labelOptions = labelOptions(noHide = T),
    icon = icon.fa)

很高兴看到你找到了一个例子。我想知道你的机器上安装了哪个版本的leaflet包。由于我的版本中不存在addAwesomeMarkers(),所以我无法运行上面的代码。 - jazzurro
1
Leaflet 版本 1.0.1.9000。您可能需要删除库中现有的 Leaflet 目录,因为在现有包的顶部安装更新一开始并不起作用... - M.Qasim
1
请安装以下内容以获取addAwesomeMarkers()函数。devtools::install_github('bhaskarvk/leaflet') - M.Qasim
谢谢留言,我会去做的。 :) - jazzurro

5
为什么不使用基于SVG的矢量标记(这里有一个示例实现 - https://github.com/hiasinho/Leaflet.vector-markers),您可以应用任何您想要的填充颜色,而无需创建大量静态图像文件。需要涉及一些代码,但更加灵活。

2

L.Marker使用了两张图片(一个用于标记,一个用于阴影),所以不能直接更改。不过你可以使用自己的图片,Leaflet网站的教程中有一篇很好的文章介绍了这个主题:

http://leafletjs.com/examples/custom-icons.html


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接