[R-sig-Geo] Adding some data as colors to world map
Roger.Bivand at nhh.no
Tue Mar 7 09:01:35 CET 2006
On Mon, 6 Mar 2006, Gregor Gorjanc wrote:
> Roger Bivand wrote:
> > On Mon, 6 Mar 2006, Gregor Gorjanc wrote:
> >>I am a total newbie to map handling, but a keen R user. I have some
> >>estimates by numbers per country (in names, such as Germany, France,
> >>...) and would like to add this information with use of color on world
> >>and perhaps additionally to Europe map. How can this be achieved in R?
> >>Any pointers to relevant literature/tutorials would be fine.
> > Welcome! The answer depends on how you want to represent the countries
> > graphically. Do you want to display the formatted numbers on a map of
> > country borders, to fill the country polygons with colour or shading
> > representing the values of the numbers, or maybe to put a filled bubble at
> > some point representing the countries?
> Filling country polygons with different colors would be just fine.
> Perhaps also filled bubble, but that one might not be so good since our
> eyes are bad at interpreting circles. Are there any examples where I
> could start with? Say I would like to color Australia with blue, states
> with red, Argentina with green etc. I will rather play with some color
> palette, but that would be more than great for the start.
> > Which countries? Are they already in the maps package (maybe not, some of
> > the borders there are rather out-of-date)?
> > library(maps)
> > map("world", xlim=c(-12,35), ylim=c(35,65))
> > shows you a pretty old version of Europe! There are plenty of sources of
> > border maps, but getting something matching the borders you need may
> > require a bit of googling (usually looking for a vector representation).
> Yes, my country is missing ;)
> > Does anybody have recommended websites for current European national
> > border data (preferably in a format we can read)?
> Hmm, this is then the main bottleneck - the maps.
First hit on google: "world" "shapefile" is:
in geographical coordinates and the WGS84 datum; using the MapInfo files
rather than the derived shapefile without spatial reference system
> world <- readOGR("country_col.TAB", "country_col")
# takes some considerable time to check the polygons,
# but save(world, ...) makes this a once only cost
Object of class SpatialPolygonsDataFrame
r1 -180.0000 180.00000
r2 -59.4409 83.67473
Is projected: FALSE
proj4string : [+proj=longlat +ellps=WGS84 +towgs84=0,0,0,-0,-0,-0,0
Country_Name Capital Population_approx
: 3 : 12 Min. :0.000e+00
CANADA : 2 AVARUA : 2 1st Qu.:1.715e+05
MARSHALL ISLANDS: 2 CHARLOTTE AMALIE: 2 Median :3.819e+06
AFGHANISTAN : 1 KINGSTON : 2 Mean :2.535e+07
ALBANIA : 1 MAJURO : 2 3rd Qu.:1.392e+07
ALGERIA : 1 OTTAWA : 2 Max. :1.179e+09
(Other) :233 (Other) :221
Min. : 0
1st Qu.: 2530
Median : 78149
Mean : 677622
3rd Qu.: 409047
The Country_Name factor isn't quite clean, there are three objects with no
name, CANADA is included twice, and the MARSHALL ISLANDS entry appears to
be messed up (appears to be partly the same as the no-name objects).
> world$uCountry_Name <- make.names(as.character(world$Country_Name),
gives something to match against. So now the next thing is to add ISO
country codes (or other suitable codes) to this dataset, and drop rows
(and polygons) not present. This looks promising:
and from the left navbar, there's a link to download for TXT, semicolon
delimited, 5 KB, XML, zipped file, 3 KB versions. Maybe library(XML) or
> plot(world, xlim=c(-12,35), ylim=c(35,65))
certainly looks much better!
So now we need match these spatial objects to an index, to know which
indexed countries you have in your data, and how to add NAs to country
rows present on the map for which you have no data.
> >>Many thanks!
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no
More information about the R-sig-Geo