简体   繁体   English

在图例中操纵“失踪”label:tmap

[英]Manipulating “missing” label in legend: tmap

I want to manipulate the "Missing" label in my legend.我想在我的传说中操纵“丢失”的 label。

图像1

I'm using the tmap function in R.我在 R 中使用 tmap function。 I want to change it to read "Missing or not eligible to gentrify"我想将其更改为“缺少或没有资格进行绅士化”

I have tried using the tm_text function and considered how I would change the label in the underlying data, but haven't found a solution.我尝试使用 tm_text function 并考虑如何更改基础数据中的 label,但没有找到解决方案。 Also, note the code uses a mapping function.另外,请注意代码使用映射 function。

########################
# categorical mapping function 
########################

cat.maps.wide.function <- function(data, varname, ltitle, colorplaette){
  tm_shape(data, unit = "mi") +
    tm_polygons(col = varname , # add variable(s)
                style = "cat", # set break pattern to object LQ.cuts
                palette = colorplaette,  # set color fill to blue refreshing
                border.col = "grey40", # color the borders white
                border.alpha = 0.5, # set border transparency
                title = ltitle, # title for legend
                colorNA = "white") + # color of missing data
    tm_style("bw") +
    tm_layout(panel.label.bg.color ="NA", 
              frame = FALSE, 
              bg.color = "transparent") + # panel label color
    tm_legend(legend.title.fontface = 2,  # legend bold
              legend.title.size = 0.75, 
              legend.text.size = 0.65, 
              legend.bg.alpha = 0, 
              legend.width = 5) + 
    tm_scale_bar(color.dark = "gray60", # Customize scale bar and north arrow
                 position = c(0.6, 0.05)) +  # set position of the scale bar
    tm_compass(type = "4star", 
               size = 2.5, # set size of the compass
               fontsize = 0.5, # set font size of the compass
               color.dark = "gray60", # color the compass
               text.color = "gray60", # color the text of the compass
               position = c(0.5, 0.05)) +  # set position of the compass

    # add border names
    tm_shape(boro.boundaries) + 
    tm_borders(alpha = .5) + 
    tm_text("boro", 
            size = 0.75, 
            remove.overlap = TRUE, 
            auto.placement=FALSE, 
            xmod= "x", ymod= "y") 

}

########################
# change in residential housing price
########################

# object for 2016 variable                                                 
mt1pva5.2016 <- cat.maps.wide.function(
  data = data.map.tract.wide,
  varname = "chgpcmt1pva5_overlap2016", 
  colorplaette = mt1pva5.overlap.colors, 
  ltitle = "Change in residential housing price for eligible tracts ")

mt1pva5.2016

the tm_layout() functin has the labels argument which lets you maniputale the label of the legend passing a character vector. tm_layout()函数具有标签参数,可让您通过字符向量操作图例的 label。 In yout tm_layout add something like:在 yout tm_layout 添加如下内容:

tm_layout(labels = c("Decrease in residential housing price", 
                     "Increase in residential housing price", 
                     "Missing or not eligible to gentrify")

Thanks for sharing the data.感谢您分享数据。 Apparently, you can use labels in tm_polygons but not to change the NA values.显然,您可以在tm_polygons中使用labels ,但不能更改NA值。 For that, you also will need textNA :为此,您还需要textNA

tm_polygons(col = varname , # add variable(s)
                style = "cat", # set break pattern to object LQ.cuts
                palette = colorplaette,  # set color fill to blue refreshing
                border.col = "grey40", # color the borders white
                border.alpha = 0.5, # set border transparency
                title = ltitle, # title for legend
                colorNA = "white", # color of missing data
                textNA = "Missing or not eligible to gentrify",
                labels = c("Decrease in residential housing price", 
                           "Increase in residential housing price")) +
...

带有 NA 自定义标签的 tmap

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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