Hello,
I'm mapping some data with R's diverging color palette RdBu. My values are continuous and some dip below 0. I'm trying to figure out how to center the palette so the values above 0 take on a blue hue, the values below 0 take on a red hue, and those at 0 are white (the center of the RdBu palette). The hue direction is right so far, just not the "center".
Here's a quick reprex with some dummy data. I set the Texas value at 0.0 as a test element.
library(tidyverse)
library(tigris)
library(leaflet)
set.seed(617)
states <- states(cb=T)
# Fake data
dat <- tibble(NAME = states@data$NAME,
dat = rnorm(56, .2, .15))
# 0.0 Texas
dat[50,2] <- 0.0
new_states <- merge(states, dat)
pal <- colorNumeric("RdBu", domain = new_states$dat)
leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
setView(-98.483330, 38.712046, zoom = 4) %>%
addPolygons(data = new_states,
fillColor = ~pal(new_states$dat),
fillOpacity = 0.7,
weight = 0.2,
smoothFactor = 0.2,
popup = ~ paste0(str_extract(NAME, "^([^,]*)"), "<br>", dat)) %>%
addLegend(pal = pal,
values = new_states$dat,
position = "bottomright",
title = "Values")