Manipulating "missing" label in legend: tmap - r

I want to manipulate the "Missing" label in my legend.
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

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

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

Related

Labeling contour lines in R using metR with gratia or ggplot2

Some ways to add labels on contour plots
# load packages
library('mgcv')
library('gratia') # draw(); smooth_estimates()
library('metR') # geom_contour2(); geom_text_contour()
library('ggplot2')
Simulate data using the example from Gavin Simpson's website: https://fromthebottomoftheheap.net/2018/10/23/introducing-gratia/
set.seed(1)
dat <- gamSim(2, n = 4000, dist = "normal", scale = 1, verbose = FALSE)
mod <- gam(y ~ s(x, z, k = 30), data = dat$data, method = "REML")
sm <- smooth_estimates(mod); sm
Plot using gratia with the number of contour lines automatically adjusted:
draw(mod) +
geom_text_contour(
aes(z = est), # 'est' from smooth_estimates(mod)
colour = "black", size = 4.5, fontface = "bold",
stroke = 0.3, stroke.colour = "white", # 'stroke' controls the width of stroke relative to the size of the text
skip = 0, # number of contours to skip
rotate = FALSE, # horizontal labeling; if TRUE, rotate text following the contour
label.placer = label_placer_fraction(frac = 0.5)) # 'frac = 0.5' places the label at equal distance from extremities. Try 'label.placer = label_placer_n(2)' to display two labels per contour line
However, contour lines and labeling do no longer match if we use e.g. 'n_contour = 10' within draw().
To allow this matching, use 'n_contour = 0' within draw(), define 'binwidth' within geom_contour2() and 'breaks' within geom_text_contour(), as follows.
Plot using gratia::draw with 'binwidth'-adjusted contour lines:
min(sm$est); max(sm$est) # find min() and max() for adjusting the 'est' z-scale
draw(mod, n_contour = 0) +
geom_contour2(aes(z = est), binwidth = 0.2) +
geom_text_contour(
aes(z = est), # 'est' from smooth_estimates(mod)
breaks = seq(-0.4, 0.4, by = 0.2), # 'breaks' must match with 'binwidth' above
colour = "black", size = 4.5, fontface = "bold",
stroke = 0.3, stroke.colour = "white", # 'stroke' controls the width of stroke relative to the size of the text
skip = 0, # number of contours to skip
rotate = FALSE, # horizontal labelling; if TRUE, rotate text following the contour
label.placer = label_placer_fraction(frac = 0.5)) # 'frac = 0.5' places the label at equal distance from contour lines' extremities. Try 'label.placer = label_placer_n(2)' to display two labels per contour line
Also possible to customize the graph directly with ggplot2:
ggplot(data = sm, aes(x = x, y = z, z = est)) +
geom_contour2(aes(z = est), binwidth = 0.1) +
geom_text_contour(
aes(z = est), # 'est' from smooth_estimates(mod)
breaks = seq(-0.4, 0.4, by = 0.1), # 'breaks' instead of 'bins' to not have too many decimals
colour = "black", size = 4.5, fontface = "bold",
stroke = 0.3, stroke.colour = "white", # 'stroke' controls the width of stroke relative to the size of the text
skip = 0, # number of contours to skip
rotate = FALSE, # horizontal labelling; if TRUE, rotate text following the contour
label.placer = label_placer_fraction(frac = 0.5)) # 'frac = 0.5' places the label at equal distance from contour lines' extremities. Try 'label.placer = label_placer_n(2)' to display two labels per contour line
You can use geom_textcontour from geomtextpath to obtain nicely placed labels without having to tweak lots of different parameters:
library(geomtextpath)
ggplot(sm, aes(x, z, z = est)) + geom_textcontour()
To use it within the gratia::draw framework, you can remove the existing contour from the plot first:
p <- draw(mod)
p$layers[[2]] <- NULL
p + geom_textcontour(aes(z = est), fontface = 'bold')
EDIT
To get a similar effect to the stroke parameter we can do:
library(ggfx)
p + with_outer_glow(geom_textcontour(aes(z = est), fontface = 'bold',
linetype = NA),
colour = 'white', expand = 3, sigma = 1) +
geom_textcontour(aes(z = est), fontface = 'bold', textcolour = NA)

contourplot color and labels options in Lattice for R

