R for map (4下):rworldmap
2.rworldmap进阶
(1)聚合数据
rworldmap可以把网格数据和国家水平进行聚合,也可以把国家水平数据和世界区域进行聚合。
mapHalfDegreeGridToCountries( )函数输入网格数据,可以聚合到国家水平并绘图。它的参数和mapCountryData()
函数类似。
mapParams <- mapHalfDegreeGridToCountries(gridExData, addLegend=FALSE)
do.call( addMapLegend, c(mapParams, legendWidth=0.5, legendMar = 2))
country2Region()函数可以通过参数regionType把国家水平数据和世界地理区域进行聚合,输出文本信息。可有mapByRegion()函数绘制区域类型的地图。区域有四种:SRES, GEO3, Stern 和 GBD。
例
data(countryExData)
country2Region(countryExData
,nameDataColumn="PM10"
,joinCode="ISO3"
,nameJoinColumn="ISO3V10"
,regionType="SRES"
,FUN="mean")
#输出
meanPM10bySRES
Central and Eastern Europe (EEU) 29.34545
Centrally planned Asia and China (CPA) 63.36000
Latin America and the Caribbean (LAM) 50.03750
Middle East and North Africa (MEA) 90.39375
Newly Independent States of FSU (FSU) 36.15333
North America (NAM) 20.85000
Other Pacific Asia (PAS) 48.41000
Pacific OECD (PAO) 20.86667
South Asia (SAS) 96.42000
Sub-Saharan Africa (AFR) 59.48158
Western Europe (WEU) 25.30500
#绘图
mapDevice() #create world map shaped window
mapByRegion(countryExData
,nameDataColumn="PM10"
,joinCode="ISO3"
,nameJoinColumn="ISO3V10"
,regionType="SRES"
,FUN="mean")
(2)修正地图表现
rworldmap的绘图参数有大量的可选参数用来改善地图的表现形式。
catMethod:数据值如何放入对应类别(category)。
colourPalette:指定调色板,决定类别的颜色。
numCats:分配数据进入的类别数。
addLegend:决定图例,如果取值为FALSE,则可以用addMapLegend ()或
addMapLegendBoxes()函数创建图例。
mapRegion:对一个区域进行放缩。
例 15岁以下的世界人口分布
library(XML)
url<-"http://en.worldstat.info/World/List_of_countries_by_Population_under_15_years_old"
table<-readHTMLTable(url)
raw<-table[4]
data<-raw[3:226,c(1,2)]
names(data)<-c("country","pop")
data$pop<-as.numeric(gsub(",",'',data$pop))
library(rworldmap)
map <- joinCountryData2Map(data,joinCode='NAME',
nameJoinColumn = "country" )
#建立调色板
op <- palette(c('green','yellow','orange','red'))
#按四分位数分化数据
cutVector <- quantile(map$pop,na.rm=TRUE)
#分类的数据转化为因子
map$category <- cut(map$pop, cutVector, include.lowest=TRUE )
#指定因子名称
levels(map$category) <- c('low', 'med', 'high', 'vhigh')
mapCountryData( map , nameColumnToPlot='category',
catMethod='categorical',mapTitle='15岁以下的世界人口分布',
colourPalette='palette',
oceanCol='lightblue', missingCountryCol='white')
下面把区域限定在亚洲
mapCountryData(map, nameColumnToPlot='category',
mapTitle='亚洲各国15岁以下人口的比例',
colourPalette='palette',
oceanCol='lightblue',
missingCountryCol='white',
mapRegion="asia",
borderCol='black')
(3)在地图上对国家定位
identifyCountries( )是一个交互函数,允许使用者在当前地图上通过鼠标点击来显示对应国家的名称。比如,在上面的图绘出之后,写下
identifyCountries(getMap(), nameColumnToPlot="category")
可以得到这样的效果。
(1)聚合数据
rworldmap可以把网格数据和国家水平进行聚合,也可以把国家水平数据和世界区域进行聚合。
mapHalfDegreeGridToCountries( )函数输入网格数据,可以聚合到国家水平并绘图。它的参数和mapCountryData()
函数类似。
mapParams <- mapHalfDegreeGridToCountries(gridExData, addLegend=FALSE)
do.call( addMapLegend, c(mapParams, legendWidth=0.5, legendMar = 2))
country2Region()函数可以通过参数regionType把国家水平数据和世界地理区域进行聚合,输出文本信息。可有mapByRegion()函数绘制区域类型的地图。区域有四种:SRES, GEO3, Stern 和 GBD。
例
data(countryExData)
country2Region(countryExData
,nameDataColumn="PM10"
,joinCode="ISO3"
,nameJoinColumn="ISO3V10"
,regionType="SRES"
,FUN="mean")
#输出
meanPM10bySRES
Central and Eastern Europe (EEU) 29.34545
Centrally planned Asia and China (CPA) 63.36000
Latin America and the Caribbean (LAM) 50.03750
Middle East and North Africa (MEA) 90.39375
Newly Independent States of FSU (FSU) 36.15333
North America (NAM) 20.85000
Other Pacific Asia (PAS) 48.41000
Pacific OECD (PAO) 20.86667
South Asia (SAS) 96.42000
Sub-Saharan Africa (AFR) 59.48158
Western Europe (WEU) 25.30500
#绘图
mapDevice() #create world map shaped window
mapByRegion(countryExData
,nameDataColumn="PM10"
,joinCode="ISO3"
,nameJoinColumn="ISO3V10"
,regionType="SRES"
,FUN="mean")
(2)修正地图表现
rworldmap的绘图参数有大量的可选参数用来改善地图的表现形式。
catMethod:数据值如何放入对应类别(category)。
colourPalette:指定调色板,决定类别的颜色。
numCats:分配数据进入的类别数。
addLegend:决定图例,如果取值为FALSE,则可以用addMapLegend ()或
addMapLegendBoxes()函数创建图例。
mapRegion:对一个区域进行放缩。
例 15岁以下的世界人口分布
library(XML)
url<-"http://en.worldstat.info/World/List_of_countries_by_Population_under_15_years_old"
table<-readHTMLTable(url)
raw<-table[4]
data<-raw[3:226,c(1,2)]
names(data)<-c("country","pop")
data$pop<-as.numeric(gsub(",",'',data$pop))
library(rworldmap)
map <- joinCountryData2Map(data,joinCode='NAME',
nameJoinColumn = "country" )
#建立调色板
op <- palette(c('green','yellow','orange','red'))
#按四分位数分化数据
cutVector <- quantile(map$pop,na.rm=TRUE)
#分类的数据转化为因子
map$category <- cut(map$pop, cutVector, include.lowest=TRUE )
#指定因子名称
levels(map$category) <- c('low', 'med', 'high', 'vhigh')
mapCountryData( map , nameColumnToPlot='category',
catMethod='categorical',mapTitle='15岁以下的世界人口分布',
colourPalette='palette',
oceanCol='lightblue', missingCountryCol='white')
下面把区域限定在亚洲
mapCountryData(map, nameColumnToPlot='category',
mapTitle='亚洲各国15岁以下人口的比例',
colourPalette='palette',
oceanCol='lightblue',
missingCountryCol='white',
mapRegion="asia",
borderCol='black')
(3)在地图上对国家定位
identifyCountries( )是一个交互函数,允许使用者在当前地图上通过鼠标点击来显示对应国家的名称。比如,在上面的图绘出之后,写下
identifyCountries(getMap(), nameColumnToPlot="category")
可以得到这样的效果。
为什么运行到这一步就出错
raw<-table[4]
前面r for map(3下)里面也有类似的
raw<-table[[6]]也是出错,提示 Error in table[[6]] : subscript out of bounds
改成raw<-table[[4]]就可以了
r for map(3下)里面也有类似的
raw<-table[[6]]也是出错,是因为“[”都是英文的[,不是中文【
为什么这一步会报错呢?
> data<-raw[3:226,c(1,2)]
Error in raw[3:226, c(1, 2)] : incorrect number of dimensions
> 我来回应