====== World map in R ====== August 16-17, 2011 See also [[paper:poppyr:counties|drawing of US counties]]. The simplest way to draw the world map in R is > library(maps) > map("world") The problem with this approach is that the built-in map of the world is very old - before 1991. For example Yugoslavia is still in one piece. A fresh high-precision map of the world can be obtained at [[http://gadm.org/world|GADM / world]]. > setwd("C:/test/world") > library(maptools) > gpclibPermit() > W <- readShapeSpatial("gadm1_lev1.shp") Error: cannot allocate vector of size 442 Kb > This time the problem is the size of the world map data. With some searching on the web I found [[http://thematicmapping.org/downloads/world_borders.php|another world map data set]] of reasonable size - in fact it contains two data sets: a [[http://thematicmapping.org/downloads/TM_WORLD_BORDERS-0.3.zip|high-resolution]] and a [[http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip|low-resolution]] version. Now we can finally get the picture: > library(maptools) > gpclibPermit() > setwd("C:/test/world") > W <- readShapeSpatial("TM_WORLD_BORDERS-0.3.shp") > names(W) [1] "FIPS" "ISO2" "ISO3" "UN" "NAME" "AREA" [7] "POP2005" "REGION" "SUBREGION" "LON" "LAT" > N <- length(W$ISO2) > N [1] 246 > source("D:\\Data\\counties\\myColors.R") # my palettes > p <- sample(1:10,N,replace=TRUE) > pdf("world.pdf",width=11.7,height=8.3,paper="a4r") > plot(W,col=alphabet[p],bg="lightgoldenrodyellow",lwd=0.05,border="black") > dev.off() The palette ''alphabet'' is described in the note on [[notes:rcolor#alphabet_colors|the colors in R]]. {{notes:pics:world.png?720}} > png("worldEu.png",width=1170,height=830) > plot(W,xlim=c(-2,30),ylim=c(32,70),col=alphabet[p],bg="lightgoldenrodyellow",lwd=0.05,border="black") > dev.off() {{notes:pics:worldEu.png?720}} ===== Matching states in pyramids data sets and world map ===== 18. 8. 2011 > load("worldTabela.Rdata") > objects() [1] "tM1996" "tM1997" "tM1998" "tM1999" "tM2000" "tM2001" "tM2002" "tM2003" [9] "tM2004" "tM2005" "tM2006" "tM2007" "tZ1996" "tZ1997" "tZ1998" "tZ1999" [17] "tZ2000" "tZ2001" "tZ2002" "tZ2003" "tZ2004" "tZ2005" "tZ2006" "tZ2007" > length(tM1996) [1] 18 > names(tM1996) [1] "drzava" "0-4" "5-9" "10-14" "15-19" "20-24" "25-29" "30-34" [9] "35-39" "40-44" "45-49" "50-54" "55-59" "60-64" "65-69" "70-74" [17] "75-79" "80+" > s1 <- as.character(tM1996$drzava) > s1 [1] "Afghanistan" "Albania" [3] "Algeria" "American Samoa" [5] "Andorra" "Angola" [7] "Anguilla" "Antigua and Barbuda" [9] "Argentina" "Armenia" [11] "Aruba" "Australia" ............ [215] "Venezuela" "Vietnam" [217] "Virgin Islands" "Virgin Islands, British" [219] "Wallis and Futuna" "West Bank" [221] "Western Sahara" "Yemen" [223] "Zambia" "Zimbabwe" > library(maptools) > gpclibPermit() [1] FALSE > setwd("D:/Data/counties/9nations") > W <- readShapeSpatial("../world/TM_WORLD_BORDERS-0.3.shp") > W$ISO2[1:10] [1] AG DZ AZ AL AM AO AS AR AU BH 246 Levels: AD AE AF AG AI AL AM AN AO AQ AR AS AT AU AW AX AZ BA BB BD ... ZW > W$ISO3[1:10] [1] ATG DZA AZE ALB ARM AGO ASM ARG AUS BHR 246 Levels: ABW AFG AGO AIA ALA ALB AND ANT ARE ARG ARM ASM ATA ATF ATG ... ZWE > as.character(W$NAME[1:10]) [1] Antigua and Barbuda Algeria Azerbaijan [4] Albania Armenia Angola [7] American Samoa Argentina Australia [10] Bahrain > s2 <- as.character(W$NAME) To establish the relations between both sets of names we use the function ''match''. > p <- match(s1,s2) > p[1:10] [1] 31 4 2 7 133 6 128 1 8 5 > s1[1] [1] "Afghanistan" > s2[31] [1] "Afghanistan" Let us look for problematic names - countries with different writing of their names. > (m <- which(is.na(p))) [1] 15 29 45 46 58 72 73 89 94 107 108 111 116 120 121 134 194 197 216 [20] 217 218 219 220 > s1[m] [1] "Bahamas, The" "Brunei" [3] "Congo (Brazzaville)" "Congo (Kinshasa)" [5] "East Timor" "Gambia, The" [7] "Gaza Strip" "Hong Kong S.A.R." [9] "Iran" "Korea, North" [11] "Korea, South" "Laos" [13] "Libya" "Macau S.A.R." [15] "Macedonia" "Moldova" [17] "Syria" "Tanzania" [19] "Vietnam" "Virgin Islands" [21] "Virgin Islands, British" "Wallis and Futuna" [23] "West Bank" Comparing the second list s2 with the list of unmatched names from the first list > s2 [1] "Antigua and Barbuda" "Algeria" [3] "Azerbaijan" "Albania" [5] "Armenia" "Angola" [7] "American Samoa" "Argentina" [9] "Australia" "Bahrain" [11] "Barbados" "Bermuda" ...... [235] "San Marino" "Turks and Caicos Islands" [237] "Western Sahara" "Serbia" [239] "Holy See (Vatican City)" "Svalbard" [241] "Saint Martin" "Saint Barthelemy" [243] "Guernsey" "Jersey" [245] "South Georgia South Sandwich Islands" "Taiwan" > for(i in m) cat(i,': ',s1[i],'\n') 15 : Bahamas, The 29 : Brunei 45 : Congo (Brazzaville) ...... 218 : Virgin Islands, British 219 : Wallis and Futuna 220 : West Bank we manually identify the right pairs 15 : Bahamas, The [13] "Bahamas" 29 : Brunei [23] "Brunei Darussalam" 45 : Congo (Brazzaville) [27] "Congo" 46 : Congo (Kinshasa) [28] "Democratic Republic of the Congo" 58 : East Timor [229] "Timor-Leste" 72 : Gambia, The [66] "Gambia" 73 : Gaza Strip 89 : Hong Kong S.A.R. [130] "Hong Kong" 94 : Iran [84] "Iran (Islamic Republic of)" 107 : Korea, North [94] "Korea, Democratic People's Republic of" 108 : Korea, South [96] "Korea, Republic of" 111 : Laos [99] "Lao People's Democratic Republic" 116 : Libya [107] "Libyan Arab Jamahiriya" 120 : Macau S.A.R. [137] "Macau" 121 : Macedonia [112] "The former Yugoslav Republic of Macedonia" 134 : Moldova [172] "Republic of Moldova" 194 : Syria [192] "Syrian Arab Republic" 197 : Tanzania [205] "United Republic of Tanzania" 216 : Vietnam [216] "Viet Nam" 217 : Virgin Islands [217] "United States Virgin Islands" 218 : Virgin Islands, British [215] "British Virgin Islands" 219 : Wallis and Futuna [219] "Wallis and Futuna Islands" 220 : West Bank [139] "Palestine" > P <- p > P[m] <- c(13,23,27,28,229,66,NA,130,84,94,96,99,107,137,112,172,192,205,216,217,215,219,139) > which(is.na(P)) [1] 73 and produce the mapping P that for each (except 73: Gaza Strip) state from the data set determines the corresponding state on the map. To check it we print out the list of names from both sets: > for(i in 1:length(P)) {j <- P[i]; cat(i,j,as.character(W$ISO2[j]),as.character(W$ISO3[j]),s1[i],s2[j],'\n')} 1 31 AF AFG Afghanistan Afghanistan 2 4 AL ALB Albania Albania 3 2 DZ DZA Algeria Algeria 4 7 AS ASM American Samoa American Samoa ..... 72 66 GM GMB Gambia, The Gambia 73 NA NA NA Gaza Strip NA 74 68 GE GEO Georgia Georgia ..... 222 222 YE YEM Yemen Yemen 223 223 ZM ZMB Zambia Zambia 224 224 ZW ZWE Zimbabwe Zimbabwe On the map there are also some other states not listed in the first list. > q <- match(s2,s1) > M <- which(is.na(q)) > s2[M] [1] "Bahamas" "Brunei Darussalam" [3] "Congo" "Democratic Republic of the Congo" [5] "French Guiana" "Falkland Islands (Malvinas)" [7] "Gambia" "Iran (Islamic Republic of)" [9] "Korea, Democratic People's Republic of" "Korea, Republic of" [11] "Lao People's Democratic Republic" "Libyan Arab Jamahiriya" [13] "Martinique" "The former Yugoslav Republic of Macedonia" [15] "Niue" "Hong Kong" [17] "Macau" "Palestine" [19] "Ĺland Islands" "Norfolk Island" [21] "Cocos (Keeling) Islands" "Antarctica" [23] "Bouvet Island" "French Southern and Antarctic Lands" [25] "Heard Island and McDonald Islands" "British Indian Ocean Territory" [27] "Christmas Island" "United States Minor Outlying Islands" [29] "Reunion" "Republic of Moldova" [31] "Syrian Arab Republic" "Tokelau" [33] "United Republic of Tanzania" "British Virgin Islands" [35] "Viet Nam" "United States Virgin Islands" [37] "Wallis and Futuna Islands" "Guadeloupe" [39] "Timor-Leste" "Pitcairn Islands" [41] "Holy See (Vatican City)" "Svalbard" [43] "Saint Martin" "Saint Barthelemy" [45] "South Georgia South Sandwich Islands" ===== URLs ===== * [[http://cran.r-project.org/web/packages/maptools/maptools.pdf|Maptools manual]] * [[http://cran.r-project.org/web/packages/maps/maps.pdf|Maps manual]] * [[http://cran.r-project.org/web/packages/RgoogleMaps/vignettes/RgoogleMaps-intro.pdf|RgoogleMaps vignette]] * [[http://www.stanford.edu/~cengel/cgi-bin/anthrospace/download-global-administrative-areas-as-rdata-files|Swiss language regions - example]] * [[http://r-spatial.sourceforge.net/|Spatial data in R / Sourceforge]] * http://r.789695.n4.nabble.com/Projecting-data-on-a-world-map-using-long-lat-td3081298.html * Paul Murrell: [[http://www.stat.auckland.ac.nz/~paul/RGraphics/|RGraphics]] * [[http://my.safaribooksonline.com/book/programming/r/9781849513067/creating-maps/ch09lvl1sec03|R Graphs Cookbook]] * Yuri M. Zhukov: [[http://www.people.fas.harvard.edu/~zhukov/spatial.R|Applied Spatial Statistics in R]] * [[http://flowingdata.com/2011/05/11/how-to-map-connections-with-great-circles/|How to map connections with great circles]] * http://www.dplot.com/maps.htm * http://www.facebook.com/note.php?note_id=469716398919 * http://www.mail-archive.com/r-help@r-project.org/msg100579.html * [[http://blog.thematicmapping.org/|thematic mapping blog]] * http://www.stat.berkeley.edu/users/spector/s133/Graphics2a.html