Ray Brownrigg
2004-Mar-07 22:46 UTC
[R] drawing filled countries according to data using map('world')?
> My question then is, how to compute a similar procedure using the 'world' > database. Specifically, how can I access the country names in the 'world' > database to accomplish the translation to the country names in my dataset? > Is there any way to unpack the 'world' database to do the matching in an > external program? And does anybody now of other (more recent) world maps > that I could use? >The short answer is that the file world.N in .../library/maps/mapdata/ contains the mapping from polygon numbers to names. I think the only other thing you need to know is that the colours are allocated sequentially in increasing numerical order of polygon number. Ray Brownrigg
Jens Hainmueller
2004-Mar-07 23:49 UTC
[R] drawing filled countries according to data using map('world')?
Hello, I am looking for somebody who has experience with the map library (Becker and Wilks 1993) and might be able to help me with the following problem: Using the 'world' database I would like to draw filled countries in a world map so that the filling colors of each country corresponds to the value of a policy variable X at time t (the goal is to visualize a policy diffusion pattern over time using different maps for t=1985, 1990, etc.). In their explanatory note, Becker and Wilks show how to accomplish this with the 'states' database, for filling US states with color according to the republican vote in 1900.> state.names <- unix(?tr "[A-Z]" "[a-z]"?, state.name) > map.states <- unix(?sed "s/:.*//"?, map(names=T, plot=F)) > state.to.map <- match(map.states, state.names) > color <- votes.repub[state.to.map, votes.year == 1900] / 100 > map(?state?, fill=T, col=color) > map(?state?, add=T)"The first expression changes uppercase to lowercase in the standard S dataset giving state names, so that these can be compared with the names returned by map. Next the complete set of state polygon names is requested (using map(names=T,plot=F); the default database is ?state?) and the trailing portions (from the '':?? onwards) are removed so that we have a list of the state for which each polygon is a part or the whole. Then we create state.to.map that gives the translation from the ordering of the states known to S (alphabetical) to the ordering known to the mapping mechanism. By using this vector, as in the next expression, all the pieces of a state will be colored the same color. The state.to.map vector is a useful one to keep around, for it will work in any context where the ordering of the state data is as here. Notice that unless such a vector is being reused, it will usually be the case that there will be a step like this one, finding the translation between the ordering for the regions in your data and the ordering according to map. In general, the translation will have to be computed each time the set of selected polygons changes." My question then is, how to compute a similar procedure using the 'world' database. Specifically, how can I access the country names in the 'world' database to accomplish the translation to the country names in my dataset? Is there any way to unpack the 'world' database to do the matching in an external program? And does anybody now of other (more recent) world maps that I could use? Thanks very much! Best, Jens