'Adding an overlay of plotted points to a tm_shape map created in R
I create a map using the below code in R tm_shape which shades areas by number of sales (object Output_MSOA is created earlier in the code (code not shown) and takes a shape file of areas in England and joins sales by area to give all the necessary data for the mapping).
#Plot via tm_shape
OutputMap <- tm_shape(Output_MSOA) +
tm_fill("Incidence", style="quantile", title = "Quantiles", textNA = "0 sales", colorNA = "White", palette = "YlGnBu") +
tm_layout(legend.position = c("left", "bottom"),
legend.title.size = 0.5,
legend.text.size = 0.5,
legend.format = list(format = "f", text.separator = "<")) +
tm_borders(
col = NA,
lwd = 1,
lty = "solid",
alpha = NA,
zindex = NA,
group = NA
)
I would like to add some plotted points via tm_dots as an overlay. The lat and long values of the two points are:
| Warehouse | Lat | Long |
|---|---|---|
| One | 52.625848 | -0.28136 |
| Two | 52.5656 | -0.23951 |
I gather I need to code so that tm_shape understands how to plot the lat/long values - is anyone able to help with this please? Thank you!
Solution 1:[1]
Figured it out, this works (although perhaps could be made better?)
#Create data frame of postcode long and lats
myaddresses <- data.frame(
lat = c(52.625848, 52.5656),
long = c(-0.28136104, -0.23951322)
)
#geocode the newly created object of addresses in usable format
geocode <- st_as_sf(myaddresses, coords = c("long", "lat"), crs = 4326)
#Create extended map with two layers
OutputMap <- tm_shape(Output_MSOA) +
tm_fill("Incidence", style="quantile", title = "Quantiles", textNA = "0 sales", colorNA = "White", palette = "YlGnBu") +
tm_layout(legend.position = c("left", "bottom"),
legend.title.size = 0.5,
legend.text.size = 0.5,
legend.format = list(format = "f", text.separator = "<")) +
tm_borders(
col = NA,
lwd = 1,
lty = "solid",
alpha = NA,
zindex = NA,
group = NA
) +
tm_shape(geocode)+
tm_dots(col = "red", size = 0.1)
OutputMap
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | JeffWithpetersen |
