Search code examples
rtmap

Remove or customize legend in tmap "view" mode


I'm creating an interactive map in view mode with tmap package. Can someone please help me to remove the legend from tmap. I understand that tm_layout options do not work for "view" mode. So, what is the way to remove the legend from view mode? Or is there a way to customize the legend so that it only appears once outside the map?

Here is the code I have used:

library(sf)
library(tmap)
library(tidyverse)
KL_raw <- read_excel("KL.xlsx")
dput(KL_raw)
structure(list(Location = c("Alleppey", "Alleppey", "Alleppey", 
"Calicut", "Calicut", "Calicut", "Ernakulam", "Ernakulam", "Ernakulam", 
"Idukki", "Idukki", "Idukki", "Kannur", "Kannur", "Kannur", "Kasargod", 
"Kasargod", "Kasargod", "Kochi", "Kochi", "Kochi", "Kollam", 
"Kollam", "Kollam", "Kottayam", "Kottayam", "Kottayam", "Malappuram", 
"Malappuram", "Malappuram", "Palakkad", "Palakkad", "Palakkad", 
"Pathanamthitta", "Pathanamthitta", "Pathanamthitta", "Thrissur", 
"Thrissur", "Thrissur", "Trivandrum", "Trivandrum", "Trivandrum", 
"Wayanad", "Wayanad", "Wayanad"), Year = c(2021, 2022, 2023, 
2021, 2022, 2023, 2021, 2022, 2023, 2021, 2022, 2023, 2021, 2022, 
2023, 2021, 2022, 2023, 2021, 2022, 2023, 2021, 2022, 2023, 2021, 
2022, 2023, 2021, 2022, 2023, 2021, 2022, 2023, 2021, 2022, 2023, 
2021, 2022, 2023, 2021, 2022, 2023, 2021, 2022, 2023), Mobility = c(4, 
13, 25, 35, 88, 131, 35, 85, 183, 3, 14, 15, 6, 20, 25, 0, 3, 
6, 12, 23, 29, 8, 21, 40, 35, 53, 110, 2, 20, 48, 7, 14, 24, 
6, 10, 15, 29, 102, 132, 87, 75, 116, 3, 7, 15), X = c("8497384.983310532", 
"8497384.983310532", "8497384.983310532", "8435275.170162767", 
"8435275.170162767", "8435275.170162767", "8490794.745767247", 
"8490794.745767247", "8490794.745767247", "8567229.95496439", 
"8567229.95496439", "8567229.95496439", "8390572.602244912", 
"8390572.602244912", "8390572.602244912", "8347955.357610422", 
"8347955.357610422", "8347955.357610422", "8487598.670420328", 
"8487598.670420328", "8487598.670420328", "8526567.264133664", 
"8526567.264133664", "8526567.264133664", "8518408.78234175", 
"8518408.78234175", "8518408.78234175", "8469184.509474115", 
"8469184.509474115", "8469184.509474115", "8532870.85398816", 
"8532870.85398816", "8532870.85398816", "8547909.900739763", 
"8547909.900739763", "8547909.900739763", "8484020.614609588", 
"8484020.614609588", "8484020.614609588", "8565745.942463793", 
"8565745.942463793", "8565745.942463793", "8472342.086830465", 
"8472342.086830465", "8472342.086830465"), Y = c("1062220.305235952", 
"1062220.305235952", "1062220.305235952", "1259936.576874495", 
"1259936.576874495", "1259936.576874495", "1117112.2947996475", 
"1117112.2947996475", "1117112.2947996475", "1102450.522637486", 
"1102450.522637486", "1102450.522637486", "1331665.0532438054", 
"1331665.0532438054", "1331665.0532438054", "1403149.788617227", 
"1403149.788617227", "1403149.788617227", "1115243.7149433082", 
"1115243.7149433082", "1115243.7149433082", "993415.5871096748", 
"993415.5871096748", "993415.5871096748", "1072771.6322972549", 
"1072771.6322972549", "1072771.6322972549", "1237056.8643535364", 
"1237056.8643535364", "1237056.8643535364", "1205892.1224562458", 
"1205892.1224562458", "1205892.1224562458", "1035996.3714154225", 
"1035996.3714154225", "1035996.3714154225", "1178354.0909352647", 
"1178354.0909352647", "1178354.0909352647", "948415.1484541291", 
"948415.1484541291", "948415.1484541291", "1313307.407647557", 
"1313307.407647557", "1313307.407647557")), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -45L))
KL <- KL_raw %>% mutate(X = as.numeric(X), Y = as.numeric(Y))
KL_sf <- st_as_sf(KL, coords = c("X", "Y"), crs = 3857)

Output

I need the legends on the top right corner (panels with color coded 2021-2023) removed. From html developer tools, I could identify that these panels fall under "leaflet" function but any functions associated with tm_leaflet do not work on view mode. I do not have any coding/ data science background. I just have very basic knowledge of using R for data visualization. Any help will be much appreciated. Thank you.


Solution

  • To remove the legend in tmap_mode("view"), you can use the following code

    library(sf)
    library(tmap)
    library(tidyverse)
    
    KL <- KL_raw %>% mutate(X = as.numeric(X), Y = as.numeric(Y))
    KL_sf <- st_as_sf(KL, coords = c("X", "Y"), crs = 3857)
    
    tmap_mode("view")
    tm_shape(KL_sf)+
      tm_dots(col = "Year",
                 size = "Mobility",
                 border.col = "black",
                 border.lwd = 2, legend.show = FALSE)+
      tm_facets("Year",
                nrow = 1,
                sync = FALSE)
    

    enter image description here

    Or if you want to place the legend in empty space at the bottom left you can use

    tm_shape(KL_sf)+
      tm_dots(col = "Year",
              size = "Mobility",
              border.col = "black",
              border.lwd = 2, legend.show = T)+
      tm_facets("Year",
                nrow = 1,
                sync = FALSE) +
      tm_view(view.legend.position = c("left", "bottom")) 
    

    enter image description here