操作图例中的“缺失”标签:tmap

时间:2019-10-11 19:36:21

标签: r legend tmap

我想在图例中操作“缺少”标签。

img1

我在R中使用了tmap函数。我想将其更改为“缺少或没有资格绅士化”

我尝试使用tm_text函数,并考虑了如何更改基础数据中的标签,但尚未找到解决方案。另外,请注意代码使用了映射功能。

########################
# 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

2 个答案:

答案 0 :(得分:0)

tm_layout()函数具有 labels 参数,该参数使您可以通过字符向量来模拟图例的标签。在您的tm_layout中添加以下内容:

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

答案 1 :(得分:0)

感谢您共享数据。显然,您可以在0(Zero)中使用# Sample data set with one pair of Slot+Period that doesn't have any SortNumber==1 dfAccess <- data.frame(Slot = c("X1", "X2", "X3", "X1", "X2", "X3","X1", "X2", "X3","X1", "X2", "X3"), Period = c(1,1,1,2,2,2,1,1,1,2,2,2), SortNumber = c(1, 1, 2, 1,1,1,2,2,2,2,2,2), Value = c(9,5,7,9,1,5,6,4,8,10,2,1)) # Your command will generate an error dfAccess <-dfAccess %>% group_by(Slot, Period) %>% mutate(DiffValue = Value - Value[SortNumber == 1] ) # ​Error: Column `DiffValue` must be length 2 (the group size) or one, not 0 # This command will only take 1st value of SortNumber==1 in any pair Slot+Number dfAccess %>% group_by(Slot, Period) %>% mutate(DiffValue = Value - first(Value[SortNumber==1], default=0)) %>% ungroup() ,但不能更改labels的值。为此,您还需要tm_polygons

NA

tmap with custom label for NA