2013-10-21 76 views
0

我想用一個數據表創建兩個版本的世界地圖。數據表的列是:國家名稱iso3 FormDate FormDate變量是一個日期 - 在這種情況下,該國家正式形成的日期。 (這裏看到的 - http://en.wikipedia.org/wiki/List_of_sovereign_states_by_date_of_formation用日期變量創建世界地圖

formdate <- read.table(text=" 
ISO3\tCountry\tFormation Date 
DZA\tAlgeria\t07-03-1962 
AGO\tAngola\t11-11-1975 
BWA\tBotswana\t09-30-1966 
CMR\tCameroon\t01-01-1960 
BGD\tBangladesh\t03-26-1971 
IND\tIndia\t08-15-1947 
ISR\tIsrael\t05-14-1948 
LOS\tLaos\t10-22-1953 
MYS\tMalaysia\t09-16-1963 
SGP\tSingapore\t08-09-1965" 
,sep="\t",header=TRUE) 

> formdate 
    ISO3 Country Formation.Date 
1 DZA Algeria  07-03-1962 
2 AGO  Angola  11-11-1975 
3 BWA Botswana  09-30-1966 
4 CMR Cameroon  01-01-1960 
5 BGD Bangladesh  03-26-1971 
6 IND  India  08-15-1947 
7 ISR  Israel  05-14-1948 
8 LOS  Laos  10-22-1953 
9 MYS Malaysia  09-16-1963 
10 SGP Singapore  08-09-1965 

我想創建的地圖是:

1:總結圖每個國家通過形成其一年顏色編碼,在說的5-自動分級7類。

2:三折面圖,單獨顯示1945年以前出生的獨立國家,1945年以後到1965年以及之後的任何時間。麻煩的是,我很樂意能夠改變這些截止年份。

這個問題與Using [R] maps package - colouring in specific nations on a world map,RScript to create World Map with own valuesHow to create a world map in R with specific countries filled in?有關,就像那些我也試圖用自己的值創建地圖一樣。

這裏的區別是我需要使用日期變量,特別是繪製這些日期的分箱值。

幫助和建議將不勝感激。

+0

你需要顯示你已經嘗試過的代碼並提供你的數據(例如,用'dput')。 – Thomas

+0

謝謝。我添加了一個簡短版本的數據表。我認爲如果我不包含代碼,讀者會更容易,我沒有比上面鏈接中的更好的東西。 – user2627717

回答

1

這是一個使用rworldmap的解決方案。

庫(rworldmap)

#Your data 
formdate <- read.table(text=" 
ISO3\tCountry\tFormation Date 
DZA\tAlgeria\t07-03-1962 
AGO\tAngola\t11-11-1975 
BWA\tBotswana\t09-30-1966 
CMR\tCameroon\t01-01-1960 
BGD\tBangladesh\t03-26-1971 
IND\tIndia\t08-15-1947 
ISR\tIsrael\t05-14-1948 
LOS\tLaos\t10-22-1953 
MYS\tMalaysia\t09-16-1963 
SGP\tSingapore\t08-09-1965" 
,sep="\t",header=TRUE) 

#just using year as a first step 
formdate$Formation.Year<-year(formdate$Formation.Date) 

#sPDF <- joinCountryData2Map(formdate, joinCode="ISO3", nameJoinColumn="ISO3") 
#joining by country name works better because of incorrect ISO3 code for Laos in the data 
sPDF <- joinCountryData2Map(formdate, joinCode="NAME", nameJoinColumn="Country") 

#Using defaults, change catMethod, numCats & colourPalette to change map appearance 
mapCountryData(sPDF, nameColumnToPlot="Formation.Year") 

...,爲第2位,產生3個面板與不同年份的地圖:

#I've used these breaks that work with your sample data, 
#simply cahnge to work with full dataset 
yearBreak1 <- 1960 
yearBreak2 <- 1970 

oldPar <- par(mar=c(0.7, 0, 0, 0)) #set margins for subplots top,bottom,left,right 
#use layout to set up 3 panels 
nPanels <- layout(cbind(c(0,1:3)) 
        , heights=c(lcm(0.5),c(1,1,1)) 
        , respect=F) 

#add a constant column to allow plotting all countries the same colour 
formdate$constant <- 1 

#subet data by yearBreaks 
dF1 <- formdate[ formdate$Formation.Year <= yearBreak1, ] 
dF2 <- formdate[ formdate$Formation.Year > yearBreak1 & formdate$Formation.Year <= yearBreak2, ]       
dF3 <- formdate[ formdate$Formation.Year > yearBreak2, ]       

#join to a map 
sPDF1 <- joinCountryData2Map(dF1, joinCode="NAME", nameJoinColumn="Country") 
sPDF2 <- joinCountryData2Map(dF2, joinCode="NAME", nameJoinColumn="Country") 
sPDF3 <- joinCountryData2Map(dF3, joinCode="NAME", nameJoinColumn="Country") 

#plot & add titles 
mapCountryData(sPDF1, nameColumnToPlot="constant", catMethod='categorical', addLegend=FALSE, mapTitle="") 
mtext(paste("<=",yearBreak1)) 
mapCountryData(sPDF2, nameColumnToPlot="constant", catMethod='categorical', addLegend=FALSE, mapTitle="") 
mtext(paste(">",yearBreak1,"& <=",yearBreak1)) 
mapCountryData(sPDF3, nameColumnToPlot="constant", catMethod='categorical', addLegend=FALSE, mapTitle="")  
mtext(paste(">",yearBreak2)) 

應該產生這樣的: rworldmap plot

+1

+1不錯的解決方案。出於好奇,你的獨特「佈局」電話背後的原因是什麼?爲什麼在左邊創建一列固定大小的空面板,而不是在調用par(mar = ...)'時添加左邊距? – plannapus

+2

謝謝plannapus!您發現特殊的佈局調用是正確的,解釋(相當糟糕!)是當我想要更多的控制時,我已經習慣使用佈局,然後對於更簡單的情況,我會抽出一些東西。就在我提交之前,我意識到你的par命令在這種情況下也會做得很好。 – Andy

+0

好吧,它是有道理的:)至少這不是失去了:你讓我發現論點'尊重'和功能'lcm'。 – plannapus

