TA的每日心情 | 奋斗 2015-7-30 23:05 |
---|
签到天数: 12 天 [LV.3]偶尔看看II
管理员
- 积分
- 4804
|
一、调用相关包 - install.packages(c("maps", "mapdata","sp“,"maptools"))
- library(maps)
- library(mapdata)
- library(sp)
- library(maptools)
- map("china")
复制代码- x=readShapePoly('D:/工作文件夹/R语言与中国地图/chinaprovinceborderdata_tar_gz/bou2_4p.shp')
- plot(a)
- plot(a,col=gray(924:0/924))
- getColor=function(mapdata,provname,provcol,othercol)
- {
- f=function(x,y) ifelse(x %in% y,which(y==x),0);
- colIndex=sapply(mapdata@data$NAME,f,provname);
- col=c(othercol,provcol)[colIndex+1];
- return(col);
复制代码- #读取数据
- b<-read.table("D:/工作文件夹/R语言与中国地图/population.txt",head=T)
- provname<-as.matrix(b[,2])
- pop<-as.matrix(b[,3])
- provcol=rgb(red=1-pop/max(pop)/10,green=1-pop/max(pop)/2,blue=0.2)
- plot(x,col=getColor(x,provname,provcol,"white"),xlab="",ylab="")
复制代码
二、利用google数据 - library(googleVis) #类似的包还有ggmap 等
- b<-read.table("D:/工作文件夹/R语言与中国地图/population.txt",head=T)
- G3 <- gvisGeoChart(b, locationvar='ISO代码',
- colorvar='人口数量',options=list(region='CN',
- displayMode="regions",resolution="provinces",
- colorAxis="{colors: ['yellow','red']}" ))
- plot(G3)
复制代码
三、中国航线地图
- library(maptools)
- x=readShapePoly('D:/工作文件夹/R语言与中国地图/chinaprovinceborderdata_tar_gz/bou2_4p.shp')
- plot(x)
- getColor=function(mapdata,provname,provcol,othercol)
- {
- f=function(x,y) ifelse(x %in% y,which(y==x),0);
- colIndex=sapply(mapdata@data$NAME,f,provname);
- col=c(othercol,provcol)[colIndex+1];
- return(col);}
- #自定义函数
- Par(mar(0,0,1,0),bg=”gray”)
- plot(x,col="black",border="black")
- #航线数据处理
- library(stringr)
- data.port<-read.csv('D:\\工作文件夹\\R语言与中国地图\\airports.dat',F)
- data.line<-read.csv('D:\\工作文件夹\\R语言与中国地图\\ routes.dat',F)
- chinaport<-str_detect(data.port[,'V4'],"China")
- chinaport <- data.port[chinaport,]
- chinaport<-chinaport[chinaport$V5!='',c('V3','V5','V7','V8','V9')] names(chinaport)<-c('city','code','lan','lon','att')
- chinaport[2,]
- # 找出国内航班
- lineinchina <- (data.line[,'V3'] %in% chinaport$code) & (data.line[,'V5'] %in% chinaport$code)
- chinaline <- data.line[lineinchina,c('V3','V5','V9')]
- names(chinaline)<-c('f','t','a’)
- #构建一个函数,根据机场编码得到经纬度
- findposition <- function(code) {
- find <- chinaport$code==code
- x <- chinaport[find,'lon']
- y <- chinaport[find,'lan']
- return(data.frame(x,y))
- }
- # 将机场编码转为经纬度
- from <- lapply(as.character(chinaline$source),findposition)
- from <- do.call('rbind',from)
- from$group <- 1:dim(from)[1]
- names(from) <- c('lon','lan','group')
-
- to <- lapply(as.character(chinaline$destination),findposition)
- to <- do.call('rbind',to)
- to$group <-1:dim(to)[1]
- names(to) <-c('lon','lan','group')
- data.line <- rbind(from,to)
- temp<- data.line[data.line$group<100,]
- #作图
- par(bg=”gray”)
- plot(x,col="black",border="black")
- points(data.line$lon,data.line$lan, pch = 19, col ="white",cex=0.1)
- for(i in 1:5832){segments(data.line[i,1], data.line[i,2], data.line[i+5832,1],data.line[i+5832,2], col= 'blue')
- }
- for(i in 1:100){ gcIntermediate (data.line[i,1], data.line[i,2], data.line[i+100],data.line[i+100,2])
- }
复制代码
- ggmap(get_googlemap(center = 'china', zoom=4,
- maptype='roadmap'),extent='device')+
- geom_point(data=chinaport,aes(x=lon,y=lan),
- colour = 'red',alpha=0.8)+
- geom_line(data=data.line,aes(x=lon,y=lan,group=group),
- size=0.1,alpha=0.05,color='blue')
复制代码
- par(bg=”gray”)
- plot(x,col="black",border="black")
- points(data.line$lon,data.line$lan, pch = 19, col ="white",cex=0.2)
- for(i in 1:1000){segments(data.line[i,1], data.line[i,2], data.line[i+1000,1],data.line[i+1000,2], col= 'blue' )
- }
复制代码
- #弧线及其颜色处理
- library(maps)
- library(maptools)
- library(geosphere)
- map("china",col="gray20",bg="black",mar=c(0,0,0,0))
- title("中国航线图", sub = " SimSun",
- cex.main =1, col.main= "gray",)
- points(data.line$lon,data.line$lan, pch = 19, col ="white",cex=0.1)
- from <- lapply(as.character(linecount$source),findposition)
- from <- do.call('rbind',from)
- to <- lapply(as.character(linecount$destination),findposition)
- to <- do.call('rbind',to)
- map("china",col="gray20",bg="black",mar=c(0,0,0,0))
- title("中国航线图", sub = " SimSun",
- cex.main =1, col.main= "gray",)
- points(data.line$lon,data.line$lan, pch = 19, col ="white",cex=0.1)
- #颜色调整
- library(RColorBrewer)
- display.brewer.all(type = "seq")
- display.brewer.all()
- color<-brewer.pal(9,"PRGn")
- color<-colors()[grep("gray",colors())]
- map("china",col="gray20",bg="black",mar=c(0,0,0,0))
- title("中国航线图", sub = " SimSun", col.main= "gray",)
- points(data.line$lon,data.line$lan, pch = 19, col ="white",cex=0.1)
- for(i in 1:1304)
- { n= ceiling(18/linecount[i,3])
- lines(gcIntermediate(from[i,], to[i,], n=100,addStartEnd=T) ,col="blue")
- }
复制代码
R语言交流QQ群:99598210(欢迎加入) |
|