Search code examples
rggplot2mapping

How to fill US State Map Using map_data function in ggplot2?


I'm trying to use ggplot2's map_data to build a state level map. But when I call use the arguments "states" and plot it, the map doesn't fill in the entire US.

How do I get (1) map_data to fill in the missing space and (2) show state lines?

library(ggplot2)

#**************** Get Map ******************

StateMap <- ggplot2::map_data("state")

#**************** Plot ******************
ggplot(StateMap, aes(x=long, y=lat)) +
  geom_polygon()

This is what is produces:

enter image description here

Thank you for your help!


Solution

  • We have to add group aesthetic to geom_polygon:

    library(ggplot2)
    
    #**************** Get Map ******************
    StateMap <- ggplot2::map_data("state")
    
    #**************** Plot ******************
    ggplot(StateMap, aes(x=long, y=lat, group = group)) +
      geom_polygon(fill = "gray", color = "black")
    

    enter image description here