0

I am trying to add a legend to the attached plot. The code I am using here here below. Unfortunately I cannot share the data. Would you please suggest how to modify the code so that the legend would actually show up? Thanks Michele

enter image description here

Here is the code that I am using

ggplot(data = world) +
  geom_sf(,show.legend = TRUE) +
  labs( x = "Longitude", y = "Latitude") +
  ggtitle("Strike-Tried by Fishing Zones - FAO") +
  geom_sf(data = strike, pch = 16, color = "red",show.legend = TRUE) + 
  geom_sf(data = tried, pch =20, color = "navy", show.legend = TRUE) +
  theme(panel.background = element_rect(fill = "white")) + 
  geom_polygon(data = shp, aes(x = long, y = lat, group = group), colour = "black", fill = NA) +
  scale_color_manual(values=colors, labels=c("Strike", "Tried")) 
jrcalabrese
  • 2,184
  • 3
  • 10
  • 30
  • Please include a *minimal self-contained reproducible* example. You can generate some example data (see examples in the R help files and most questions on this site) – Robert Hijmans Mar 07 '23 at 19:50
  • 1
    If you want to have a legend you have to map on aesthetics, i.e. try with e.g. `geom_sf(data = strike, aes(color = "strike"), pch = 16)`. – stefan Mar 07 '23 at 19:54
  • 1
    This question gets asked nearly every day on Stack Overflow, Michele. Instead of specifying the color directly inside `geom_sf` as `color = "red"` and `color = "navy"` , use `mapping = aes(color = "Strike")` and `mapping = aes(color = "Tried")`. Then your last line can be `scale_color_manual(values = c("red", "navy"))` – Allan Cameron Mar 07 '23 at 19:54
  • Here's one example of how this sort of question has been answered: https://stackoverflow.com/a/3780353/6851825 – Jon Spring Mar 07 '23 at 19:55

0 Answers0