0

我正在尝试使用一个数据表创建两个版本的世界地图。数据表的列是: Country Name 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:一张 3 折面图,用单独的面板显示 1945 年、1945 年之后到 1965 年以及之后的任何时间出生的独立国家。麻烦的是,我希望能够改变这些截止年份。

这个问题与使用 [R] maps package-coloring in specific countries on a world mapRScript to create World Map with own valuesHow to create a world map in R with specific countries? ,就像那些我也在尝试创建一个具有自己价值观的地图。

这里的区别是我需要使用日期变量,特别是绘制这些日期的分箱值。

帮助和建议将不胜感激。

4

2 回答 2

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(wrld_simpl@data))
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(wrld_simpl@data))
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
       }
   }
于 2013-10-21T08:16:13.123 回答
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")

...并为第二位制作 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 图

于 2013-10-21T14:39:47.260 回答