I am quite new to Lattice and I am stuck with some possibly basic coding. I am using shapefiles and geoTIFFS to produce maps of animals distribution and in particular I have:
1 x point shapefile
2 x geoTIFF
1 x polygon shapefile
I am overlapping a levelplot of one of the geoTIFF (UD generated with adehabitatHR) with a contourplot of the same geoTIFF at specific intervals (percentile values), a contourplot of the second geoTIFF (depth raster from ETOPO2) for three specific values (-200, -1000 and -2000), the point shapefile (animal locations) and the polygon shapefile (land). All works fine but I need to change the font size of contour plot labels, their length (i.e. from 0.12315 to 0.123) and positioning for all the contourplots. For the depth contourplot I would like to change the style of each line in something like "continous line", "dashed line" and "point line", and for the contourplot of the UD I would like to change the color of each line using a yellow to red palette.
As far as I understand, I should use panel functions to implement these changes (e.g. Controlling z labels in contourplot) but i am not quite sure how to do it. Part of my code to generate the "plot":
aa <-
quantile(
UD_raster,
probs = c(0.25, 0.75),
type = 8,
names = TRUE
)
my.at <- c(aa[1], aa[2])
depth<-c(-100, -200, -2000)
levelplot(
UD_raster,
xlab = "",
ylab = "",
margin = FALSE,
contour = FALSE,
col.regions = viridis(100),
main = "A",
maxpixels = 2e5
) + layer(sp.polygons(Land, fill = "grey40", col = NA)) + layer(sp.points(locations, pts = 2, col = "red")) + contourplot(
UD_raster,
at = my.at,
labels = TRUE,
margin = FALSE
) + contourplot(
ETOPO2,
at = depth,
labels = TRUE,
margin = FALSE
)
A simplified image, with no UD layer and no point shapefile can be found here and as you can see it is pretty messy. Thanks for your help.
So far for the ETOPO2 countourplot I have solved by eliminating the labels and adding the argument lty to style the line. Because I can't figure out how to use lty with different values for each single line in my contour, I have replicated the contourplot function three times on the same surface, one for each contour I am interested into (this was easy because I only need three contours).
For the position, font and font size of the labels of the remaining contourplot I have used
labels = list(cex = 0.8, "verdana"),
label.style = "flat"
To "shorten" the length of the labels I have used the function round where I specify to which decimal digit to round number.
So now my new code looks like:
aa <-
quantile(
UD_raster,
probs = c(0.25, 0.75),
type = 8,
names = TRUE
)
my.at <- c(aa[1], aa[2])
my.at <- round(my.at, 3)
levelplot(
UD_raster,
xlab = "",
ylab = "",
margin = FALSE,
contour = FALSE,
col.regions = viridis(100),
main = "A",
maxpixels = 2e5
) + layer(sp.polygons(Land, fill = "grey40", col = NA)) + layer(sp.points(positions, pts = 2, col = "red")) + contourplot(
UD_raster,
at = my.at,
labels = list(cex = 0.8, "verdana"),
label.style = "flat",
margin = FALSE
) + contourplot(
ETOPO2,
at = -200,
labels = FALSE,
margin = FALSE,
lty = 1,
pretty = TRUE
) + contourplot(
ETOPO2,
at = -1000,
labels = FALSE,
margin = FALSE,
lty = 2,
pretty = TRUE
) + contourplot(
ETOPO2,
at = -2000,
labels = FALSE,
margin = FALSE,
lty = 3,
pretty = TRUE
)
As one could expect, it takes a bit longer to produce the plot. Still no idea on how to change the colors of the UD contourplot.

R ::tmap plot and display NA values in legend

I want to map point data with results of bird counts. The point size should be scaled according to the number of birds that were counted. If an area has not been counted an x should be displayed for NA values. How do I plot these data and reach a nice legend with the tmap package?
This is a similar example:
rm(list=ls(all=TRUE))
library(tmap)
data(World, metro)
# put population size pop2020 to NA for some cities
metro$pop2020[10:300] <- NA
# add column with code for the shape of the symbol (21 for data available, 4 for NA)
metro$shape_symbol <- 21
metro[is.na(metro$pop2020), ]$shape_symbol <- 4
tm_shape(World) + tm_fill()+
tm_shape(metro) +
tm_symbols(
size = "pop2020",
col = "black",
shape = "shape_symbol", # use column shape_symbol in metro for the symbol
# shapeNA = "4", # should plot NA as cross by default - didnĀ“t work for me
title.size = "subtitle",
legend.size.is.portrait=TRUE) +
tm_layout(legend.bg.color = "gray",
legend.frame = "black")
Which gives this output. Why are the NA values not shown? And how do I reach a nice legend output?
My aim is to reach this:
The name of the bird species should be given as bold legend title with an additional label "Anzahl" for the legend symbols below. Ideally the symbol x for NA should be place like this. I can paste(i) from a loop as legend title with title.size but how do I get a second title in the legend.
Additional question: can I set the size of the points to a certain range? So that very small numbers have a minimum size in the map?
I solved it myself using a workaround:
I added an extra layer and selected only the data with missing values. Then I added an additional legend element using tm_add_legend.
data(World, metro)
# put population size pop2020 to NA for some cities
metro$pop2020[10:300] <- NA
tm_shape(World) + tm_fill()+
tm_shape(metro) +
tm_symbols(
size = "pop2020",
col = "black",
title.size = "subtitle",
legend.size.is.portrait=TRUE) +
tm_shape(metro[is.na(metro$pop2020),]) +
tm_dots(shape=4, size = 0.5, border.lwd = 0.5) +
tm_layout(legend.bg.color = "gray",
legend.frame = "black") +
tm_add_legend(type="symbol", shape =4, labels = "not available", size = 0.5, border.lwd = 0.5, col = "black")

