Reputation: 381
This is a reproductible example of an issue I am facing. I am trying to create maps with ggplot2 in multiple stages. Here is the issue I face.
Consider the data border
with the polygons of states in US at the Mexican border, and border.county
with the polygons of the counties in these states. The following code allows you to get the data:
library(maps)
library(ggmap)
library(ggplot2)
USA <- get_googlemap(center = 'usa', zoom = 4,
style = 'administrative|element:labels|visibility:off')
us.df <- map_data("state")
border <- subset(us.df,
region %in% c("california","arizona","new mexico","texas"))
counties <- map_data("county")
border.county <- subset(counties,
region %in% c("california","arizona","new mexico","texas"))
Now I wan to create a map, with the background of a map from Google Maps, with the state polygons and the county borders. If I do the following, it works neatly:
Allmap <- ggmap(USA) +
geom_polygon(aes(x = long, y = lat, fill = region, group = group),
data=border, color = "white") +
geom_polygon(aes(x = long, y = lat, group = group),
data=border.county, fill=NA, color="red")
Now if I wanted to create this map in multiple stages, I hit problems. I just want the county boundaries for background information (as sort of 'recurrent theme'), and I will create multiple maps with changing information at the state level. So I create the 'background map' with counties, which works fine:
Countmap <- ggmap(USA) +
geom_polygon(aes(x = long, y = lat, group = group),
data=border.county, fill=NA, color="red")
And now I try to combine it with the state maps:
Statmap <- ggmap(USA) +
geom_polygon(aes(x = long, y = lat, fill = region, group = group),
data=border, color = "white") +
Countmap
That gives me the error:
Error: Don't know how to add o to a plot
How can I solve this? I can combine the maps in the other way (as in: Statmap <- Countmap + geom_polygon(aes(x = long, y = lat, fill = region, group = group), data=border, color = "white")
); however, that puts the counties under the state boundaries.
I also know this specific problem has the easy solution of just drawing a map with the states first, and combine it with the counties in a second stage. However, in my real scenario, that is not an option because the recurrent theme of the map is something that needs to be drawn in second place : cities and geographic borders (like my county boundaries here).
This is the map I want to create:
Upvotes: 2
Views: 6714
Reputation: 29125
If I understand your description correctly, you don't want to combine maps. You want to combine layers, specifically, to overlay the county outlines on changing state-level maps.
Try this:
# define county outlines as a geom_polygon layer
county.layer <- geom_polygon(aes(x = long, y = lat, group = group),
data = border.county, fill = NA, color = "red")
# add county.layer as the last layer to your state-level map
Statmap <- ggmap(USA) +
geom_polygon(aes(x = long, y = lat, fill = region, group = group),
data=border, color = "white") +
county.layer
Statmap
Edit in response to comment
If you have multiple county layers to plot, place them in a list:
border.county2 <- subset(counties, region %in% c("montana"))
layer2 <- list(geom_polygon(aes(x = long, y = lat, group = group),
data = border.county2, fill = NA, color = "blue"),
geom_polygon(aes(x = long, y = lat, group = group),
data = border.county, fill = NA, color = "red"))
Statmap <- ggmap(USA) +
geom_polygon(aes(x = long, y = lat, fill = region, group = group),
data=border, color = "white") +
layer2
Upvotes: 5