tmap icon indicating copy to clipboard operation
tmap copied to clipboard

legend ticks/labels downscaling inconsistent

Open mtennekes opened this issue 10 months ago • 6 comments

tm_shape(World) +
	tm_polygons(fill = "gender",
				fill.scale = tm_scale_continuous(values = "-hcl.cyan_magenta")) +
	tm_bubbles(size = "pop_est") +
	tm_compass() +
	tm_scalebar() + tm_layout(scale = 0.4)

Image

mtennekes avatar Feb 07 '25 10:02 mtennekes

I've also encountered this issue today. Is there a workaround available? I've tried to change the text size of the legend, but with no effect...

Nowosad avatar Feb 07 '25 13:02 Nowosad

@mtennekes I've tried a few scale values (e.g., 1 and 2) and it seems that the legend labels' size does not change with the different scale values. E.g., try 1 and 2 in the code below and notice that the size of the "missing" text is constant.

# remotes::install_github("r-tmap/tmap")
library(tmap)
data(World)

tm_shape(World) +
  tm_polygons(fill = "gender",
              fill.scale = tm_scale_continuous(values = "-hcl.cyan_magenta")) +
  tm_bubbles(size = "pop_est") +
  tm_compass() +
  tm_scalebar() + tm_layout(scale = 1, 
                            legend.position = c("left", "bottom"))

Nowosad avatar Feb 07 '25 19:02 Nowosad

@Nowosad That's a tricker one, because the labels also have impact on the overal legend size. What happens under the hood by design:

  • In advance the legend (or component) height and width are computed, taking scaling into account.
  • If the required minimum height cannot be obtained, the legend/component is scaled down such that the layout does not change, at least that is the aim.
tm_shape(World) +
	tm_polygons(fill = "gender",
				fill.scale = tm_scale_continuous(values = "-hcl.cyan_magenta")) +
	tm_bubbles(size = "pop_est") +
	tm_compass() +
	tm_scalebar() + tm_layout(scale = 4, 
							  legend.position = c("left", "bottom"))

Image

The fill legend does not fit, so it's scaled down. (To be consistent the border and tick marks should also not become thicker when following this approach).

A shorter legend will give room for the labels to grow:


tm_shape(World) +
	tm_polygons(fill = "gender",
				fill.scale = tm_scale_continuous(values = "-hcl.cyan_magenta", ticks = c(0.2,0.6))) +
	tm_bubbles(size = "pop_est") +
	tm_compass() +
	tm_scalebar() + tm_layout(scale = 4, 
							  legend.position = c("left", "bottom"))

Image

What I could do is to let the labels still grow, even if they don't fit.

mtennekes avatar Feb 08 '25 20:02 mtennekes

For now, it fixes my issue -- I will play with the tick argument. I do not have any strong suggestions here.

Nowosad avatar Feb 09 '25 12:02 Nowosad

Not working correctly....

Not just oversized legends, but the general scale argument does not work well:

tm_shape(metro) +
  tm_bubbles(
    size = "pop2030") +
  tm_layout(scale = 2)

Image

Tokyo is less than 40mln but bubble suggests otherwise

mtennekes avatar Feb 14 '25 15:02 mtennekes

The last issue is solved:

tm_shape(metro) +
	tm_bubbles(
		size = "pop2030") +
	tm_layout(scale = 2)

Image

mtennekes avatar Feb 14 '25 19:02 mtennekes

Will close this issue now. All inconsistencies seem to be solved. Bubbles are still overlapping with tm_layout(scale = 4) but that is unavoidable.

mtennekes avatar Jul 24 '25 09:07 mtennekes