当使用
tm_bubbles
或 tm_symbols
创建代表我的数据的气泡或点时,使用 size="data column of my choice"
进行分类,我遇到一个问题,其中数据集中的数字非常小(假设 n=2,我的范围是 10) ,2000),代表 x1=2000 的气泡在屏幕上非常明显,但 x2=10 非常小,我几乎在屏幕上看不到它。我需要的是一个为气泡设置 size.minimum
的函数。有人找到解决这个问题的方法吗?或者我们可以为符号创建类似 size.minimum
函数的东西吗?
这就是目前的样子:
library(tidyverse)
library(tmap)
library(sf)
data("World")
df_dummy <- tibble(size = c(10, 200, 2000),
points = c(st_sfc(st_point(c(-30,20))),
st_sfc(st_point(c(7,52))),
st_sfc(st_point(c(30,5))))) %>%
st_as_sf()
tm_shape(World) +
tm_fill(col = "gray") +
tm_shape(df_dummy) +
tm_bubbles(col = "red",
size = "size")
如果您现在添加
df_dummy`<- df_dummy %>% mutate(size = log10(size))
,由于我们使用 log10 函数,您的数据会缩放不同。当然,您会丢失图例中有关“原始”值的信息,但所有符号都是可见的:
或者,您可以简单地生成一个新列,其中所有低于定义限制的值都会接收到特定的大小值(例如,所有低于 100 的值都会接收到 100 的大小)。通过这种方式,低于此限制的所有值都将被剪辑为一个值,然后可以将其用作您的尺寸参数。在以下示例中,原始大小为 10、50 和 80 的点使用剪辑大小 100 统一绘制。在自定义图例中,它们被标签 <= 100.
覆盖df_dummy <- tibble(size = c(10, 50, 80, 500, 2000),
points = c(st_sfc(st_point(c(-30,20))),
st_sfc(st_point(c(7,52))),
st_sfc(st_point(c(10,45))),
st_sfc(st_point(c(15,35))),
st_sfc(st_point(c(30,5))))) %>%
mutate(size = ifelse(size <= 100, 100, size)) %>%
st_as_sf()
# see the tm_bubbles docs regarding the applied scaling
bubble_sizes <- ((c(100, 500, 1000, 1500, 2000)) / 2000) ^ 0.5 * 2
tm_shape(World) +
tm_fill(col = "gray") +
tm_shape(df_dummy) +
tm_bubbles(col = "red",
size = "size",
scale = 2,
border.col = "black",
legend.size.show = F,
legend.col.show = F) +
tm_add_legend("symbol",
size = bubble_sizes,
col = "red",
labels = c("<= 100","500","1000","1500", "2000"),
title = "custom legend")