tmap
tmap copied to clipboard
NA argument does not work on tm_add_legend
data(World) tm_shape(World) + tm_polygons(col='black', alpha=0.5) + tm_add_legend(type=c('fill'), col='black', labels=c('labels'), alpha=0.5)

NA argument in the manually added legend does not change the fill transparency. I found people complained about this on StackOverflow as well: https://stackoverflow.com/questions/59838780/tmap-tm-add-legend-alpha-doesnt-work-for-fill
I am also curious if there is a way to enact automatic legend (with parameters in tm_polygons) because I have to define manual legend every time if I am not using tm_fill.
Just fixed the bug that alpha
didn't work for tm_add_legend
. Thanks for the find!
What do you mean with your last sentence?
For example, with the sample code I have, I have to manually repeat the parameters I used in tm_polygons in tm_add_legend (col='black', alpha = 0.5). If I have multiple tm_polygons layers, I have to manually add multiple tm_add_legend for each and repeat the parameter values. I know such a thing exists for tm_fill, as in the legend show automatically and can be defined in tm_fill arguments, but tm_fill only works if the layers are just different values in a variable. Is there a way that I can have the legend shown automatically by adjusting parameters in tm_polygons? I originally thought the "legend.show=TRUE" argument in tm_polygons will have the legend shown automatically, but it is not working (idk if I interpret it wrong or it is simply not working).
In I understand your use case correctly: why not combine all the polygons into one sf
object (with rbind
), and create a standard legend for that? In this way, you don't need tm_add_legend
.
Otherwise, please give an example that illustrates you use case.
library(tmap)
data(World)
points = st_centroid(World) %>% select(c(continent))
tm_shape(World) +
tm_polygons(col='black', alpha=0.5) +
tm_shape(points) +
tm_symbols(size=0.1, col='red') +
tm_add_legend(type=c('fill'), col='black', labels=c('continents'), alpha=0.5) +
tm_add_legend(type=c('symbol'), col='red', labels=c('centroid'))

So here is an example. I have two layers. One is points and the other is polygons. I am trying to generate a plot that overlay the two. They have a different number of columns, so rbind will not work. You can see that I have to manually add parameters in tm_add_legend that are the same as those in the tm_polygons and tm_symbols. In ArcGIS, when generating the legends, it will automatically take on the parameters (e.g., color, alpha etc.) used for each layer. I wonder if this is the most efficient way to do what I want because when the number of layers increases, I have to do a lot of manual work to create legends. Ideally, I will want a parameter in tm_polygons (or tm_symbols) that allows me to set legend = TRUE, and the legend can automatically take on the parameters defined in tm_polygons.
Is this clear? I know my question has gone beyond the original post and thanks for tagging along. I can move it to a new post if this is worth discussing. I am writing a tutorial using tmap for my field of study so I want to make sure I am writing the most efficient codes. Thanks!
[I am cleaning older issues that have a low chance to be implemented in the foreseeable future]