Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

map_id variable is ignored by geom_map #504

Closed
PaulHiemstra opened this Issue Apr 19, 2012 · 2 comments

Comments

Projects
None yet
2 participants

Consider the following example using geom_map:

crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
states_map <- map_data("state")
ggplot(crimes, aes(map_id = state)) +
   geom_map(aes(fill = Murder), map = states_map) +
   expand_limits(x = states_map$long, y = states_map$lat) +
   guides(fill = guide_colorbar(colours = topo.colors(10))) +
   opts(legend.position = "top")

One would expect that by using map_id = state, that a link was made between a column in states_map (the polygons) and a column in crimes (Murder). crimes contains a state column:

> head(crimes)
               state Murder Assault UrbanPop Rape
Alabama       alabama   13.2     236       58 21.2
Alaska         alaska   10.0     263       48 44.5
Arizona       arizona    8.1     294       80 31.0
Arkansas     arkansas    8.8     190       50 19.5
California california    9.0     276       91 40.6
Colorado     colorado    7.9     204       78 38.7

but states_map does not:

> head(states_map)
       long      lat group order  region subregion
1 -87.46201 30.38968     1     1 alabama      <NA>
2 -87.48493 30.37249     1     2 alabama      <NA>
3 -87.52503 30.37249     1     3 alabama      <NA>
4 -87.53076 30.33239     1     4 alabama      <NA>
5 -87.57087 30.32665     1     5 alabama      <NA>
6 -87.58806 30.32665     1     6 alabama      <NA>

When using geom_map, the variable that links the polygons to the value in the dataset is ignored. In stead, the region variable in states_maps is always used. In this example above, this is expected behavior. But in this StackOverflow question this present a problem:

http://stackoverflow.com/questions/10219919/geom-map-all-subregions-the-same-color/10224042#10224042

So, I think the map_id variable should be interpreted correctly, and not ignored.

Owner

hadley commented Apr 19, 2012

I don't think this is a bug - the map_id names the matching variable in the data data frame, not the map data frame. As documented, that must always be called id or region

@hadley hadley closed this Apr 19, 2012

I edited the SO answer I wrote to reflect the documentation. Thanks for the feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment