tmap icon indicating copy to clipboard operation
tmap copied to clipboard

Legend: side titles

Open aonojeghuo opened this issue 10 months ago • 9 comments

Hi everyone. Great to see Tmap version 4 come so far. I'm still getting used to replicating ideas from version 3 to 4. I'm trying to plot a raster stack and want only one legend position outside (center, bottom). I've tried and cant figure out how to force it. even when I use tm_add_legend for a custom legend, I get 3 legends. How do I fix this in tmap version 4 please?

Here's my code and output:

library(terra)
library(tmap)
# Create sample data
m <- matrix(1:25, nrow=5, ncol=5)
rm <- rast(m)
# Stack and assign names
Rstack <- c(rm,rm,rm)
names(Rstack) <- c("A1","A2","A3")
#Plot
tm_shape(Rstack) + tm_raster(col_alpha=0.8,col.scale = tm_scale_intervals(values =  "brewer.yl_or_rd",n=5,style="fisher"),  col.legend = 
tm_legend(title = "Mean Frequency (Days)", orientation = "landscape", position = tm_pos_out("center", 
"bottom"),title.fontface="bold",title.padding=c(0,0,0,0), title.align="center", frame = F) ) + 
tm_layout(legend.title.size = 0.8,legend.text.size = 0.7,frame = F,legend.position=c("center","bottom"), 
title.position=c("center","top"),legend.outside=F,legend.show=T, inner.margins=c(0.1,0,0,0), 
panel.labels = c("(a)","(b)  ","(c) "), panel.label.bg.color=NA,panel.label.frame=NA,panel.label.fontface= "bold") + 
tm_options(raster.max_cells = c(plot=1e+15, view=1e+15)  ) 

Image

aonojeghuo avatar Feb 10 '25 16:02 aonojeghuo

Set col.free = FALSE in tm_raster.

mtennekes avatar Feb 10 '25 20:02 mtennekes

Set col.free = FALSE in tm_raster.

Thanks @mtennekes .

aonojeghuo avatar Feb 10 '25 20:02 aonojeghuo

Thanks @Nowosad and @mtennekes the legend is still one sided at the bottom. Is there a way to centralize this that I haven't added yet? added legend.frame.r=0 and the legend item edges are still rounded. How do I fix this? Also, if I wanted the legend with ticks and numbers on one side lie the second picture, how do I specify this, please?

Image

I hope to be able to have the legend ticks and labels this way: Image

aonojeghuo avatar Feb 11 '25 15:02 aonojeghuo

Regarding the position of the legend: you could do , position = tm_pos_out("center", "bottom", "center").

Nowosad avatar Feb 11 '25 15:02 Nowosad

Regarding the position of the legend: you could do , position = tm_pos_out("center", "bottom", "center").

Thanks @Nowosad . So, just the rounded edges on items, ticks and labels in between colors that are pending.

Image

aonojeghuo avatar Feb 11 '25 15:02 aonojeghuo

Rounded edges: add item.r = 0 to tm_legend

The other legend design: to be implemented.

mtennekes avatar Feb 11 '25 21:02 mtennekes

Thanks, @mtennekes .

How do we produce an outside portrait legend so that the title can be flipped?

Image

Image

aonojeghuo avatar Feb 11 '25 21:02 aonojeghuo

How do we produce an outside portrait legend so that the title can be flipped?

Also not yet. This has lower priority than the ticks/labels in between. However, it can be done with some tweaking:

tm_shape(World) + 
  tm_polygons(
    "HPI",
    fill.legend = tm_legend("", margins = c(0.5,4,0.5,0.5))) +
tm_title("Happy\nPlanet\nIndex", size = 0.8, 
  position = tm_pos_out("right", "center", pos.h = 0.03, pos.v = 0.85, just.v = "center"))

Unfortunately, I don't have a rotation argument in tm_title yet (should be added).

If you want rotated text you can resort to grid:

grid::grid.text("Manual title", x = 0.95, y = 0.85, rot = 90)

Image

mtennekes avatar Feb 13 '25 08:02 mtennekes

Thanks @Nowosad and @mtennekes the legend is still one sided at the bottom. Is there a way to centralize this that I haven't added yet? added legend.frame.r=0 and the legend item edges are still rounded. How do I fix this? Also, if I wanted the legend with ticks and numbers on one side lie the second picture, how do I specify this, please?

Image

I hope to be able to have the legend ticks and labels this way: Image

This feature further discussed #1175

mtennekes avatar Aug 13 '25 05:08 mtennekes