Cannot get alpha to work with ggplot2 :: geom_sf on sf linestring object

This requires the sf and ggplot2 libraries in R. I have an sf object with 4 linestrings. 3 of them are identical and one is extended:

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)`

If I build this in ggplot2 with alpha = 0.1, I would expect the diagonal line to be darker than the vertical one, as this happens more often. This is normal (non-sf) behavior in ggplot2.

 ggplot(data = testsf) + geom_sf(data = testsf, alpha = 0.1, lwd = 2, color = "black")

However, all lines seem to be equal to alpha. Why is this happening?

Refresh . If i try

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")

the legend shows the alpha change as if it were a polygon ... maybe geom_sf doesn’t handle alpha for strings correctly (note that the dplyr and magrittr packages are required for the code above)

+4
1
+1

Source: https://habr.com/ru/post/1675475/


All Articles