YOUZIXIDA 2021-02-16 10:38 采纳率: 100%
浏览 221
已采纳

用R分析北京airbnb数据集 用R绘制北京地图

在做Airbnb 北京数据集分析的课题,请问如何用R来绘制北京地图,并且用热力的形式显示房租均价高低,例如下图,谢谢

数据集下载地址:https://www.kaggle.com/zihanli1/beijing-airbnb-dataclean

  • 写回答

3条回答 默认 最新

  • ProfSnail 2021-02-17 11:27
    关注

    原始数据比较混乱,我用正则表达式在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()
      )
    
    

     

     

    修正后的数据记得私信找我要。 

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 关于#硬件工程#的问题:求一下解题思路
  • ¥15 运筹学对偶单纯行法构造扩充问题
  • ¥20 XP系统的老电脑一开机就提示找不到rundll.exe,付费求解
  • ¥15 milvus查询出来的score怎么转换成0-1之间的相似性
  • ¥15 多ip服务器站群如何搭建l2tp服务器
  • ¥15 lvgl V9移植到linux开发板
  • ¥15 VB.net中在窗体中创建一个button控件来关闭窗体,但是提示错误,我该怎么办
  • ¥15 网上下载好的程序但是arduinoIDE编程报错,运行不了,哪里出错了,能具体给改一下吗
  • ¥15 Sharepoint JS开发 付费技术指导
  • ¥15 输入程序运行仿真后,烟雾值不实时检测,变成固定值