Jake
Jake

Reputation: 117

How to add a legend when mapping with stat_sf_coordinates?

I'm using ggplot and sf to produce a map that layers points over a choropleth. The points are rendered with stat_sf_coordinates and the choropleth is rendered with geom_sf.

The resulting plot prints a nice legend for the choropleth but nothing for the points.

map with choropleth and points, missing the desired legend I'm trying to add a legend using stat_sf_coordinates that specifies what the points are, but I can't figure out how. There's a show.legend argument, but it seems to be overridden by the choropleth specifications.

Can someone please advise on how to add the second legend for the points?

ggplot(final.OD.2024) +
  geom_sf(data = OD.deaths.map, aes(fill = all.drug.death.rate, geometry = geometry), colour = "white") + 
  stat_sf_coordinates(aes(geometry = geometry), color = "red", size = 0.9, position = position_jitter(), show.legend = "geometry") + 
  theme(panel.background = element_rect(fill = "white"),
        plot.title = element_text(size = 20, hjust = 0.5),
        axis.ticks = element_blank(),
        axis.text = element_blank()) + 
  scale_fill_gradient2(low = "white", high='darkblue') + 
  xlab(element_blank()) +
  ylab(element_blank()) +
  guides(fill = guide_colorbar(title = "All Drug Overdose Death Rate \n (Deaths per 100,000 Pop.)")) + 
  theme(legend.justification = "center")

Upvotes: 0

Views: 32

Answers (1)

Jake
Jake

Reputation: 117

Answer from @Axeman solved this for me:

stat_sf_coordinates(aes(geometry = geometry, color = 'my label'), size = 0.9, position = position_jitter(), show.legend = "geometry") and + scale_color_manual(values = 'red'). 

Upvotes: 0

Related Questions