R - Adding dots to map in tmap

My map-making code generates a map based on census data and plots important points as a tm_dots() layer. What I'd like to be able to do is differentiate between the types of dots (e.g. if the location is "Informal" or "Commercial").
tm_shape(bristol) + tm_fill("population", palette = "YlOrRd",
auto.palette.mapping = TRUE,
title = "Bristol Population",
breaks = c(0,5,10,15,20,25), colorNA = "darkgrey") + tm_borders("grey25",alpha = 0.7, lwd = 0.1) +
tm_dots("n", size=0.1,col="green", shapeNA = NA, title = "Spaces") +
tm_legend(text.size=1,title.size=1.2,position=c("left","top")) +
tm_layout(legend.outside = TRUE, legend.outside.position = "bottom", title.snap.to.legend = TRUE)
What I'm looking for is essentially:
tm_dots("n", size=0.1,col=Classification, shapeNA = NA, title = "Spaces")
Adding several tm_dots() layers isn't an option. I also can't rename the dot legend, any advice on that too is appreciated.
Thanks for your help!
Solution
For future reference, I added offices to bristol via left_join, thus adding the Classification variable to the SpatialPolygonsDataFrame. I was having issues with it displaying NA values despite the showNA = NA parameter, but colorNA = NULL worked. Final line:
tm_dots(size=0.1,col="Classification", palette = "Set1", colorNA = NULL)
So bristol is a polygon shape (SpatialPolygonDataFrame or sf), and you want to plot dots in some polygons?
Normally, you would have a variable Offices, with two levels "Informal" and "Commercial". Then it's just tm_dots(size = 0.1, col = "Offices"). If you want to place two dots in one polygons because there are Informal and Commercial offices, then you can use your own approach (and use xmod and/or ymod for one group to prevent overlap), or create a SpatialPointsDataFrame or sf object with all offices, and a variable Offices with two levels as described above.
I figured it out, you need to have another tm_shape() for it to work. Still haven't got the title() to appear properly but one step at a time.
tm_shape(bristol) + tm_fill("population", palette = "YlOrRd", auto.palette.mapping = TRUE,
title = "Bristol Population",
breaks = c(0,5,10,15,20,25), colorNA = "darkgrey") + tm_borders("grey25",alpha = 0.7, lwd = 0.1) +
tm_dots("Informal_Offices", size=0.1,col="green", shapeNA = NA, title = "Informal Offices") +
tm_shape(bristol) + tm_dots("Commercial_Offices", size=0.1,col="white",shapeNA=NA, title="Commercial Offices") +
tm_legend(text.size=1,title.size=1.2,position=c("left","top")) +
tm_layout(legend.outside = TRUE, legend.outside.position = "bottom", title.snap.to.legend = TRUE)
Result

incorrect plotting state borders

I am intending to plot a county level map with 5 colors depending on a value of an attribute. I did that with no problems. Now, I want to add the state borders and the end result is not good. The border lines are too straight and the limits of the states are not well plotted. They do not follow the actual limits of the states.
Here is the code I am using:
# Pick the colors for the categorical values
colors = c("#CC0000", "#FF9999", "#E0E0E0", "#99FF99", "#00CC00")
#Creates a new variable with the categories.
data$colorBuckets <- as.numeric(cut(data$Slope, c(-0.30, -0.20, -0.10, 0.10,
0.20, 0.30)))
# Put a 3 (equivalent to slope 0) when there is NA's
data$colorBuckets[is.na(data$colorBuckets)] <- 3
# Matches the values of FIPS with the color categories
colorsmatched <- data$colorBuckets
library(mapproj)
# Map colored counties according the categories
map("county", col = colors[colorsmatched], fill = TRUE, resolution = 0,
lty = 0, projection = "polyconic")
# Add border around each county
map("county", col = "gray", fill = FALSE, add = TRUE, lty = 1, lwd = 0.2,
projection = "polyconic")
# Add border to each state (THIS IS THE PROBLEMATIC STEP)
map("state", col = "black", fill = FALSE, add = TRUE, lty = 1, lwd = 0.4,
projection = "polyconic")
Any idea why this may be happening?

Resources