0

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
Chris
  • 1,197
  • 9
  • 28

1 Answers1

1

I was able to get most of your points desired with only a minor changes in your code.

The main changes are:

  • reframing the group to col (I guess this was your biggest stumbling block)

  • adding a vector of the three legend labels in labels

  • adding legend title in title

  • adding popup title (the big, bold item on top of the popup) in id

  • adding popup "also-rans" (the small, grey items inside) in popup.vars; note how I renamed them to included the colon (othervise you would end up with your field names)

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(col = "value", 
                               breaks = c(0,1,2,Inf),
                               labels = c("some text A", "some text B", "some text C"),
                               id = "label", # bold in popup
                               popup.vars = c("area:" = "area", "code:" = "code"), # light in popup
                               palette = c("aquamarine2", "cornflowerblue", "brown1"),
                               title = "Lo & behold: a legend!")

map

tmap-map-with-dots

The popups are not easily screen captured, but seem to work :)

Jindra Lacko
  • 7,814
  • 3
  • 22
  • 44
  • Perfect. Thanks. Also, specifying popup.vars = TRUE means all attributes are shown in the popup (not sure how I missed that before!). – Chris Jul 30 '19 at 22:04
  • Glad to be of service! I personally prefer to list the popup.vars individually, as 1) my variables tend to have cryptic names and 2) my data frames contain a lot of vars. But I have to admit that the Tennekes guy is pretty smart! :) – Jindra Lacko Jul 31 '19 at 15:17