考虑这个例子:
library(dplyr)
library(sf)
library(tmap)
d <- data_frame(one = c(1,1,2,1,1,1,1),
two = c(1,1,2,1,1,1,1))
std <- st_as_sf(d, coords = c('one', 'two'))
std %>% tm_shape() + tm_bubbles(alpha = 0.3)
您可以看到 (1, 1)
点颜色较深,因为它在数据中出现了 6 次。因此,感谢 alpha
混合,这些点加起来。
我的问题是我无法按原样存储数据集。我所拥有的只是一个聚合版本,比如
d_agg <- d %>% group_by(one, two) %>%
summarize(count = n()) %>%
ungroup()
# A tibble: 2 x 3
one two count
<dbl> <dbl> <int>
1 1 1 6
2 2 2 1
如何使用 d_agg
和相应的 count
变量重现与以前完全相同的图表?
当然,重新创建上面的初始数据框不是可行的解决方案,因为我的点太多了(有些点重复了太多次)
只是使用:
std_agg %>% tm_shape() + tm_bubbles(col = 'count', alpha = 0.3)
不工作
最佳答案
不幸的是,alpha(目前)还不是一种美学,所以不可能做到 alpha = "count"
。
我的问题:你真的需要 alpha 吗?如果您不使用颜色美学,可能不会。在那种情况下,您使用颜色来模拟 alpha 透明度的方法实际上很好,但只需要一点配置:
std_agg %>% tm_shape() + tm_bubbles(col = 'count', style = "cont",
palette = "Greys", contrast = c(.3, .7), legend.col.show = FALSE)
关于r - 如何根据列设置 alpha?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50864644/