Reputation: 110024
I'm attempting to move over from older ways of mapping data to a choropleth now that ggplot2 has the geom_map. An example is seen on pp. 10-11 (HERE).
I'm attempting to do this with a data set I've created a choropleth from in the past just not with ggplot's new geom_map. Here's my attempt that I think is like Hadely's example but everything is the same color.
The data set and code:
#loads 2 data frames: ny and cad from my drop box
load(url("http://dl.dropbox.com/u/61803503/MAPPING.RData"))
library(ggplot2)
ggplot(cad, aes(map_id = subregion)) +
geom_map(aes(fill = Math_Pass_Rate), map = ny) +
expand_limits(x = ny$long, y = ny$lat) +
guides(fill = guide_colorbar(colours = topo.colors(10))) +
opts(legend.position = "top")
Why is it showing up as the same color?
Additional information from @PaulHiemstra
I've puzzling a bit on it, and could not get a good result. However, I also wonder why the example from the ggplot2 pdf you link to works.
This code produces a correct choropleth 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>
So in the link between the polygons and the data, some black magic seems to be happening. This might also explain the problems @TylerRinker has.
Upvotes: 3
Views: 1529
Reputation: 60984
This is documented behavior of geom_map
. geom_map
always draws the region
variable (or alternatively id
) from states_map
. This is confirmed by the following. Running:
ny$region = ny$subregion
puts the subregion
names into the region
column. Now plotting leads to the correct image:
So, geom_map
uses the region
or id
.
Upvotes: 5