geo_rkal
geo_rkal

Reputation: 43

tmap tm_add_legend alpha doesn't work for "fill"

I'm trying to change the alpha value of my legend to match that of the polygon fill, however nothing seems to happen when I specify the alpha argument in tm_add_legend. Is there something blindingly obvious that i'm missing here?

library(tmap)
library(sf)

nc <- st_read(system.file("shape/nc.shp", package="sf"))

tm_shape(nc) + 
tm_polygons(col = "#326abf", border.col = "white", lwd = 2, alpha = 0.5) +
tm_add_legend(type = "fill", 
              alpha = 0.5,
              labels = "NC",
              col = "#326abf")

enter image description here

Upvotes: 1

Views: 744

Answers (1)

jazzurro
jazzurro

Reputation: 23574

I am not sure how tmap exactly works in this case. But I found a workaround. I created a dummy variable called mycat. Then, I used this variable for col in tm_polygons() and specify the color you are using in palette. In this way, you do not have to use tm_add_legend().

library(dplyr)
library(sf)
library(tmap)

mutate(nc, mycat = "one") -> nc

tm_shape(nc) +
tm_polygons(col = "mycat", 
            palette = "#326abf",
            border.col = "white", lwd = 2, alpha = 0.5,
            title = "",
            labels = "NC") 

enter image description here

Upvotes: 3

Related Questions