tmap
tmap copied to clipboard
tm_scale_discrete returns non-discrete values (e.g., 1.5)
@mtennekes -- is this behavior expected?
library(tmap)
library(spData)
nz$val = c(rep(1, 8), rep(2, 8))
tm_shape(nz) +
tm_polygons(fill = "val",
fill.scale = tm_scale_discrete())

Yes and no.
The ticks of a discrete scale (in the current implementation) are not necessarily integers; they can be real numbers, as long the differences between subsequent numbers is strictly positive and constant. So 1.0, 1.5, 2.0, 2.5, 3.0, 3.5 etc could be a valid discrete scale.
The algorithm tries to find the 'intended' scale for the present data values. E.g.:
nz$val = rep(c(1.5,2.5,5,7.5), 4)
tm_shape(nz) +
tm_polygons(fill = "val",
fill.scale = tm_scale_discrete())
nz$val = rep(c(1,2,5,7), 4)
tm_shape(nz) +
tm_polygons(fill = "val",
fill.scale = tm_scale_discrete())
However, in your example it is strange that the 1.5 value is there.
First example works: the problem was that pretty(1:2, n = 2) returns c(1, 1.5, 2). I've found out that pretty(1:2, n = 1) returns c(1, 2) so that is useful.