简体   繁体   中英

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")

在此处输入图像描述

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") 

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM