I am a great fan of the tmap package by Martijn Tennekes. It is a very versatile package, and my favorite tool for visualization of geospatial data. The package is, by necessity, somewhat complex and some clever and very useful features were not obvious (to me) at the first glance.

This includes advanced formatting of legend. I would therefore like to share some tips and tricks I wish I had learned the easy way by googling, and not the hard way by trial and error.

Hiding not applicable values

On some occasions it is desirable to hide N/A values. For example when drawing polygons for metro areas and leaving the rural areas blank (but still displayed for context). This is achieved by using showNA = F parameter of the tm_fill call.

Showing special format for not applicable values

On some occasions it is desirable to display the N/A values with a special label. For example when drawing polygons for administrative areas, and explaining in the legend why some are intentionally left blank - I have used this when drawing military training grounds (areas of special administrative status). This is achieved by using textNA = "special" parameter of the tm_fill call.

Advanced formatting of legend (intervals)

On some occasions it is desirable to avoid the default separator “to” in intervals. For example when drawing plots that are labeled in a different language than English. This is achieved by using the legend.format = list(text.separator= "-") parameter of the tm_style call. The dash is my favorite text separator by far - it is not language specific.

Advanced formatting of legend (currency and percentages)

On some occasions it is desirable to display the values of legend in special format: as currency, or as percentages.

This is achieved by using fun argument of the legend.format parameter of the tm_fill call. For example legend.format = list(fun = function(x) paste0(formatC(x, digits = 0, format = "f"), " Kč"))) will truncate the legend items to zero decimal places and apply “Kč” (i.e. Czech Crowns) as currency symbol.

Using percentage sign is analogic to currency symbol; the only trick is to remember to multiply the value by 100 at exactly one place (either in raw data, or when displaying legend).
This call from tm_fill assumes that data has been multiplied at source legend.format=list(fun=function(x) paste0(formatC(x, digits=0, format="f"), " %")).