download.file(url = 'http://dapengde.com/r4rookies/us.csv',
destfile ='F:/R lab/学R/r4r/us.csv' )
为什么我用download.file(),按下F1没反应呢?
download.file(url = 'http://dapengde.com/r4rookies/us.csv',
destfile ='F:/R lab/学R/r4r/us.csv' )
us <- read.csv('F:/R lab/学R/r4r/us.csv')
nlevels(us$state) #49个洲
#给洲画个大致雏形
plot(us$lon,us$lat) #居然这么简单就把图形画出来了
points(us$lon[us$state == 'Texas'],
us$lat[us$state == 'Texas'], col = 'blue')
cols <- rainbow(nlevels(us$state)) #给洲涂上彩虹色
plot(us$lon,us$lat,col = cols[us$state], pch = 20) #无与伦比的美丽地图
#给洲添加名称,
lon.median <- tapply(us$lon,us$state,median)
lat.median <- tapply(us$lat,us$state,median)
text(labels = levels(us$state), x = lon.median, y = lat.median,
cex = 0.5, col = 'White')
points(-74.02, 40.73, pch = 17, cex = 2)
课后作业
> us <- read.csv('F:/R lab/学R/r4r/us.csv')
> plot(us$lon,us$lat)
> cols <- rainbow(nlevels(us$state))
> plot(x = us$lon, y = us$lat, col = cols[us$state], pch = 20)
> lon.median <- tapply(us$lon,us$state,median)
> lat.median <- tapply(us$lat,us$state,median)
> text(labels = levels(us$state), x = lon.median, y = lat.median,
+ cex = 0.5, col = 'White')
> abline(h = seq(from = 25, to = 50, by = 5),col = 'grey')
> unique(us$state[us$lat > 34.9 & us$lat < 35.1])
[1] California Arizona New Mexico Texas Oklahoma
[6] Arkansas Tennessee North Carolina South Carolina Mississippi
[11] Alabama Georgia
49 Levels: Alabama Arizona Arkansas California Colorado Connecticut ... Wyoming
timez <- as.factor(us$lon %/% 15) #将经度按照每15度来划分,取其商部分
mycol <- rainbow(nlevels(timez))[timez] #根据商的取值来分色
plot(us$lon,us$lat, col=mycol,pch = 20)
#矢量地图
download.file(url = 'http://dapengde.com/r4rookies/cm.zip',
destfile ='F:/R lab/学R/r4r/cm.zip' )
unzip(zipfile = 'F:/R lab/学R/r4r/cm.zip',exdir = 'F:/R lab/学R/r4r')
require(rgdal)
cm <- readOGR(dsn = 'F:/R lab/学R/r4r/cm',layer = 'bou2_4p')
plot(cm)
查看省市名称,下载空气污染数据
根据污染的情况对严重程度进行评级
summary(cm)
is.factor(cm$NAME)
levels(cm$NAME) #查看省市名称
#下载空气数据
download.file(url = 'http://dapengde.com/r4rookies/aqi.csv',
destfile ='F:/R lab/学R/r4r/aqi.csv' )
aqi <- read.csv('F:/R lab/学R/r4r/aqi.csv')
head(aqi)
#对空气质量进行分级
aqstandard <- c(0,50,100,150,200,300,500,Inf)
aqilevel <- cut(aqi$aqi[match(levels(cm$NAME),aqi$province)],
aqstandard) #将aqi省份按照cm中的顺序排列后, 表明其所属空气质量范围,左开右闭区间
#将颜色按照实际空气质量所涵盖的级别多少, 用相应个数的灰度显示
mycol <- grey(seq(1, 0,
length.out = nlevels(aqilevel)))[aqilevel]
col <- cm$NAME
levels(col) <- mycol
plot(cm ,col = as.character(factor(col)), axes = TRUE)
#用plotrix包给地图添加漂亮的图例
library(plotrix)
legendn <- character((length(aqstandard) - 2) * 2 + 1)
legendn[seq(2, length(legendn), by = 2)] <-
aqstandard[2:(length(aqstandard)-1)]
color.legend(
xl = 135,yb = 10,xr = 137,yt = 30,legend = legendn,
rect.col = grey(seq(1,0,length.out = nlevels(aqilevel))),
align = 'lb',gradient = 'y' #添加图例
)
color.legend(
xl = 135,yb = 10,xr = 137,yt = 30,
legend = c('Excellent','Good','Lightly','Moderately','Heavily','Severely','OMG'),
rect.col = grey(seq(1,0,length.out = nlevels(aqilevel))),
align = 'rb',gradient = 'y' #添加图例
)
> plot(cm)
> aqi <- read.csv('F:/R lab/学R/r4r/aqi.csv')
> #对空气质量进行分级
> aqstandard <- c(0,50,100,150,200,300,500,Inf)
> aqilevel <- cut(aqi$aqi[match(levels(cm$NAME),aqi$province)],
+ aqstandard)
> #将颜色按照实际空气质量所涵盖的级别多少, 用相应个数的灰度显示
> mycol <- rev(rainbow(nlevels(aqilevel)))[aqilevel]
> col <- cm$NAME
> levels(col) <- mycol
> plot(cm ,col = as.character(factor(col)), axes = TRUE)
> #用plotrix包给地图添加漂亮的图例
> library(plotrix)
> legendn <- character((length(aqstandard) - 2) * 2 + 1)
> legendn[seq(2, length(legendn), by = 2)] <-
+ aqstandard[2:(length(aqstandard)-1)]
> color.legend(
+ xl = 135,yb = 10,xr = 137,yt = 30,legend = legendn,
+ rect.col = rev(rainbow(nlevels(aqilevel))),
+ align = 'lb',gradient = 'y' #添加图例
+ )
> color.legend(
+ xl = 135,yb = 10,xr = 137,yt = 30,
+ legend = c('Excellent','Good','Lightly','Moderately','Heavily','Severely','OMG'),
+ rect.col = rev(rainbow(nlevels(aqilevel))),
+ align = 'rb',gradient = 'y' #添加图例
+ )
太复杂, 需得好好研究
#交互地图
require(leafletCN)
aqi$aqi[aqi$aqi == -1] <- NA
pvs <- regionNames('china')
loc <- match(pvs, aqi$province)
aqi2 <- data.frame(name = pvs, value = aqi$aqi[loc])
#交互最后一击
geojsonMap(dat = aqi2,mapName = 'china',
popup = paste(aqi2$name, aqi2$value),
legendTitle = 'AQI')