操纵图例中的 "missing" 标签:tmap
Manipulating "missing" label in legend: tmap
我想操纵图例中的 "Missing" 标签。
我在 R 中使用 tmap 函数。我想将其更改为 "Missing or not eligible to gentrify"
我尝试使用 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
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")
感谢您分享数据。显然,您可以在 tm_polygons
中使用 labels
但不能更改 NA
值。为此,您还需要 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")) +
...
我想操纵图例中的 "Missing" 标签。
我在 R 中使用 tmap 函数。我想将其更改为 "Missing or not eligible to gentrify"
我尝试使用 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
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")
感谢您分享数据。显然,您可以在 tm_polygons
中使用 labels
但不能更改 NA
值。为此,您还需要 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")) +
...