查看原文
其他

R可视化分析链家网南京楼市数据

2017-11-10 邬书豪 R语言中文社区


作者:邬书豪,车联网数据挖掘工程师 ,R语言中文社区负责人之一。微信ID:wsh137552775

知乎专栏:https://www.zhihu.com/people/wu-shu-hao-67/activities


数据下载:公众号后台回复“链家网”下载数据

一、10个数据分析维度

  1. 各一级区域成交量排行和销售均价情况

  2. 热门成交房屋户型绘饼图分析

  3. 链家南京房屋年代统计分析

  4. 链家南京房屋交易议价空间统计(议价空间 = 挂牌价 - 成交价)

  5. 链家南京楼层房价统计

  6. 最贵的top10小区

  7. 链家每日销售额

  8. 平均成交总价与平均成交单价

  9. 房屋成交周期

  10. 报价和成交价差异

二、数据处理及可视化分析

#加载数据 data = read.csv('D:/练习集/南京链家楼市分析/dpLianjiabydistrict.csv',header = TRUE,fileEncoding = "gbk") dim(data) #数据共有30515行,24列 #看一下数据 str(data) #1、链家南京每日成交量统计可视化 daySaleAmount <- with(data, table(dealDate)) barplot(daySaleAmount, xlab="交易时间", ylab="成交量(套)", main="链家南京成交量统计(2012.5.3-2017.7.6)"
  • 从2016年12月开始至2017年2月末,南京楼市迎来买买买的高潮!成交量爆增

  • 2012.5.3-2017.7.6近五年,南京楼市成交量在2016年末开始迎来买房高峰

#2、一级区域成交量排行和销售均价 #一级区域观测赋值给增加的新变量zone data$district <-as.character(data$district) zonedata = function(x){   strsplit(x,split = "/")[[1]][1] } zone1 <-sapply(data$district,zonedata) data$zone <- zone1 data$zone <- factor(data$zone) zoneSales <- with(data, table(zone)) zoneSales <- sort(zoneSales, decreasing=TRUE) #一级区域成交量数据表 (图2) zoneSales=as.data.frame(zoneSales)
  • 鼓楼区成交量最大,以8350套成交套数雄居第一!

  • 秦淮区以6388套成交套数位于NO2!

