3

I have a shapefile of water from Spain and boundaries shapefile of Spain.

boundaries shapefile called boundaries and water shapfile is called water

boundaries <- st_read("boundaries_spain.shp")
water <- st_read("water_spain.shp")

I changed the names of the shapefile. boundaries are the administrative areas and water is the inland water

Both shapefiles can be found here https://www.diva-gis.org/gdata

I used that code but seems not work to add lakes into a legend

 tm_shape(boundaries) +
  tm_borders("grey", lwd = 1.25) + tm_shape(water_shp) + tm_fill("blue", title.col="water bodies") 

But it doesn't work that an output with a legend

the website seems to be the best solution I can find since I am not able to share my data

John Coleman
  • 51,337
  • 7
  • 54
  • 119

1 Answers1

2

As requested, I leave my code here as a reference. Adding tm_add_legend() does the job you asked.

library(sf)
library(tmap)

spain <- st_read("ESP_adm2.shp")
water <- st_read("ESP_water_areas_dcw.shp")

tm_shape(spain) +
  tm_borders("grey", lwd = 1.25) +
  tm_shape(water) + 
  tm_fill("blue", title.col="water bodies") +
  tm_add_legend('Lake', 
                type = "fill",
                col = "blue",
                border.col = "blue",
                title = "Lake") -> g

tmap_save(g, "spain_water.png")

enter image description here

jazzurro
  • 23,179
  • 35
  • 66
  • 76