tmap: how do I customize breaks in legend by adding ">o"?

i am working to building a map and I have having troubles with the breaks, I want values above zero to show up with a colour. If not it can be grey. i used the code below:

testmap <- tm_shape(map)+
   tm_fill(col="per.content",
           breaks = c(0,2,5,10,15,25,Inf),
           textNA = "Not enough information",
           colorNA = "grey",
           title = " ",
           palette = MyColors)+
   tm_shape(districtsg)+
   tm_borders(col = "white")

I want a way to have only values > than zero show up in colour.I used ">0" to replace "0" but it showed up as a potential error.

What is the best way to add to the breaks?

There are several options for you to consider; as your example is not exactly reproducible I am using the North Carolina shapefile that is distributed with the {sf} package (and thus widely available).

# dramatis personae: 3 packages & a shapefile
library(tmap)
library(dplyr)
library(sf)

# NC counties - a shapefile shipped with the sf package
shape <- st_read(system.file("shape/nc.shp", package ="sf")) %>% 
  st_transform(shape, crs = 4326) # WGS84, just because...

The first scenario is the baseline: a {tmap} map, with a red yellow blue palette for Births 1974 - 1978 of the North Carolina SIDS dataset (i.e. some data...)

# plain vanilla - base scenario
tm_shape(shape) + tm_fill("BIR74", palette = "-RdYlBu")

The first modification you may consider is using the NA mapping feature - i.e. first setting a value to NA, and then making use of colorNA and textNA arguments of your {tmap} call.

In this case I am setting to NA all records with BIR74 value less than ten thousand

# set BIR74 of less than 10K to NA
shape <- shape %>% 
  mutate(BIR74a = ifelse(BIR74 > 10e3, BIR74, NA))

# modified scenario - with special NA value
tm_shape(shape) + tm_fill("BIR74a", palette = "-RdYlBu",
                          colorNA = "grey75",
                          textNA = "lower than low...")

This might be an option in your case of "less then 0" use case, though note how the automatic legend struggles with the (now very limited) range of values.

The last option I propose is taking full control of the legend binning process yourself, and replace your continuous variable with a factor. I am intentionally replacing any kind of meaningful labels with gibberish - a great power should come with a great responsibility, or maybe not.

You might use dplyr::case_when() here but I kind of like base::cut() as it is a bit less verbose.

# converting continuous variable to factor
shape <- shape %>% 
  mutate(BIR74b = cut(BIR74, 
                      breaks = c(0, 2500, 5000, 12500, +Inf),
                      labels = c("eeny", "meeny", "miney", "mo")))

# modified scenario - with factor instead of range
tm_shape(shape) + tm_fill("BIR74b", palette = "-RdYlBu")

I believe you would be best served by the middle option - replacing values of zero and less by NA - but you may also consider the full manual override of factor conversion.

Thank you, this is exactly what I needed. I ended changing the 0 to NA.Thank you for your help.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.