'Assigned data `geom` must be compatible with existing data

I am trying to create a simple map of the total number of collisions by county. I will pool the collision across species and then make the fill color dependent on the number of collisions per 1,000 residents. This formula is used for collisions scaled:

collisions_scaled= (n_collisions × 1000)/ population

Here is the code I have tried

df %>% 
 st_as_sf() %>%  
  group_by(county_id, species) %>% 
  summarise(
    n_collisions = n(), 
    collisions_scaled = (n_collisions * 1000)/county_population_2020,
    .groups = 'drop') %>% 
  ggplot() +
  geom_sf(data = wildlife_collisions,
          aes(fill = collisions_scaled)) +
  theme_void()

but I kept getting this error message:

Error:
! Assigned data geom must be compatible with existing data.
x Existing data has 40015 rows.
x Assigned data has 505 rows.
i Only vectors of size 1 are recycled.

Where am I missing something? What is wrong with my code? I have been on this for over 3 hours. Note that I have used this in a previous way and it worked. Here is the way I used it.

df %>% 
  st_as_sf() %>% 
  group_by(county_id, species) %>% 
  summarise(
    total = n(), .groups = 'drop') %>% 
  ggplot() +
  geom_sf(aes(fill = species)) +
  theme_void()
r


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source