I'm trying to edit a leaflet
legend. Does anyone know how to edit the number of breaks
and where to place tick-marks
when using a predefined palette from colorNumeric
(not colorQuantile
, see here) in labelFormat
? This is the legend I get (code below) is:
As you can see, it is a highly skewed legend and would make more sense if the breaks were at c(1, 3, 6, 9)
or something similar that could easily be edited including limits.
You can include bins
but these according to the help file will be equally spaced:
From this link it seems you can do it with colorQuantile
but thats not what I want here. I think I may need to go through the source code as in this example but I can't see where I could edit breaks
or tick-marks.
Reproducible code following my other question :
#following my other question:
#https://stackoverflow.com/questions/60321495/merging-palettes-with-colorramppalette-and-plotting-with-leaflet/
library(leaflet)
library(tidyverse)
set.seed(8)
df <- data.frame(var = rnorm(100)^2)
df <- df %>%
arrange(var) %>%
mutate(ranking = 1:n())
threshold <- sum(df$var < 1)
# number of values below threshold
threshold #= 67
### Create an asymmetric color range
## Make vector of colors for values smaller than 1
rc1 <- colorRampPalette(colors = c("#006837", "#1A9850"), space = "Lab")(threshold)
## Make vector of colors for values larger than 1
rc2 <- colorRampPalette(colors = c("#FDAE61", "#A50026"), space = "Lab")(length(df$var) - threshold)
## Combine the two color palettes
rampcols <- c(rc1, rc2)
# Create a palette for a legend with ranking again. But this time with
# colorNumeric()
mypal2 <- colorNumeric(palette = rampcols, domain = df$ranking)
leaflet() %>%
addLegend(pal = mypal2, values = df$ranking,
#bins = 10,
labFormat = labelFormat(transform = function(x) df$var[x]))
Would anyone have any suggestions? thanks