5

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!

imguessing
  • 377
  • 1
  • 3
  • 9
  • I don't know tmap, but since it is related to ggplot, did you try `legend.position="none"`? – January Jul 14 '19 at 22:18
  • 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 :( – imguessing Jul 14 '19 at 22:32

2 Answers2

17

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.

Humpelstielzchen
  • 6,126
  • 3
  • 14
  • 34
1

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

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
alevy02
  • 11
  • 1