Reputation: 1023
I am creating choropleth maps of county level data with ggplot2. I want to highlight multiple-county regions. It is simple enough to overlay a path or polygon of a subset of the county map data, but this leaves "interior" path lines. Is there a way to combine these regions into one group or simply hide the interior lines?
require(ggplot2)
require(maps)
require(maptools)
va <- map_data("county", region="virginia")
ggplot(va, aes(long, lat)) + coord_map() +
geom_polygon(aes(group=group)) +
geom_path(data=subset(va, subregion %in% c("patrick", "henry", "franklin",
"pittsylvania")), aes(group=group), size=1, color="white")
https://i.sstatic.net/BIiAG.png
EDIT: The above example isn't exactly what I want to do. The code below is more representative of the actual project. The actual maps will be even more detailed than this since they use census tract level data and have geocoded points on top.
va <- map_data("county", region="virginia")
cty <- data.frame(subregion=unique(va$subregion))
cty$stat <- sample.int(nrow(cty))
va <- merge(va, cty, by="subregion")
p <- ggplot(va, aes(long, lat)) + coord_map() +
geom_polygon(aes(group=group, fill=stat), color="grey") +
geom_path(data=subset(va, subregion %in% c("patrick", "henry", "franklin",
"pittsylvania")), aes(group=group), size=1, color="white")
Upvotes: 2
Views: 4145
Reputation: 77096
looking at ?map
, you can simply do the following,
boundary <- map("county", regions=c("virginia.patrick", "virginia.henry", "virginia.franklin", "virginia.pittsylvania"), boundary=TRUE, interior=FALSE)
now, for some reason neither map_data
nor ggplot2:::fortify.map
seem to be happy with this, but it wouldn't be hard to extract the relevant part of fortify.map
and get it in a format that pleases coord_map. If you do, I'd suggest that you file a bug report as it seems that ggplot2 should support those options of map
.
Upvotes: 2
Reputation: 77096
You can identify and remove the points at the border between subregions,
d <- subset(va, subregion %in% c("patrick", "henry", "franklin", "pittsylvania"))
duplicates <- duplicated(d[, c("long","lat")]) |
duplicated(d[, c("long","lat")], fromLast=TRUE)
d2 <- d[duplicates, ]
d3 <- d[!duplicates, ]
d3 <- arrange(d3, order , region, group )
ggplot(d, aes(long, lat)) + geom_polygon(aes(fill=subregion), col=NA) +
geom_point(colour="black") +
geom_point(data=d2, colour="red") +
geom_path(data=d3, fill=NA, colour="blue", size=1.2)
not sure how to fix the winding order and this sort of thing...
Upvotes: 0
Reputation: 6410
If I get you right, you just need to swap geom_path
for geom_polygon
to plot without internal lines:
ggplot(va, aes(long, lat)) + coord_map() + geom_polygon(aes(group=group)) +
geom_polygon(data=subset(va, subregion %in% c("patrick", "henry", "franklin",
"pittsylvania")), aes(group=group), size=1, color="white",fill="white")
Upvotes: 5