0

I am plotting spatial point data (sf) with ggplot2. Color is mapped to an associated value. The legend for color is unexpectedly using a polygon shape as it would for a two-dimensional geom that might have separate color and fill attributes. Is there a way to specify that the shape for the color legend so that I can make it a filled circle?

library(sf)
library(ggplot2)

set.seed(2)
df <- data.frame(x = runif(10, min = 0, max = 10),
                 y = runif(10, min = 0, max = 10),
                 value = sample(c(FALSE, TRUE), 10, replace = TRUE))
sf <- st_as_sf(df, coords = c("x", "y"))

ggplot(sf, aes(color=value)) +
  geom_sf()

enter image description here

Gregory
  • 4,147
  • 7
  • 33
  • 44
  • Possible duplicate of [Add line legend to geom\_sf](https://stackoverflow.com/questions/47996634/add-line-legend-to-geom-sf) – camille Oct 14 '19 at 04:58

1 Answers1

1

The proper way to do this is as below:

ggplot() +
  geom_sf(data = sf, aes(color=value), show.legend = "point") 
# you can also choose "line" for instance

enter image description here

Majid
  • 1,836
  • 9
  • 19
  • Now why did I not see that in the documentation for `geom_sf`. I think I just expected `show.legend` to take a logical argument. – Gregory Oct 13 '19 at 23:10
  • Usually it does only take a logical argument. For `geom_sf`, this is an issue still https://github.com/r-spatial/sf/issues/881 – camille Oct 14 '19 at 04:56