r - 如何根据列设置 alpha?

标签 r sp sf tmap

考虑这个例子:

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)

enter image description here

您可以看到 (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)

不工作

enter image description here

最佳答案

不幸的是,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)

enter image description here

关于r - 如何根据列设置 alpha?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50864644/

相关文章:

c - 剖析 C 代码 R

r - 如何使用 ggmap 绘制带孔的 shp?

r - 计算最近要素的方位角

c++ - 如何使用 RcppEigen

R 创建带有 R CMD 检查的引用手册

r - 使用ggplot facet_grid在不同条件下相同变量的散点图?

r - 计算分组数据中各点之间的欧式距离

r - 映射澳大利亚城市 - R 空间

r - 为什么 geom_sf 不允许填充数据帧中的离散列?

r - 散点饼图 : circles not properly positioned over map