Given the following example data
mydata <- data.frame(
lat = c(21.05939, 21.04305, 21.05977, 21.04336, 21.04434),
lng = c(92.22692 ,92.23357 ,92.22733 ,92.23361 ,92.23478),
X1 = c("sometimes", "always", "never", "often", "rarely")
)
And the following Leaflet plot:
pal1 <- c("#003366","#00ced1", "#ffd700","#ffa500","#ff1a1a")
color <- colorFactor(pal1, domain = mydata$X1)
leaflet(data = mydata) %>%
addTiles() %>%
addCircleMarkers(lng = mydata$lng,
lat = mydata$lat,
color = ~color(mydata$X1)) %>%
addLegend("topright",
pal=color,
values=mydata$X1,
opacity = 1)
How can I manipulate the order of labels in the legend so that they are:
always,
often,
sometimes,
rarely,
never
I have attempted to specify the levels
argument in colorFactor()
and have also attempted the same with the values
argument in addLegend
However, the legend still resorts to alphabetical order of the items.
NVM I think I figured it out. I first specified a sort order by:
sort_val = factor(mydata$X1, levels = c('always',
'often',
'sometimes',
'rarely',
'never'))
I then passed sort_val
to the values
argument in addlegend()
addLegend("topright",
pal=color,
values=sort_val,
opacity = 1)
I think this is correct unless anyone can suggest an alternative?