rr-leaflet

Leaflet in R not showing variable circle colors


After perusing the Intro to Leaflet, I tried to work with a dataset, but kelp getting no color for circles or markers when I attempted to use the formula interface. I've recreated a toy example, and, as you can see, something is off. Although it might be me! So:

library("leaflet")
set.seed(100)
pdf <- data.frame(Latitude = runif(100, -90,90), Longitude = runif(100, -180,180))

#just red
leaflet(pdf) %>% addTiles()  %>%
  addCircleMarkers(lat = ~ Latitude, lng = ~ Longitude, color= "red")

Works just fine with a nice map showing red circle markers.

Red map

But. Adding color seems to bork the markers entirely.

#let's add some color!
pdf$Study <- factor(rep(1:10,10))
cols <- rainbow(length(levels(pdf$Study)))
pdf$colors <- cols[unclass(pdf$Study)]

leaflet(pdf) %>% addTiles()  %>%
  addCircleMarkers(lat = ~ Latitude, lng = ~ Longitude, color= ~ colors)

This should be colorful

An inspection of the colors column of pdf shows that all should be bright and beautiful, and yet, alas. What's going on here?


Solution

  • By default, rainbow returns colors in #RRGGBBAA format, which Leaflet can't use. If you add the argument alpha=NULL then it should work fine.