jerH
jerH

Reputation: 1299

ggplot2 legend for geom_point on a filled map

I've been working with plotting covid-19 on a US county map, and thanks to help on this forum have gotten a product I'm pretty happy with. However, I'd like to make a change to the way the legends are produced and am unsure how to. There are several dataframes that go into the below snippet

p <- counties_cov %>%
  ggplot() +
  geom_sf(mapping = aes(fill = cases), color = NA) +
  geom_sf(data = states_sf, fill = NA, color = "black", size = 0.25) +
  coord_sf(datum = NA) +   
  scale_fill_gradient(name = "Cases", trans = "log", low='green', high='red',
                      na.value = "white",
                      breaks=c(1, max(counties_cov$cases))) +
  geom_point(data=myBizLocations, aes(x=longitude.1, y=latitude.1,size=personnel), color = "hotpink") +
  theme_bw() + 
  theme(legend.position="bottom", 
        panel.border = element_blank(),
        axis.title.x=element_blank(), 
        axis.title.y=element_blank())

For yesterday's data this produces

enter image description here

What I'd like to do is simply modify the legend text from the current "personnel" which is just the name of the data column used to size the geom_points. I'm unsure of how to do that without impacting the "cases" scale...

Ideally I'd like to go one step further and leave the cases scale on the bottom but put the personnel scale on the right...that's secondary though.

Upvotes: 1

Views: 812

Answers (1)

chemdork123
chemdork123

Reputation: 13833

You should be able to change the name of the legend for any aes() element as an argument within labs(). So something like:

p <- your plot code
p + labs(size='Fun New Text!')

Alternatively, you can use theme(legend.title = ...), or a call within scale_size_continuous(), but the labs() way is probably the simplest.

Upvotes: 2

Related Questions