1

I have created a map using get_map and ggmap) that includes points reflecting bird diversity and richness. I want to crop the resulting map to only include certain bounds of latitude and longitude, but when I try to use the xlim() and ylim() functions, the map itself vanishes.

This code is used to create my map with data points.

Detroit_MI_Map_MCK <- get_map(location = c(lon = -83.10484, lat = 42.34633), zoom = 16, source = "google", maptype = "satellite")

MCK_SITE_MAP <- ggmap(Detroit_MI_Map_MCK, x = Longitude, y = Latitude, label = AUDIOMOTH_BirdStats_Local$AudioMoth)

MCK_Map <- MCK_SITE_MAP +
  geom_point(data = AUDIOMOTH_BirdStats_Local, alpha = 1.0, aes(x = AUDIOMOTH_BirdStats_Local$longitude, y = AUDIOMOTH_BirdStats_Local$latitude, color = Richness, size = Shannon)) +
  geom_label_repel(data = AUDIOMOTH_BirdStats_Local,aes(x = AUDIOMOTH_BirdStats_Local$longitude, y = AUDIOMOTH_BirdStats_Local$latitude, label = AUDIOMOTH_BirdStats_Local$AudioMoth), position = position_dodge(width = 1), size = 4) +
  scale_color_gradient(low = "red", high = "springgreen3") +
  scale_size(range = c(4,10))

Map resutling from code above

However, when I try to add xlim and ylim to focus on the MCK points, the map disappears:

lat_MCK <- c(42.342, 42.350)
lon_MCK <- c(-83.100, -83.108)

MCK_Map + 
  xlim(lon_MCK) +
  ylim(lat_MCK)

Result after attmpeting xlim and ylim

Any assistance on how to fix this problem would be greatly appreciated!

  • 2
    Setting limits on the scale (like `xlim` and `ylim` removes any data outside the limits. This can mess up things like polygons when part of the polygon is outside the limits. To zoom instead, and keep the all the data, use the limits of the coordinate system, like `coord_map(xlim = lon_MCK, ylim = lat_MCK)`. (I can't reproduce your code without registering an API key, so that's a no-go for me.) – Axeman May 09 '23 at 19:31
  • 1
    Afternoon Axeman. Thank you so much! Your solution worked perfectly :D – Christopher Dennison May 09 '23 at 20:26

0 Answers0