这需要 R 中的库 sf 和 ggplot2。我有一个带有 4 个线串的 sf 对象。其中 3 个是相同的,一个是扩展的:
a <- st_linestring(rbind(c(2,2), c(3,3), c(3,2)))
b <- st_linestring(rbind(c(2,2), c(3,3)))
c <- st_linestring(rbind(c(2,2), c(3,3)))
d <- st_linestring(rbind(c(2,2), c(3,3)))
testsf <- st_sf(object = c(1, 2, 3, 4), geometry = st_sfc(a, b, c, d), crs = 4326)`
如果我在 ggplot2 中以 alpha = 0.1 绘制此图,我希望对角线比垂直线更暗,因为它出现的频率更高。这是 ggplot2 中的正常(非科幻)行为。
ggplot(data = testsf) + geom_sf(data = testsf, alpha = 0.1, lwd = 2, color = "black")
但是,所有行似乎都等于 alpha。为什么会出现这种情况?
更新 : 如果我尝试
testsf %<>% dplyr::mutate(geochar = as.character(geometry)) %>% dplyr::group_by(geochar) %>% dplyr::tally() %>% sf::st_cast()
ggplot(data = testsf) + geom_sf(data = testsf, aes(alpha = n), lwd = 2, color = "black")
图例显示 alpha 变化就好像它是一个多边形......也许 geom_sf 没有正确处理线条的 alpha (注意,上面的代码需要 dplyr 和 magrittr 包)
最佳答案
我在 github.com/edzer/sf 提交了一个问题。
https://github.com/edzer/sfr/issues/311#issuecomment-296752334
geom_sf 尚不支持此功能。希望在不久的将来...
关于r - 无法让 alpha 与 ggplot2::geom_sf 在 sf linestring 对象上一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43578082/