Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

ggplot2

sf

This requires the libraries sf and ggplot2 in R. I have a sf object with 4 linestrings. 3 of these 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 plot this in ggplot2 with alpha = 0.1, I would expect that the diagonal line is darker than the vertical line as it occurs more often. This is normal (non-sf) behaviour in ggplot2.

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

However, all lines appear to be equal alpha. Why is this occurring?

Update: 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 alpha changing as if it is a polygon...perhaps geom_sf is not properly handling alpha for lines (note, above code requires dplyr and magrittr packages)

like image 263
sebdalgarno Avatar asked Oct 29 '22 08:10

sebdalgarno


1 Answers

I filed an issue at github.com/edzer/sf.

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

This is not yet supported in geom_sf. Hopefully in the near future...

like image 155
sebdalgarno Avatar answered Nov 13 '22 02:11

sebdalgarno