Skip to content

Adding some data as colors to world map

9 messages · Miha Staut, Roger Bivand, Gregor Gorjanc

#
Hello!

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.

Many thanks!
#
On Mon, 6 Mar 2006, Gregor Gorjanc wrote:

            
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?

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).

Does anybody have recommended websites for current European national 
border data (preferably in a format we can read)?

Roger

  
    
#
Roger Bivand wrote:
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.
Yes, my country is missing ;)
Hmm, this is then the main bottleneck - the maps.

  
    
#
On Mon, 6 Mar 2006, Gregor Gorjanc wrote:

            
First hit on google: "world" "shapefile" is:

http://aprsworld.net/gisdata/world/

leading to:

http://data.geocomm.com/editorspicks/data_world11.html

in geographical coordinates and the WGS84 datum; using the MapInfo files 
rather than the derived shapefile without spatial reference system 
metadata:
# takes some considerable time to check the polygons,
  # but save(world, ...) makes this a once only cost
Object of class SpatialPolygonsDataFrame
Coordinates:
         min       max
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 
+no_defs]
Data attributes:
           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                      
      Area         
 Min.   :       0  
 1st Qu.:    2530  
 Median :   78149  
 Mean   :  677622  
 3rd Qu.:  409047  
 Max.   :16631081  

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).
+ unique=TRUE)

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:

http://www.iso.org/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html

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 
read.csv2()?
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.

Roger

  
    
#
Try this link http://thuban.intevation.org/download.html and search for the
freegis_worlddata either simplified or the detailed map. You may find Slovenia
on both of them. Import the shapefiles with the command read.shape from the
maptools library. The data you want to display in colour are stored in
imported.shp$att.data. So add the column with your data to this data.frame and
plot it with plot as explained in help pages of plot.Map. 

library(maptools)
w.data <- read.shape("countries.shp")
# edit data in w.data$att.data
plot(w.data, auxvar=w.data$att.data$your.variable) 
# play with aditional arguments of plot.Map as "recs" to plot only the european
part, either "fg" or "color" for the color ramp, "type" etc. 

All best, Lep pozdrav,
Miha Staut
1 day later
#
On Mon, 6 Mar 2006, Gregor Gorjanc wrote:

            
I've produced a world map with ISO3166 two-letter codes. To get your codes 
right - this lists out country names and codes:

as(world_countries, "data.frame")

then

countries <- c("AU", "US", "AR")
colours <- c("blue", "red", "green")
plot(world_countries, col=colours[match(world_countries$ISO3166, countries)])

gives the attached PNG. Shall I make an .RData file of the
SpatialPolygonsDataFrame object available?

Roger

  
    
2 days later
#
Hello!

I appologize for my absence but I was completely stuffed with my work in
last days. I would like to express thanks to all who took their time and
showed various ways how to accomplish my task. I did not expect to get
so many valuable responses. I will plot data from FAOSTAT database
http://faostat.fao.org/faostat/collections?subset=agriculture

...
OK, I will need to do the mapping, since FAO uses full names for
countries, but that is a minor problem. I will just create a list of
mappings and apply it.
Very clean and nice solution. I like it. If I would also go for bubbles,
how can I determine the position where to put the bubble on i.e. "the
center" of the country?
Yes, please do.

Many thanks again!!!

P.S. In one of previous posts you mentioned
Can you please elaborate a bit further on this topic since I do not
follow it fully. Perhaps you can show on above example.

P.P.S. Did you "get any further" with Giovanni's tutorial and recent
changes in maptools?
3 days later
#
On Sat, 11 Mar 2006, Gregor Gorjanc wrote:

            
Using world_countries.rda (see below for access) and matching against a 
short sample csv from FAO (they don't seem to give access to more than 25 
line samples without registration) - sample for Oceania:
as.character(world_countries$names))]
 [1] AS   AU   CK   <NA> PF   GU   KI   MH   <NA> NC   NZ   NU   NF   <NA> PW  
[16] PG   WS   SB   TO   TV   VU   <NA>
230 Levels: AD AE AF AG AI AL AM AN AO AR AS AT AU AW AZ BA BB BD BE BF ... ZW

looks promising - you'll still have a bit of manual editing to add the 
ISO3166 codes to the FAO data. But it can be done once-off, I guess, as 
you said.
The classInt package new on CRAN can help, if you like, with choosing 
colours for the polygons or bubbles.
symbols(coordinates(world_countries), ...)

should do it. Have a look at Susumu Tanimura et al. in JSS 2006 for a nice 
discussion of symbol size.
On:

http://spatial.nhh.no/R/etc/world_countries.rda

now, please let me know if it works for you. Please use library(sp) to 
define the loaded object.
The specific choice of the projection depends on which part of the world 
you want to present, and on other cartographic criteria. This is a good 
reference:

http://www.colorado.edu/geography/gcraft/notes/mapproj/mapproj_f.html

but there are thousands of interesting choices, I'm afraid. Projection is 
supported in the newest version of rgdal on CRAN using the transform() 
method, but you need to know which coordinate reference system to go to. 
If there is a typical projection used in your field, it should be 
identifiable, and can then be done to make a projected basemap.
Not yet, but it'll come before too long!

Best wishes,

Roger

  
    
1 day later
#
Hi!

Roger, many thanks for all this. I installed package sp and successfully
loaded object you provided and ploted the image you attached last time.
This is really nice. Thanks also for other comments for R and projections.

Btw. will you remove that world_countries.rda object or will it be
accessible in an R package or ???

Good luck!
Roger Bivand wrote: