I have a point dataset I would like to map. I am new to tmap
and have been playing around with it, but have been unable to get the output I desire. I am after something that does the following (if possible):
- Display the points for a designated
area
only (e.g. A002) - Colour the points based on
value
and specify what those colours are (e.g. 0=green, 1= blue and 2 = red) - Display a legend inside the map with a custom title (e.g. Legend numbers)
- Allow custom text to be placed in the legend (e.g. 0 [Some text]; 1 [Some text]; 2 [Some text]
- When you hover over a point the entry in
label
is displayed - When you click on a point all the attributes are shown (e.g. values for code, area, value and label)
This is the code I have been using:
require("data.table")
require("sf")
require("tmap")
dt1 <- data.table(
code=c("A00111", "A00112","A00113","A00211","A00212","A00213","A00214","A00311","A00312"),
area=c("A001", "A001","A001","A002","A002","A002","A002","A003","A003"),
x=c(325147,323095,596020,257409,241206,248371,261076,595218,596678),
y=c(286151,284740,335814,079727,084266,078283,062045,333889,337836),
value=c(0,1,2,0,1,1,2,2,2),
label=c("A00 111", "A00 112","A00 113","A00 211","A00 212","A00 213","A00 214","A00 311","A00 312"))
sf1 <- st_as_sf(dt1, coords = c("x","y"), crs=27700, na.fail=FALSE)
tmap_mode("view")
map <- tm_shape(sf1) + tm_dots(group = "value", breaks = c(0,1,2,Inf), palette = c("aquamarine2", "cornflowerblue", "brown1"))
map