7

I want to manipulate the "Missing" label in my legend.

img1

I'm using the tmap function in R. 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. Also, note the code uses a mapping 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
camille
  • 16,432
  • 18
  • 38
  • 60
kaseyzapatka
  • 149
  • 2
  • 9
  • @Ben Here's a [link](https://drive.google.com/open?id=1SfczvBhD2Omp6IN-FjJlkxckYXtsFx2S) to what I'm working with. Let me know if you figure something out. I thought it was because the table were being overwritten by a latter command, but that didn't seem to be the case. – kaseyzapatka Oct 11 '19 at 23:01

2 Answers2

8

Thanks for sharing the data. Apparently, you can use labels in tm_polygons but not to change the NA values. For that, you also will need 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")) +
...

tmap with custom label for NA

Ben
  • 28,684
  • 5
  • 23
  • 45
0

the tm_layout() functin has the labels argument which lets you maniputale the label of the legend passing a character vector. In yout tm_layout add something like:

tm_layout(labels = c("Decrease in residential housing price", 
                     "Increase in residential housing price", 
                     "Missing or not eligible to gentrify")
Orlando Sabogal
  • 1,470
  • 7
  • 20
  • Thanks for this suggestion, but it keeps saying that there is an used function (i.e., the labels function. I couldn't find a labels function in the documentation Error it kept throwing me: `"Error in tm_layout(legend.title.fontface = 2, legend.title.size = 0.85, : unused argument (labels = c("Decrease in residential housing price", "Increase in residential housing price", "Missing or not eligible to gentrify"))"` – kaseyzapatka Oct 11 '19 at 21:47