在tmap中指定大小图例的填充颜色

问题描述 投票:1回答:1

这是一个tmap的简单示例,其中气泡大小被映射到sf对象的一个​​属性,而颜色被映射到另一个对象。

library(sf)
library(tmap)
# Make 2 attributes for each of 4 points.
sf.att <- data.frame(value = c(10, 20, 30, 40), category = c("foo", "foo", "bar", "bar"))
# Make a geometry list for 4 points.
p.l <- list(st_point(c(1, 5)), st_point(c(1, 1)), st_point(c(5, 1)), st_point(c(5, 5)))
# Combine into a spatial feature object.
p.sf <- st_sf(sf.att, p.l, crs = 4326)
# Make a bounding box with a small amount of extension.
b.box <- bb(p.sf, ext = 3)
# Make a tmap in plot mode
tmap_mode("plot")
tm_shape(p.sf, bbox = b.box) +
  tm_bubbles(size = "value", scale = 5, col = "category")

结果是:

enter image description here

...这很可爱,但是值图例的默认填充颜色是为类别图例选择的第一种颜色(此处为绿色)。是否可以手动指定值图例填充颜色,以使其在视觉上变得完全独立于类别-例如,仅用灰色填充值图例圆圈?

r sp sf tmap
1个回答
1
投票

考虑用您选择的灰色填充shapes.legend.fill参数;请注意,要使其正常工作,您还必须指定shapes.legend-如本例所示:

library(sf)
library(tmap)
# Make 2 attributes for each of 4 points.
sf.att <- data.frame(value = c(10, 20, 30, 40), category = c("foo", "foo", "bar", "bar"))
# Make a geometry list for 4 points.
p.l <- list(st_point(c(1, 5)), st_point(c(1, 1)), st_point(c(5, 1)), st_point(c(5, 5)))
# Combine into a spatial feature object.
p.sf <- st_sf(sf.att, p.l, crs = 4326)
# Make a bounding box with a small amount of extension.
b.box <- tmaptools::bb(p.sf, ext = 3)
# Make a tmap in plot mode
tmap_mode("plot")
tm_shape(p.sf, bbox = b.box) +
  tm_bubbles(size = "value", scale = 5, col = "category",
             palette = c("firebrick","cornflowerblue"),
             shapes.legend = 21,
             shapes.legend.fill = "grey80")

shapes instead of bubbles

© www.soinside.com 2019 - 2024. All rights reserved.