rtmap

How to do a map with negative values, midpoint equal to 0 and positive values using tmap?


I'm currently working on creating a map using tm_polygons in R, and I'm specifically interested in implementing the Jenks classification for styling. My goal is to classify the values into three distinct ranges: strictly negative values, zero, and strictly positive values.

Here's a prototype of the classification and color scheme I'm aiming for:

-50 to -21 (red-type of color); -20 to -10 (red-type of color); -9 to -1 (red-type of color); 0 (white); 1 to 10 (blue-type of color); 10 to 30 (blue-type of color); 50 to 70 (blue-type of color)

I would appreciate any guidance or code snippets on how to achieve this classification using tm_polygons and ensuring the correct styling with the specified color ranges.

I have tried the following but without success:

tm_shape(df_name %>% filter(ANO == 2012), bbox = bbox_new) +
tm_polygons(col = "tot_reloc_net_05",
            style = "jenks" , palette = "RdBu", midpoint = 0, title = "", n = 5) + 
  tm_layout(main.title = "", 
            main.title.position = "center" ,frame = F, main.title.size = 1.5, fontfamily = "serif", fontface = "bold",
            legend.width = 0.40, legend.outside = F, legend.position = c(0.75, 0.25),legend.text.size = 0.5, legend.title.size = 1.0)+
  tm_shape(df_shape_1)+
  tm_borders(lwd = 1.85, col = "black")+
  tm_text("MA", xmod = -1.25, ymod = -0.995, size = 0.6) +
  tm_shape(df_shape_2)+
  tm_borders(lwd = 0.5, col = "black")+
  tm_compass(type = "arrow", position = c("right", "bottom"), size = 1.35)+
  tm_scale_bar(text.size = 0.45, position = c("right", "bottom"), width = 0.18)

Solution

  • Hard to debug it without your data, but I guess, the main issue is mixing the jenks style with desired classification. Please note, jenks style uses own bins for classification. Below examples of jenks and fixed styles classifications with same number of bins. Please note, I'm using newest version of {tmap} (you can install it from github) with tmap::tm_scale_xxxxx() functions, but (if I recall) in tmap v.3 you can use style and breaks as parametrs for tm_polygons().

    pl <- geodata::gadm("POL", level = 2, path = "/home/sapi/projekty/test/data") |>
      sf::st_as_sf() |>
      sf::st_simplify(dTolerance = 1000)
    pl$n <- sample(c(-50:50), nrow(pl), replace = TRUE)
    
    tmap::tm_shape(pl) +
      tmap::tm_polygons(fill = "n",
                        fill.scale = tmap::tm_scale_intervals(
                          n = 8,
                          values = "RdBu",
                          style = "jenks",
                          midpoint = 0
                        ))
    

    
    tmap::tm_shape(pl) +
      tmap::tm_polygons(fill = "n",
                        fill.scale = tmap::tm_scale_intervals(
                          values = "RdBu",
                          style = "fixed",
                          breaks = c(-50, -30, -10, -1, 0, 1, 10, 30, 50),
                          midpoint = 0
                        ))
    

    
    
    packageVersion("tmap")
    #> [1] '3.99.9000'
    

    Created on 2024-02-03 with reprex v2.1.0