1

也許沒有這樣做的最直接的方式,但這裏是你的第一個問題的解決方案:

# First changing the class of the two columns we're going to use: 
formdate$Formation.Date<-as.Date(formdate$Formation.Date, "%m-%d-%Y") 
formdate$ISO3<-as.character(formdate$ISO3) 
formdate$ISO3[8]<-"LAO" #Laos'ISO3 code is "LAO" and not "LOS" in the wrld_simpl data 
# Make regular temporal interval 
intval <- cut(formdate$Formation.Date, 
       breaks = seq(min(formdate$Formation.Date),max(formdate$Formation.Date),length=5), 
       right=TRUE, include.lowest=TRUE) # So that the intervals include the lowest date and the highest 
# Make these values correspond with their polygons 
library(maptools) 
data(wrld_simpl) 
f <- rep(NA,nrow([email protected])) 
f[sapply(formdate$ISO3,function(x)which(wrld_simpl$ISO3==x))] <- intval 
# Plot 
plot(wrld_simpl, col=(1:6)[f]) 

關於你的第二點:

#This time you can define your breakpoints before hand (don't forget to include a minimum age and a maximum age) 
d <- c("1900/01/01","1945/01/01","1965/01/01","2020/01/01") 
intval <- cut(formdate$Formation.Date, 
        breaks = as.Date(d)) 
f <- rep(0,nrow([email protected])) 
f[sapply(formdate$ISO3,function(x)which(wrld_simpl$ISO3==x))] <- intval 

,然後繪圖:

par(mfcol=c(3,1)) #Here, three vertical panels 
for(i in 1:3){ 
    par(mar=c(0,0,0,0)) 
    plot(wrld_simpl) #First plot the whole world 
    if(any(f==i)){ 
     plot(wrld_simpl[f==i,],add=TRUE,col="red") #Then the countries that belong to the proper category 
     } 
    } 
+0

偉大的工作。謝謝。 – user2627717