Skip to content

tm_legend(frame = FALSE) doesn't remove legend's frame when used inside tm_raster() #1120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
shringi opened this issue May 25, 2025 · 1 comment
Assignees
Labels
Legends Issues related to legends Plot-mode Static mode issues

Comments

@shringi
Copy link

shringi commented May 25, 2025

I think tm_legend(frame = FALSE) inside tm_raster() doesn't work as expected, however, it works as usual for tm_polygon().
It starts to work okay when a map has both tm_raster() and tm_polygon() layers added in a single map.

The following is an example I could reproduce.

library(tmap)

packageVersion("tmap")
#> [1] '4.1'
packageDate("tmap")
#> [1] "2025-05-25"
tmap_options(component.autoscale = FALSE)

The following plot doesn’t remove the legend frame when tm_legend(frame = FALSE) inside tm_raster()

rast_Frame_F <- tm_shape(land) +
  tm_raster("trees", 
            col.legend = tm_legend(frame = F, title = "Trees \n(Rast Frame = F)"))

rast_Frame_F

Image

However, the option works when used inside the tm_polygon()

poly_Frame_F <- tm_shape(World) +
  tm_borders(col = "area", 
             col.legend = tm_legend(frame = F, title = "Area \n(Poly Frame = F)"))

poly_Frame_F
#> [tip] Consider a suitable map projection, e.g. by adding `+ tm_crs("auto")`.
#> This message is displayed once per session.

Image

Frame appears as expected when tm_legend(frame = TRUE)

rast_Frame_T <- tm_shape(land) +
  tm_raster("trees", 
            col.legend = tm_legend(frame = T, title = "Trees \n(Rast Frame = T)"))
poly_Frame_T <- tm_shape(World) +
  tm_borders(col = "area", 
             col.legend = tm_legend(frame = T, title = "Area \n(Poly Frame = T)"))

However, the options start to work according to the first map layer setting of tm_legen() when the two layers are combined. Examples are as follows.

poly_Frame_T + rast_Frame_F # T

Image

poly_Frame_F + rast_Frame_T # F

Image

rast_Frame_T + poly_Frame_F # T

Image

rast_Frame_F + poly_Frame_T # F

Image

Created on 2025-05-25 with reprex v2.1.1

Session info

sessionInfo()
#> R version 4.5.0 (2025-04-11 ucrt)
#> Platform: x86_64-w64-mingw32/x64
#> Running under: Windows 11 x64 (build 22631)
#> 
#> Matrix products: default
#>   LAPACK version 3.12.1
#> 
#> locale:
#> [1] LC_COLLATE=English_India.utf8  LC_CTYPE=English_India.utf8   
#> [3] LC_MONETARY=English_India.utf8 LC_NUMERIC=C                  
#> [5] LC_TIME=English_India.utf8    
#> 
#> time zone: Asia/Calcutta
#> tzcode source: internal
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] tmap_4.1
#> 
#> loaded via a namespace (and not attached):
#>  [1] s2_1.1.9                class_7.3-23            lwgeom_0.2-14          
#>  [4] KernSmooth_2.23-26      lattice_0.22-7          digest_0.6.37          
#>  [7] magrittr_2.0.3          evaluate_1.0.3          grid_4.5.0             
#> [10] RColorBrewer_1.1-3      fastmap_1.2.0           e1071_1.7-16           
#> [13] leafsync_0.1.0          DBI_1.2.3               crosstalk_1.2.1        
#> [16] viridisLite_0.4.2       cols4all_0.8            XML_3.99-0.18          
#> [19] codetools_0.2-20        abind_1.4-8             cli_3.6.5              
#> [22] rlang_1.1.6             units_0.8-7             tmaptools_3.2          
#> [25] reprex_2.1.1            base64enc_0.1-3         withr_3.0.2            
#> [28] yaml_2.3.10             leaflegend_1.2.1        tools_4.5.0            
#> [31] raster_3.6-32           parallel_4.5.0          maptiles_0.10.0        
#> [34] colorspace_2.1-1        spacesXYZ_1.5-1         logger_0.4.0           
#> [37] R6_2.6.1                png_0.1-8               proxy_0.4-27           
#> [40] lifecycle_1.0.4         classInt_0.4-11         leaflet_2.2.2          
#> [43] leaflet.providers_2.0.0 fs_1.6.6                htmlwidgets_1.6.4      
#> [46] terra_1.8-50            data.table_1.17.2       glue_1.8.0             
#> [49] Rcpp_1.0.14             sf_1.0-21               xfun_0.52              
#> [52] rstudioapi_0.17.1       knitr_1.50              dichromat_2.0-0.1      
#> [55] htmltools_0.5.8.1       rmarkdown_2.29          leafem_0.2.4           
#> [58] wk_0.9.4                compiler_4.5.0          sp_2.2-0               
#> [61] stars_0.6-8
@mtennekes
Copy link
Member

Thx @shringi for this clear report.

It seems to be an issue with tm_raster() only. I'll look into it.

You can set the frame color to missing to make it work for the time being:

tm_shape(land) +
	tm_raster("trees", 
			  col.legend = tm_legend(frame = FALSE, frame.color = NA))

@mtennekes mtennekes self-assigned this May 28, 2025
@mtennekes mtennekes added Plot-mode Static mode issues Legends Issues related to legends labels May 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Legends Issues related to legends Plot-mode Static mode issues
Projects
None yet
Development

No branches or pull requests

2 participants