tmap icon indicating copy to clipboard operation
tmap copied to clipboard

NA argument does not work on tm_add_legend

Open xiaofanliang opened this issue 4 years ago • 4 comments

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)

Screen Shot 2020-03-27 at 2 40 47 PM

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.

xiaofanliang avatar Mar 27 '20 18:03 xiaofanliang

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?

mtennekes avatar Apr 01 '20 16:04 mtennekes

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

xiaofanliang avatar Apr 05 '20 02:04 xiaofanliang

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.

mtennekes avatar Apr 05 '20 06:04 mtennekes

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'))
Screen Shot 2020-04-24 at 11 43 52 AM

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!

xiaofanliang avatar Apr 24 '20 15:04 xiaofanliang

[I am cleaning older issues that have a low chance to be implemented in the foreseeable future]

Nowosad avatar Sep 18 '23 07:09 Nowosad