I want to depict a dot as a color-filled shape. Below = example data.
library(tidyverse)
library(sf)
library(tmap)
data <- data.frame(sample = c(1:5),
class = as.character(c(1:5)),
method = c("m1", "m2", "m2", "m1", "m2"),
long = seq(18.0,18.4,0.1),
lat = c(-34, -34, -33.5, -35, -35)) %>%
st_as_sf(coords = c("long", "lat"), crs = 4326)
tm_shape(data) +
tm_dots(col = "class", shape ="method", size = 2, title = "Class type")
My problem is that I can specify only one legend title (in the above case "class") and not an alternative title for "method" i.e. "Sampling method". Can someone explain how I can do this in tmap?
If you define your data in a tibble instead of a data.frame you can use nonsyntactic names for naming columns. Thus, it's easy to define e.g. `Sampling method` as colum name before passing the data to tmap for plotting. the legend then keeps that names as you defined them.
library(tidyverse)
library(sf)
library(tmap)
data <- tibble(sample = c(1:5),
class = as.character(c(1:5)),
`Sampling method` = c("m1", "m2", "m2", "m1", "m2"),
long = seq(1,5,1), #slightly adjusted your data
lat = seq(1,5,1)) %>%
st_as_sf(coords = c("long", "lat"), crs = 4326)
tm_shape(data) +
tm_dots(col = "class", shape ="Sampling method", size = 2, title = "Class type")