tmap icon indicating copy to clipboard operation
tmap copied to clipboard

tm_scale_discrete returns non-discrete values (e.g., 1.5)

Open Nowosad opened this issue 6 months ago • 1 comments

@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())

Nowosad avatar Jun 17 '25 10:06 Nowosad

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())

Image

nz$val = rep(c(1,2,5,7), 4)

tm_shape(nz) +
	tm_polygons(fill = "val",
				fill.scale = tm_scale_discrete())

Image

However, in your example it is strange that the 1.5 value is there.

mtennekes avatar Jun 17 '25 12:06 mtennekes

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.

mtennekes avatar Jun 25 '25 11:06 mtennekes