改变传单标记的颜色

时间:2015-10-05 02:51:10

标签: 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))

5 个答案:

答案 0 :(得分: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])

enter image description here

答案 1 :(得分:9)

这个对我有用:

来源: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', prefix='fa', iconColor = 'black')
icon.ion <- makeAwesomeIcon(icon = 'home', markerColor = 'green', prefix='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)

答案 2 :(得分:3)

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

答案 3 :(得分:2)

L.Marker使用图像(一个用于标记,一个用于阴影),这是不可能的。然而,您可以使用自己的图像,在Leaflet网站上的教程中有一个很好的主题:

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

答案 4 :(得分:2)

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

# 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