Legend: side titles
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) )
Set col.free = FALSE in tm_raster.
Set
col.free = FALSEintm_raster.
Thanks @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?
I hope to be able to have the legend ticks and labels this way:
Regarding the position of the legend: you could do , position = tm_pos_out("center", "bottom", "center").
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.
Rounded edges: add item.r = 0 to tm_legend
The other legend design: to be implemented.
Thanks, @mtennekes .
How do we produce an outside portrait legend so that the title can be flipped?
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)
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?
I hope to be able to have the legend ticks and labels this way:
This feature further discussed #1175