Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change color of leaflet marker

Tags:

r

leaflet

Is there anyway to change the color of leaflet marker base on the value of some variable. In the following map, for example, I wish to assign marker color based on mag variable:

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))
like image 975
M.Qasim Avatar asked Oct 05 '15 02:10

M.Qasim


People also ask

How do I change the marker color?

To edit the marker color, click or tap on the marker icon. When you do that, you can change both the color of the marker and its style. Go for the color or style you want to change and then click OK to see the effect. Click on the button labeled Done to save your new marker color settings.

How do you add a marker on leaflet?

Adding a Simple Marker Step 1 − Create a Map object by passing a <div> element (String or object) and map options (optional). Step 2 − Create a Layer object by passing the URL of the desired tile. Step 3 − Add the layer object to the map using the addLayer() method of the Map class.

How many markers can leaflet handle?

The Clusterer can handle 10,000 or even 50,000 markers (in chrome).

How do you remove a marker in leaflet?

You just need to filter the clicked marker' coords not to be included anymore to your state variable and that's it!


2 Answers

I often use the circle markers because you can change both the size and color based on other variables. For example, I have created a binned variable from a continuous using the following code:

# 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) 

When you plot it, I use the code for circle markers. The radius/area of the circle is based on the actual value of the factor and then color is assigned according to the bins.

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') %>% 

At the end of your code add a legend. I added some formatting.

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

This gives you color-coded and sized circles based on a variable and a nice legend.

enter image description here

like image 157
Bryan Butler Avatar answered Sep 25 '22 14:09

Bryan Butler


As far as I know, you need to assign an image file to one level of icon. For instance, if you have three levels in magnitude in the earthquake data, you need to create an icon list with three image paths. Then, you can have three different colors in markers. At least, the following example is getting closer to what you want. I edited a png file and created three png files. You need to specify the paths of the file when you make an icon list.

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

like image 30
jazzurro Avatar answered Sep 22 '22 14:09

jazzurro