I have generated a map using tmap
over which I'm overlaying some dots (using tm_symbols()
). The dots indicate the location of the cases and the number of cases occuring there. I'm using the size of the dot to indicate the number of cases found at that location, but I'd also like to add a colour scale to the dots.
This is my code for the dots:
tm_shape(dataplot) +
tm_symbols(
size = "Case count",
col = "Case count",
palette = "Reds",
alpha = .5
)
This is how the legend comes out:
Is there any way to combine them (similar to what would be possible on ggplot2
)?
---- EDIT -----
Here is the output of dput()
for the relevant layer:
tm_element object
List of 18
$ layer : chr "symbols"
$ trans.fun :function (shpTM, xmod = NULL, ymod = NULL, ord__, plot.order,
args, scale)
$ trans.aes : list()
$ trans.args :List of 4
..$ points_only: chr "ifany"
..$ point_per : chr "feature"
..$ on_surface : logi FALSE
..$ along_lines: logi FALSE
$ trans.isglobal: logi FALSE
$ mapping.aes :List of 8
..$ size :List of 6
.. ..- attr(*, "class")= chr [1:2] "tmapScale" "list"
..$ fill :List of 6
.. ..- attr(*, "class")= chr [1:2] "tmapScale" "list"
..$ col :List of 6
.. ..- attr(*, "class")= chr [1:2] "tmapScale" "list"
..$ shape :List of 6
.. ..- attr(*, "class")= chr [1:2] "tmapScale" "list"
..$ lwd :List of 6
.. ..- attr(*, "class")= chr [1:2] "tmapScale" "list"
..$ lty :List of 6
.. ..- attr(*, "class")= chr [1:2] "tmapScale" "list"
..$ fill_alpha:List of 6
.. ..- attr(*, "class")= chr [1:2] "tmapScale" "list"
..$ col_alpha :List of 6
.. ..- attr(*, "class")= chr [1:2] "tmapScale" "list"
$ gpar :List of 11
..$ fill : chr "__fill"
..$ col : chr "__col"
..$ shape : chr "__shape"
..$ size : chr "__size"
..$ fill_alpha: chr "__fill_alpha"
..$ col_alpha : chr "__col_alpha"
..$ lty : chr "__lty"
..$ lwd : chr "__lwd"
..$ linejoin : logi NA
..$ lineend : logi NA
..$ pattern : chr "fill"
..- attr(*, "class")= chr "tmapGpar"
$ tpar : list()
..- attr(*, "class")= chr "tmapTpar"
$ plot.order :List of 5
..$ aes : chr "size"
..$ reverse : logi TRUE
..$ na.order : chr "mix"
..$ null.order : chr "bottom"
..$ null.below.na: logi TRUE
..- attr(*, "class")= chr "tm_plot_order"
$ mapping.fun : chr "Symbols"
$ mapping.args :List of 3
..$ icon.scale: num 3
..$ just : logi NA
..$ grob.dim : Named num [1:4] 48 48 256 256
.. ..- attr(*, "names")= chr [1:4] "width" "height" "render.width" "render.height"
$ zindex : logi NA
$ group : logi NA
$ group.control : chr "check"
$ popup.vars : logi NA
$ popup.format : list()
$ hover : logi NA
$ id : chr ""
- attr(*, "class")= chr [1:4] "tm_aes_layer" "tm_layer" "tm_element" "list"
You can use tm_legend_combine
to combine legends. As you provided no example data I draw on the default example from tmap
version 4.0
(!!). Note that combining legends will only work if they share the "same" number of items and in case of missing might requires to add an item for missing manually, i.e. in the code below I had to add a legend entry for missing to the size
legend.
library(tmap)
packageVersion("tmap")
#> [1] '4.0'
tm_shape(World) +
tm_polygons() +
tm_symbols(
size = "HPI",
fill = "HPI",
size.scale = tm_scale_intervals(
n = 5,
values = tm_seq(0.25, 1, power = "sqrt"),
label.na = "Missing", value.na = sqrt(.25)
),
fill.scale = tm_scale_intervals(n = 5, values = "brewer.reds"),
fill.legend = tm_legend_combine("size")
)
#> [tip] Consider a suitable map projection, e.g. by adding `+ tm_crs("auto")`.
#> This message is displayed once per session.
EDIT And in case of a "categorical" (like) variable you can switch to tm_scale_categorical
:
library(tmap)
packageVersion("tmap")
#> [1] '4.0'
World2 <- World
World2$hpi_cut <- ggplot2::cut_number(World$HPI, n = 3, labels = FALSE)
tm_shape(World2) +
tm_polygons() +
tm_symbols(
size = "hpi_cut",
fill = "hpi_cut",
size.scale = tm_scale_categorical(
values = sqrt(c(1:3) / 2),
label.na = "Missing", value.na = sqrt(.25)
),
fill.scale = tm_scale_categorical(values = "brewer.reds"),
fill.legend = tm_legend_combine("size")
)
#> [tip] Consider a suitable map projection, e.g. by adding `+ tm_crs("auto")`.
#> This message is displayed once per session.