In this post, I show you how to create the outline for a Nebraska map in
ggplot2 that is separated by counties. First, R already has latitudes, longitudes and mapping necessities we need for this, it’s just a matter of accessing them, which is one of the reasons why R is so great and easy to use.
maps_data from the
maps package, we can turn all of these coordinates into a data frame. Let’s name ours ‘states’, but then also create a smaller data frame that only contains Nebraska data:
states <- map_data("state") ne_coords <- subset(states, region=="nebraska") head(ne_coords)
Now, let’s do the same thing, but with counties…
counties <- map_data("county") ne_county <- subset(counties, region=="nebraska") head(ne_county)
Okay! We have all of the data we need to draw the map. Let’s activate ggplot2 and see what this looks like when we visualize it.
Nebraska map without county lines:
ne_map <- ggplot(data=ne_coords, mapping = aes(x=long, y=lat, group=group)) + coord_fixed(1.3) + geom_polygon(color="black", fill="gray")
ne_map + theme_clasic() + geom_polygon(data = ne_county, fill=NA, color="white")