Remove legend in tmap in R
Asked Answered
H

2

6

I have a simple question: How does one remove an automatically added legend in tmap in R?

In this case, I want to remove that legend on the right, depicting 'level'.

enter image description here

Here's what I've tried:

tm_shape(densities$polygons)+
  tm_polygons(col='level', palette='Reds', alpha=0.5, border.col = 'transparent') + 
  tm_legend(show=FALSE)

I have also tried:

tm_shape(densities$polygons)+
  tm_polygons(col='level', palette='Reds', alpha=0.5, border.col = 'transparent') + 
  tm_layout(legend.show=FALSE)

Not sure why but none of these removes the legend. Would appreciate any solutions, thank you!

Historiated answered 14/7, 2019 at 21:57 Comment(2)
I don't know tmap, but since it is related to ggplot, did you try legend.position="none"?Coreencorel
Just tried: tm_shape(densities$polygons)+ tm_polygons(col='level', palette='Reds', alpha=0.5, border.col = 'transparent') + tm_view(view.legend.position="none") Doesn't work unfortunately :(Historiated
D
19

Try this:

library(tmap)

tmap_mode("view")

tm_shape(densities$polygons) +
  tm_polygons(col = "plz", legend.show = FALSE) 

tm_layout() doesn't work in your case because you are in view-mode. Several other options for the legend in view mode won't work with tm_layout either, such as legend.position = (there is tm_view() for this)

If you switch to tmap_mode("plot") your code will work.

Depressive answered 15/7, 2019 at 5:2 Comment(0)
G
2

Instead of tm_polygons use tm_fill(title = "") to remove the legend title. This worked for me.

Gooey answered 3/7, 2020 at 2:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.