Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does geom_map "map_id" function work?

Tags:

r

ggplot2

I am trying to make sense of the use of geom_map in ggplot2.

Setup:

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

Why does 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 changing the map_id=region to map_id=subregion do this?

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

enter image description here

like image 306
moman822 Avatar asked Jun 19 '16 22:06

moman822


1 Answers

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

Alex is correct that map has to look like a fortified spatial object. That does the "remembering". map_id can be any column that hold the identifier for other layers.

Your first call to geom_map() should (usually) be the "base layer", similar to what you'd do with a full-on GIS program, that has the polygon outlines and perhaps a base fill.

Other calls to geom_map() can 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 mappings is that the county names aren't 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 here how the map_id is not region or id but it still works. That' b/c the values in that 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

Notice here that we can use a different spatial objects and wrap an outline 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 final one: a composite using three spatial objects layered on top of each other. Note that you prbly want to use something like this if you really want to map counties since it has FIPS codes (i.e. a unique id for each county you can map aesthetics to).

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

like image 159
hrbrmstr Avatar answered Oct 22 '22 21:10

hrbrmstr