0 not being mapped to white in diverging palette

49 Views Asked by At

I am currently generating several maps with diverging palettes. Below is an example.

hom_trend_2019_2023 = (tm_shape(houston_zip) + 
tm_polygons(col="hom_change_2019_2023", border.alpha=0.1, 
            palette="-RdBu", style="fixed",
            breaks=c(-Inf,-50,-40,-30,-20,-10,0,10,20,30,40,50,Inf),
            title="Change in Homicide Rate,\n2019 to 2023 (Percent)") + 
    tm_legend(bg.color="white", outside=TRUE, outside.size=0.15) + 
    tm_shape(tx_hwys) + tm_lines(col="black"))

tmap_save(hom_trend_2019_2023,"./plots/homicide_trend_2019_2023.png")

An issue I am having is that values equal to 0 are being mapped in light red instead of white. Homicide Map

0 is specified as a break, and I have tried other diverging palettes, but none map 0 to white.

1

There are 1 best solutions below

0
Nir Graham On

I think what you are seeing is expected behaviour given your choice of breaks. White is for the 'middle' category. but there isnt one per say. you are passing a vector of breaks to make levels out of, and the result is 12 levels (being an even number) there are two candidates that can compete to be considered in the middle. whereas if you had 13 levels, there would be an uncontested middle (the 7th level with 6 levels below and 6 levels above

length(levels(cut(1:100,c(-Inf,-50,-40,-30,-20,-10,0,10,20,30,40,50,Inf))))
# 12