Remove legend in tmap in R

不问归期 提交于 2020-07-03 16:59:51

问题


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

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!


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/57031445/remove-legend-in-tmap-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!