在做Airbnb 北京数据集分析的课题,请问如何用R来绘制北京地图,并且用热力的形式显示房租均价高低,例如下图,谢谢
数据集下载地址:https://www.kaggle.com/zihanli1/beijing-airbnb-dataclean
在做Airbnb 北京数据集分析的课题,请问如何用R来绘制北京地图,并且用热力的形式显示房租均价高低,例如下图,谢谢
数据集下载地址:https://www.kaggle.com/zihanli1/beijing-airbnb-dataclean
原始数据比较混乱,我用正则表达式在sublime里修正了一下原始数据,代码中没有展示,r代码如下。由于没有找到更细致的城区shp图像,所以将主城区的数据进行了合并。其余城区保持不变。共11个城区。
library("sp")
library("rgdal")
library("broom")
library("plyr")
# 读取数据
china_map_adm2 <- readOGR("gadm36_CHN_shp/gadm36_CHN_3.shp",use_iconv = TRUE, encoding="UTF-8")
beijing_map <- subset(china_map_adm2,NAME_1=="Beijing")
beijing_map@data$id <- rownames(beijing_map@data)
beijingdata <- beijing_map@data
#将空间数据转换为数据框
beijingmapdata <- broom::tidy(beijing_map)
data = read.csv2('Beijing Airbnb Data(clean)/listings_clean.csv', sep=',', fileEncoding = 'utf-8')
dict = c("朝阳区"='Beijing',
"东城区"="Beijing",
"西城区"="Beijing",
"海淀区"='Beijing',
"石景山区"="Beijing",
"丰台区"="Beijing",
"昌平区"='Changping',
"大兴区"="Daxing",
"房山区"="Fangshan",
"怀柔区"='Huairou',
"门头沟区"="Mentougou",
"密云县"="Miyun",
"平谷区"='Pinggu',
"顺义区"="Shunyi",
"通州区"="Tongzhou",
"延庆县"='Yanqing'
)
data$neighbourhood=dict[data$neighbourhood]
beijing.neighbour = c("Beijing","Changping","Daxing","Fangshan","Huairou","Mentougou","Miyun","Pinggu","Shunyi","Tongzhou","Yanqing" )
i = 0
average = c()
for(i in 1:11){
each.neighbour = beijing.neighbour[i]
each.sum = sum(data[data$neighbourhood==each.neighbour,'price'])
each.mean = mean(data[data$neighbourhood==each.neighbour,'price'])
average = c(average, each.mean)
cat(each.neighbour, each.mean, '\n')
}
beijingdata$average<-average
#将其与几何映射层进行合并:
beijingdatanewmapdata<-merge(beijingmapdata[,c(-4,-5)],beijingdata[,c("id","GID_2","NL_NAME_2","average")],by.x="id",by.y="id")
# 开始作图
library("ggplot2")
library("ggthemes")
library("maps")
library("mapproj")
ggplot()+ geom_polygon(data=beijingdatanewmapdata,aes(x=long,y=lat,group=group,fill=average),col="grey95")+
scale_fill_gradient(low="white",high="steelblue") +
coord_map("polyconic") +
theme_map()
#添加城市名标签
midpos <- function(x) mean(range(x,na.rm=TRUE))
centres <- ddply(beijingdatanewmapdata,.(NL_NAME_2),colwise(midpos,.(long,lat)))
ggplot(beijingdatanewmapdata,aes(long,lat)) +
geom_polygon(aes(group=group,fill=average),colour="black") +
scale_fill_gradient(low="white",high="steelblue") +
geom_text(aes(long, lat, label=""),data=centres) +
theme(
panel.grid = element_blank(),
panel.background = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.title = element_blank()
)
修正后的数据记得私信找我要。