【发布时间】: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)不起作用