I am trying to plot out a base map of Illinois by counties. Libraries I had loaded:
library(ggplot2)
library(maps)
library(ggmap)
library(mapdata)
This is my code:
states <- map_data("state")
IL <- subset(states, region %in% c("illinois"))
counties <- map_data("county")
IL_county <- subset(counties, region == "illinois")
il_base <- ggplot(data = IL, mapping = aes(x = long, y = lat)) +
coord_fixed(1.3) +
geom_polygon(color = "black", fill = NA) +
theme_nothing()
il_base
il_base +
geom_polygon(data = IL_county, fill = NA, color = "black") +
geom_polygon(color = "black", fill = NA)
The il_base
plot is fine, it shows a basic outline of the state. However, as soon as I add geom_polygon
to this it maps the counties out like this:
And this is NOT what the counties of IL look like. What did I do wrong here?
I solved the problem by modifying the base plot to:
# Add group
il_base <- ggplot(data = IL, mapping = aes(x = long, y = lat, group = group)) +
coord_fixed(1.3) +
geom_polygon(color = "black", fill = NA) +
theme_nothing()