【问题标题】:Cannot get alpha to work with ggplot2::geom_sf on sf linestring object无法在 sf 线串对象上使用 ggplot2::geom_sf 的 alpha
【发布时间】:2017-04-24 00:37:50
【问题描述】:

这需要 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 包)

【问题讨论】:

  • 你试过把alpha=0.1放在aes()里面吗?
  • geom_sf 实际上是 sf 包的一部分吗?当我安装并加载sf 时,我仍然收到以下错误could not find function "geom_sf"
  • geom_sf 是 ggplot2 包的一部分。您可能需要安装开发版:devtools::install_github("tidyverse/ggplot2")
  • J.缺点:aes(alpha = 0.1) 不起作用

标签: r ggplot2 sf


【解决方案1】:

我在 github.com/edzer/sf 提交了一个问题。

https://github.com/edzer/sfr/issues/311#issuecomment-296752334

geom_sf 尚不支持此功能。希望在不久的将来...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 2018-02-21
    • 2019-09-12
    • 1970-01-01
    • 2018-09-24
    • 1970-01-01
    • 2020-07-07
    相关资源
    最近更新 更多