改变传单标记的颜色

M.Q*_*sim 14 r 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))
Run Code Online (Sandbox Code Playgroud)

jaz*_*rro 12

据我所知,您需要将图像文件分配到一个级别的图标.例如,如果地震数据中有三个级别,则需要创建一个包含三个图像路径的图标列表.然后,您可以在标记中使用三种不同的颜色.至少,以下示例越来越接近您想要的.我编辑了一个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])
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • @ M.Qasim我更新了我的答案.我想如果我们想为制造商分配不同的颜色,我们基本上需要自己创建标记的图像文件.请看一看.我希望这会让你前进. (2认同)

M.Q*_*sim 11

这个对我有用:

资料来源: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)
Run Code Online (Sandbox Code Playgroud)


snk*_*his 5

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


Bry*_*ler 5

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

# 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)
Run Code Online (Sandbox Code Playgroud)

当您绘制它时,我将代码用于圆圈标记。圆的半径/面积基于因子的实际值,然后根据分档分配颜色。

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') %>%
Run Code Online (Sandbox Code Playgroud)

在代码末尾添加图例。我添加了一些格式。

  addLegend('bottomright', pal = beatCol, values = last$BeatHomeLvl,
            title = 'Compare Home<br>Quote Count to<br>3Mos State Avg',
            opacity = 1)
Run Code Online (Sandbox Code Playgroud)

这使您可以根据变量和漂亮的图例进行颜色编码和大小调整的圆。

在此处输入图片说明