How does the geom_map "map_id" function work?

I am trying to understand the use of geom_map in ggplot2.

Setup:

 library(ggplot2) library(maps) county2 <- map_data("county") 

Why is this code:

 ggplot() + geom_map(data=county2, map=county2, aes(x=long, y=lat, map_id=region), col="white", fill="grey") 

Produce this correct plot: enter image description here

But do you change the value of map_id=region to map_id=subregion ?

 ggplot() + geom_map(data=county2, map=county2, aes(x=long, y=lat, map_id=subregion), col="white", fill="grey") 

enter image description here

+5
source share
1 answer

geom_map() does the job of remembering the polygons in the data frame for you.

Alex is right that map should look like a fortified feature. It does "memorization." map_id can be any column that contains an identifier for other layers.

Your first call to geom_map() should (usually) be a β€œbase layer”, similar to what you are doing with a full-featured GIS program that has polygon outlines and possibly a base fill.

Other calls to geom_map() may add other aesthetics (including other shapefiles).

Here are some examples to demonstrate.

 library(ggplot2) library(maptools) library(mapdata) library(ggthemes) library(tibble) library(viridis) us <- map_data("state") choro_dat <- data_frame(some_other_name=unique(us$region), some_critical_value=sample(10000, length(some_other_name))) gg <- ggplot() gg <- gg + geom_map(data=us, map=us, aes(long, lat, map_id=region), color="#2b2b2b", fill=NA, size=0.15) gg <- gg + coord_map("polyconic") gg <- gg + theme_map() gg <- gg + theme(plot.margin=margin(20,20,20,20)) gg 

enter image description here

 county <- map_data("county") gg <- ggplot() gg <- gg + geom_map(data=county, map=county, aes(long, lat, map_id=region), color="#2b2b2b", fill=NA, size=0.15) gg <- gg + coord_map("polyconic") gg <- gg + theme_map() gg <- gg + theme(plot.margin=margin(20,20,20,20)) gg 

enter image description here

The reason for the strange county maps is that the county names are not unique.

 gg <- ggplot() gg <- gg + geom_map(data=county, map=county, aes(long, lat, map_id=subregion), color="#2b2b2b", fill=NA, size=0.15) gg <- gg + coord_map("polyconic") gg <- gg + theme_map() gg <- gg + theme(plot.margin=margin(20,20,20,20)) gg 

enter image description here

Note that map_id not a region or id , but it still works. What the 'b / c values ​​in this column are in us$region .

 gg <- ggplot() gg <- gg + geom_map(data=us, map=us, aes(long, lat, map_id=region), color="#2b2b2b", fill=NA, size=0.15) gg <- gg + geom_map(data=choro_dat, map=us, aes(fill=some_critical_value, map_id=some_other_name), color="white", size=0.15) gg <- gg + scale_fill_viridis(name="Value") gg <- gg + coord_map("polyconic") gg <- gg + theme_map() gg <- gg + theme(plot.margin=margin(20,20,20,20)) gg <- gg + theme(legend.position=c(0.85, 0.2)) gg 

enter image description here

Please note that we can use different features and wrap a path around our map:

 outline <- map_data("usa") gg <- gg + geom_map(data=outline, map=outline, aes(long, lat, map_id=region), color="black", fill=NA, size=1) gg 

enter image description here

One last one: a composite using three features located one above the other. Note that you want to use something like this if you really want to display the counties, as it has FIPS codes (that is, a unique identifier for each county can display aesthetics).

 state <- map_data("state") county <- map_data("county") usa <- map_data("usa") gg <- ggplot() gg <- gg + geom_map(data=county, map=county, aes(long, lat, map_id=region), color="#2b2b2b", fill=NA, size=0.15) gg <- gg + geom_map(data=state, map=state, aes(long, lat, map_id=region), color="#2166ac", fill=NA, size=0.5) gg <- gg + geom_map(data=usa, map=usa, aes(long, lat, map_id=region), color="#4d9221", fill=NA, size=1) gg <- gg + coord_map("polyconic") gg <- gg + theme_map() gg <- gg + theme(plot.margin=margin(20,20,20,20)) gg 

enter image description here

+8
source

All Articles