前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R语言可视化——数据地图离散百分比填充(环渤海)

R语言可视化——数据地图离散百分比填充(环渤海)

作者头像
数据小磨坊
发布2018-04-11 14:42:13
9230
发布2018-04-11 14:42:13
举报
文章被收录于专栏:数据小魔方数据小魔方

今天跟大家分享如何以百分比形式填充离散分段数据地图。

案例用环渤海三省二市的地理数据。

library(ggplot2)

library(maptools)

library(plyr)

数据导入、转换、抽取

CHN_adm2 <- readShapePoly("c:/rstudy/CHN_adm/CHN_adm2.shp")

CHN_adm2_1 <- fortify(CHN_adm2)

data1 <- CHN_adm2@data

data2 <- data.frame(id=row.names(data1),data1)

china_map_data <- join(CHN_adm2_1, data2, type = "full")

huanbohai <-subset(china_map_data,NAME_1==c("Beijing","Tianjin","Nei Mongol","Hebei","Shandong"))

建立业务数据:

huanbohai_perm<-data.frame(NAME_2=unique(huanbohai$NAME_2),zhibiao=rnorm(42,100,50))

huanbohai_perm$zhibiao<-round(huanbohai_perm$zhibiao,0)

write.table (huanbohai_perm, file ="C:/rstudy/huanbohai.csv", sep =",", row.names =FALSE)

业务数据导入及合并:

mydata<-read.csv("C:/rstudy/huanbohai.csv",header=T)

huanbohai_map_data <- join(huanbohai,mydata, type="full")

###将转换的分段因子变量重新命名为我们需要的分段阀值:

huanbohai_map_data$fau <- cut(huanbohai_map_data$zhibiao, breaks = c(0,40,80,120,160,200))

huanbohai_map_data$fam<-factor(huanbohai_map_data$fau,levels=c('(0,40]','(40,80]','(80,120]','(120,160]','(160,200]'),labels=c('0~40','40~80','80~120','120~160','160~200'),order=TRUE)

离散颜色标度填充(实际值分段)

windowsFonts(myFont = windowsFont("微软雅黑"))

ggplot(huanbohai_map_data, aes(x = long, y = lat, group = group,fill =fam)) +

geom_polygon(colour="white")+

scale_fill_brewer(palette="Greens") + ###Blues&Greens

coord_map("polyconic") +

ggtitle("某公司2015~2016年度营业状况分布图")+

guides(fill=guide_legend(reverse=TRUE,title=NULL))+

theme(

title=element_text(family="myFont"),

legend.text.align=1, ###图例标签右对齐

panel.grid = element_blank(),

panel.background = element_blank(),

axis.text = element_blank(),

axis.ticks = element_blank(),

axis.title = element_blank(),

legend.position = c(0.08,0.6)

)

以上是昨天在东三省填充地图中所使用过的方法,接下来我解释一种新的填充方案,通过将数量段转换为百分比进行离散颜色标度填充:

离散颜色标度分割(百分比数量段):

qa <- quantile(na.omit(huanbohai_map_data$zhibiao), c(0,0.2,0.4,0.6,0.8,1.0))

huanbohai_map_data$zhibiao_q<-cut(huanbohai_map_data$zhibiao,qa,labels = c("0-20%", "20-40%","40-60%","60-80%", "80-100%"),include.lowest = TRUE)

> levels(huanbohai_map_data$zhibiao_q)

[1] "0-20%" "20-40%" "40-60%" "60-80%" "80-100%"

通过将指标变量以分段百分比的方式进行分割,新建立一个百分比分段因子变量:

离散渐变(百分比)

windowsFonts(myFont = windowsFont("微软雅黑"))

ggplot(huanbohai_map_data,aes(long,lat))+

geom_polygon(aes(group=group,fill=zhibiao_q),colour="white")+

scale_fill_brewer(palette="Greens")+

coord_map("polyconic") +

guides(fill=guide_legend(reverse=TRUE,title=NULL))+

ggtitle("某公司2015~2016年度营业状况分布图")+

theme(

title=element_text(family="myFont"),

panel.grid = element_blank(),

panel.background = element_blank(),

axis.text = element_blank(),

axis.ticks = element_blank(),

axis.title = element_blank(),

legend.position =c(0.2,0.7),

legend.text.align=1

)

###带标签

接下来处理标签问题:

创建各城市中心地理坐标:

midpos <- function(data1) mean(range(data1,na.rm=TRUE))

centres <- ddply(huanbohai_map_data,.(city),colwise(midpos,.(long,lat)))

填充并添加标签:

ggplot(huanbohai_map_data,aes(long,lat))+

geom_polygon(aes(group=group,fill=zhibiao_q),colour="white")+

scale_fill_brewer(palette="Greens")+

coord_map("polyconic") +

geom_text(aes(label=city),size =3,family="myFont",fontface="plain",data=centres) +

guides(fill=guide_legend(reverse=TRUE,title=NULL))+

ggtitle("某公司2015~2016年度营业状况分布图")+

theme(

title=element_text(family="myFont"),

title=element_text(family="myFont"),

panel.grid = element_blank(),

panel.background = element_blank(),

legend.text.align=1,

axis.text = element_blank(),

axis.ticks = element_blank(),

axis.title = element_blank(),

legend.position = c(0.2,0.7)

)

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2016-10-12,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 数据小魔方 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档