Gregory
Gregory

Reputation: 4279

How to specify shape for color legend?

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

Upvotes: 0

Views: 224

Answers (1)

Majid
Majid

Reputation: 1854

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

Upvotes: 1

Related Questions