#链家南京各区域交易情况绘图! # 整合数据,计算出各区域均价 tempPrices <- aggregate(data$unitPrice, by=list(zone=data$zone), mean) zoneUnitPrices <- NULL #   a=NULL a[c("will","night")] = c(1,2) a zoneUnitPrices[t(tempPrices["zone"])] <- t(tempPrices["x"]) #zoneUnitPrices["建邺"] zoneUnitPrices[2] <- round(zoneUnitPrices[2], 0) ry = as.data.frame(zoneUnitPrices)$zoneUnitPrices ly = zoneSales$Freq twoord.plot(lx=c(1:9), ly=ly, lylim=c(0, max(zoneSales$Freq)*1.1),            rylim=c(0, max(ry)*1.1), main="链家南京各区域交易情况",            xlab="区域", ylab="成交量(套)", rylab="均价(元)",            rx=c(1:9), ry=ry,            type=c("bar", "b"), xlim=c(0, 10)) ##加载均价数据 text(c(1:9), zoneSales$Freq+300, zoneSales$Freq) rypos <- round(zoneUnitPrices[zoneSales$zone]*max(zoneSales$Freq)/max(zoneUnitPrices)) text(c(1:9), rypos+600, round(zoneUnitPrices[zoneSales$zone]), col="red")
图3
#链家南京热门成交户型Top6 layoutSaleAmount <- with(data, table(layout)) layoutSaleAmount <- sort(layoutSaleAmount, decreasing=TRUE) topLayoutSaleAmount <- layoutSaleAmount[1:6] topLayoutSaleAmount[7] <- sum(layoutSaleAmount)-sum(layoutSaleAmount[1:6]) names(topLayoutSaleAmount)[7] <- "其他" pctTopLayoutSaleAmount <- round(topLayoutSaleAmount/sum(topLayoutSaleAmount)*100,2) pctTopLayoutSaleAmount <- paste(names(topLayoutSaleAmount), ":", pctTopLayoutSaleAmount, "%", sep="") par(mfrow=c(1,1)) pie(topLayoutSaleAmount, labels= pctTopLayoutSaleAmount, col=rainbow(length(topLayoutSaleAmount)),main="链家 48 30904 48 14987 0 0 2318 0 0:00:13 0:00:06 0:00:07 3218京热门成交户型Top6(2012.5.3-2017.7.6)")#链家南京房屋年代统计 data$years <- as.character(data$years) data$years <- as.numeric(data$years) data <- within(data, { hood <- NA hood[is.na(data$years)] <- NA hood[data$years<1990] <- "90年前" hood[data$years>=1990 & data$years<2000] <- "90年代" hood[data$years>=2000 & data$years<2010] <- "00年代" hood[data$years>=2010] <- "10年代" }) data$hood <- ordered(data$hood, levels=c("10年代", "00年代", "90年代", "90年前")) hoodSales <- with(data, table(hood)) barplot(hoodSales, xlab="房屋年代", ylab="成交量(套)", ylim=c(0, max(hoodSales)*1.1), main="链家南京房屋年代统计(2012.5.3-2017.7.6)") text(c(1:length(hoodSales))*1.2-0.48, hoodSales+400, hoodSales)#5、议价空间 = 挂牌价 - 成交价 gapPrices <- data$quotedPrice - data$totalPrice dayGapPrices <- aggregate(gapPrices, by=list(dealDate=data$dealDate), mean) tsGapPrices=ts(dayGapPrices[2],frequency=365,start=c(2012,5,3),end=c(2017,7,6)) par(mfrow=c(2,1), mar=c(2,4,3,2)) plot.ts(tsGapPrices, type="p", xlab=NULL, xaxt="n", ylab=NULL, main="链家南京房屋交易议价空间统计(2012.5.3-2017.7.6)") decTsGapPrices <- decompose(tsGapPrices) par(mar=c(3,4,0,2))#楼层房价统计 data$loft <- as.character(data$loft) data$loft <- as.numeric(data$loft) # 无电梯房 elderdata <- data[data$elevator=="无电梯" | (is.na(data$elevator) & data$loft <= 7),] # 有电梯房 newerdata <- data[!(data$elevator=="无电梯" | (is.na(data$elevator) & data$loft <= 7)),] # 根据stair计算交易均价 stairElderPrices <- aggregate(elderdata$unitPrice, by=list(stair=elderdata$stair), mean) stairNewerPrices <- aggregate(newerdata$unitPrice, by=list(stair=newerdata$stair), mean) stairElderPrices[,2] <- round(stairElderPrices[,2], 0) stairNewerPrices[,2] <- round(stairNewerPrices[,2], 0) maxPrice <- max(stairElderPrices[,2],stairNewerPrices[,2]) plot(stairElderPrices[,1], stairElderPrices[,2], ylim=c(0, maxPrice*1.1), main="链家南京楼层房价统计", type="b", col="blue", xlab="楼层", ylab="交易均价(元)") text(stairElderPrices[,1], stairElderPrices[,2]+1000, stairElderPrices[,2], col="blue") par(new=TRUE) plot(stairElderPrices[,1], stairNewerPrices[,2], ylim=c(0, maxPrice*1.1), type="b", col="red", xlab="楼层", ylab="交易均价(元)") text(stairElderPrices[,1], stairNewerPrices[,2]+1000, stairNewerPrices[,2], col="red") legend("bottomright", legend=c("无电梯","有电梯"), col=c("blue", "red"), bty="y", bg="aliceblue", pch=c(15,15)) par(new=FALSE)
链家南京楼层房价统计
#7、最贵的top10小区 blockPrices <- aggregate(data$unitPrice, by=list(block=data$block), mean) blockPrices <- blockPrices[order(blockPrices[,2], decreasing=TRUE),] names(blockPrices)[2] <- "unitPrice" # 小区总数 length(blockPrices[,1]) # 最贵Top10小区 blockPrices[1:10,]# 链家每日销售额 daySales <- aggregate(data$totalPrice, by=list(dealDate=data$dealDate), sum) plot(daySales[,1], daySales[,2], type='b', xlab="Deal Date", ylab="Sale Value", main="Lianjia Day Sale Value(2017.3.15-2017.7.3)")

链家每日销售额

#平均成交总价 mean(data$totalPrice) #平均成交单价 mean(data$unitPrice)# 房屋成交周期 tranCycle <- aggregate(data$tranCycle, by=list(dealDate=data$dealDate), mean) plot(tranCycle[,1], tranCycle[,2], type='b', xlab="Deal Date", ylab="Transaction Cycle", main="Lianjia Transaction Cycle(2017.3.15-2017.7.3)"
房屋成交周期
# 报价和成交价差异 gapPrice <- data$quotedPrice - data$totalPrice gap <- aggregate(gapPrice, by=list(dealDate=data$dealDate), mean) plot(gap[,1], gap[,2], type='b', xlab="Deal Date", ylab="Gap Price", main="Lianjia Gap Price(2017.3.15-2017.7.3)")
报价和成交价差异

文末彩蛋

Hadley 大神的个人主页:http://hadley.nz/ 

《R for Data Science》 使用R做数据科学的核心工具:http://r4ds.had.co.nz/

《ggplot2:elegant graphics for data analysis》使用ggplot2创建图形:https://www.amazon.com/dp/0387981403/ref=cm_sw_su_dp?tag=ggplot2-20

《Advanced R》 R的运行机制:http://adv-r.had.co.nz/

《R packages》 R的软件工程实践,并使用包打包、记录和测试代码:http://r-pkgs.had.co.nz/


公众号后台回复关键字即可学习

回复 R              R语言快速入门免费视频 
回复 统计          统计方法及其在R中的实现
回复 用户画像   民生银行客户画像搭建与应用 
回复 大数据      大数据系列免费视频教程
回复 可视化      利用R语言做数据可视化
回复 数据挖掘   数据挖掘算法原理解释与应用
回复 机器学习   R&Python机器学习入门 

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存