tm_polygons border not displayed in legend in view mode
Sort of duplicate of / connected to #51
library(tmap)
library(dplyr)
data("NLD_prov")
# A. add borders layer to highlight certain provinces
prov_map <- qtm(NLD_prov) +
tm_shape(filter(NLD_prov, origin_non_west > 10),
name = "Provinces with populations of non-western origin > 10%") +
tm_borders(col = "deeppink") +
# necessary since tm_borders does not provide a legend (see issue #51)
tm_add_legend(type = "fill",
border.col = "deeppink",
col = "white",
alpha = 0,
labels = "Provinces with\npopulations of non-western\norigin > 10%")
tmap_mode("plot")
prov_map # legend ok
ttm()
prov_map # legend not ok in view mode
# B. attempt with tm_polygons instead of tm_borders: similar result to A
prov_map_poly <- qtm(NLD_prov) +
tm_shape(filter(NLD_prov, origin_non_west > 10),
name = "Provinces with populations of non-western origin > 10%") +
tm_polygons(col = "white", # no legend generated because col not mapped to variable
alpha = 0,
border.col = "deeppink") +
tm_add_legend(type = "fill",
col = "white",
alpha = 0,
border.col = "deeppink",
labels = "Provinces with\npopulations of non-western\norigin > 10%")
tmap_mode("plot")
prov_map_poly
ttm()
prov_map_poly
# C. Test to see what is different if col is mapped to a variable
prov_map_poly_mapped <- qtm(NLD_prov) +
tm_shape(filter(NLD_prov, origin_non_west > 10),
name = "Provinces with populations of non-western origin > 10%") +
tm_polygons(col = "origin_non_west", # col mapped to variable
alpha = 0,
border.col = "deeppink")
prov_map_poly_mapped
The pink border does not get displayed on the legend in view mode in any of the above three examples.
This is a leaflet (R-package) shortcoming. Legend elements other than squares without borders cannot be drawn. However, it should be easy with Javascript. @tim-salabim Do you know of any implementations for this, or initiatives to do this?
No I don't but have been pondering the idea of a leafkey/leaflegend package for a while. Would you be interested in a shared effort?
There's a few plugins that could be interesting, e.g. https://github.com/consbio/Leaflet.HtmlLegend
And we could collate a few random solutions like this one https://gis.stackexchange.com/questions/341979/leaflet-make-legend-with-different-circle-sizes or this one https://cartographicperspectives.org/index.php/journal/article/view/cp76-donohue-et-al/1307
Another thing that would be nice is different point shapes (a feature request that pops up once in a while) + their repsective legend shapes/sizes
Maybe even worth a RConsortium grant application...?
It works in tmap v4.
library(sf)
library(tmap)
library(spData)
tmap_mode("view")
tm_shape(world) + tm_borders(col = "continent")