 # mapping species distribution

I am trying to plot longitudes and latitudes on a US map, but I am having a hard time coding it.

``````devtools::install_github('UrbanInstitute/urbnmapr')

ggplot() +
geom_polygon(data = urbnmapr::states, mapping = aes(x = long, y = lat, group = group),
fill = "grey", color = "white") +
coord_map(projection = "albers", lat0 = 39, lat1 = 45)
``````

Up to this point, I have a plot of the US with no points. I need to plot my longitudes and latitudes but the rest of the code I am trying to use is not coming out right

``````saltcedar.longlat %>%
ggplot(aes(lon, lat)) +
geom_polygon(color = NA) +
coord_map(projection = "albers", lat0 = 39, lat1 = 45) +
labs(title = "Species Distribution")
``````

Hi, welcome!

We don't really have enough info to help you out. Could you ask this with a minimal REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

If you've never heard of a reprex before, you might want to start by reading this FAQ:

You will find the mapping easier if you convert your spatial objects to the format of `{sf}` package, and then use `ggplot2::geom_sf()` call.

Such as this example, plotting longitudes and latitudes of three North Carolina cities:

``````library(sf)
library(tidyverse)
library(USAboundaries)

polygons <- us_boundaries(type="state", resolution = "low") %>%
filter(!state_abbr %in% c("PR", "AK", "HI")) %>% # lower 48 only
st_transform(5070) # Albers projection

points <- data.frame(name = c("Raleigh", "Greensboro", "Wilmington"),
x = c(-78.633333, -79.819444, -77.912222),
y = c(35.766667, 36.08, 34.223333)) %>%
st_as_sf(coords = c("x","y"), crs=4326)

ggplot() +
geom_sf(data = polygons, fill = NA) +
geom_sf(data = points, color = "red")
``````

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.