根据值通过R中的ggmap生成空间热图 [英] Generating spatial heat map via ggmap in R based on a value

查看:95
本文介绍了根据值通过R中的ggmap生成空间热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用以下数据点生成一个Choropleth映射:




  • 经度

  • 纬度

  • 价格



这是数据集-



这是我的代码:

 库(ggmap)

地图<-get_map (位置=奥斯汀,缩放= 9)
数据<-read.csv(file.choose(),stringsAsFactors = FALSE)
数据$ average_rate_per_night<-as.numeric(gsub( [\\ $,],,
data $ average_rate_per_night))
ggmap(地图,范围=设备)+
stat_contour(data = data,geom =多边形,
aes(x =经度,y =纬度,z = average_rate_per_night,
fill = ..level ..))+
scale_fill_continuous(name = Price,low =黄色,高=红色)

我收到以下错误消息:

  2:在stat_contour()中计算失败:
等高线在每个co处需要单个`z` x和y的绑定。

我非常感谢任何有关如何解决此问题的帮助或生成此类型的任何其他方法的热图。请注意,我对价格的权重感兴趣,而不对记录的密度感兴趣。

解决方案

如果您坚持使用轮廓线方法,则需要为数据中每种可能的x,y坐标组合提供一个值。为此,我强烈建议对空间进行网格化,并为每个容器生成一些摘要统计信息。



我根据您提供的数据在下面附上一个工作示例:

  library(ggmap)
库(data.table)

map<-get_map(location = austin,zoom = 12)
data<-setDT( read.csv(file.choose(),stringsAsFactors = FALSE))

#将汇率从字符串转换为数字
data [,average_rate_per_night:= as.numeric(gsub(, ,,
substr(average_rate_per_night,2,nchar(average_rate_per_night))))]

#生成x,y坐标的bins
xbreaks<-seq(floor (min(data $ latitude)),ceiling(max(data $ latitude)),by = 0.01)
ybreaks<-seq(floor(min(data $ longitude))),ceiling(max(data $ longitude) )),按= 0.01)

#将数据点分配到垃圾箱中
data $ latbin<-xbreaks [cut(data $ latitude,breaks = xbreaks,labels = F)]
data $ longbin<-ybreaks [cut(data $ longitude,breaks = ybreaks,labels = F)]

#汇总每个bin的数据
datamat <-数据[, list(average_rate_per_night =平均值(average_rate_per_night)),
by = c( latbin, longbin)]

#将汇总数据与所有可能的x,y坐标组合合并以获得
#每个bin的值
datamat<-merge(setDT(expand.grid(latbin = xbreaks,longbin = ybreaks)),datamat,
by = c( latbin, longbin),all.x = TRUE,all.y = FALSE)

#填满空箱0以平滑轮廓图
datamat [is.na(average_rate_per_night), ] $ average_rate_per_night<-0

#绘制轮廓
ggmap(map,range = device)+
stat_contour(data = datamat,aes(x = longbin, y = latbin,z = average_rate_per_night,
fill = ..level ..,alpha = ..level ..),geom ='polygon',binwidth = 100)+
scale_fill_gradient(name = Price ,低=绿色,高=红色)+
指南(alpha = FALSE)



然后您可以使用bin大小和轮廓线 binwidth 来获得所需的结果,但是您还可以在网格上应用平滑功能以获得更平滑的轮廓线图。


I'd like to generate a choropleth map using the following data points:

  • Longitude
  • Latitude
  • Price

Here is the dataset - https://www.dropbox.com/s/0s05cl34bko7ggm/sample_data.csv?dl=0.

I would like the map to show the areas where the price is higher and the where price is lower. It should most probably look like this (sample image):

Here is my code:

library(ggmap)

map <- get_map(location = "austin", zoom = 9)
data <- read.csv(file.choose(), stringsAsFactors = FALSE)
data$average_rate_per_night <- as.numeric(gsub("[\\$,]", "", 
data$average_rate_per_night))
ggmap(map, extent = "device") + 
stat_contour( data = data, geom="polygon", 
            aes( x = longitude, y = latitude, z = average_rate_per_night, 
fill = ..level.. ) ) +
scale_fill_continuous( name = "Price", low = "yellow", high = "red" )

I'm getting the following error message:

2: Computation failed in `stat_contour()`:
Contour requires single `z` at each combination of `x` and `y`. 

I'd really appreciate any help on how this can be fixed or any other method to generate this type of heatmap. Please note that I'm interested in the weight of the price, not density of the records.

解决方案

If you insist on using the contour approach then you need to provide a value for every possible x,y coordinate combination you have in your data. To achieve this I would highly recommend to grid the space and generate some summary statistics per bin.

I attach a working example below based on the data you provided:

library(ggmap)
library(data.table)

map <- get_map(location = "austin", zoom = 12)
data <- setDT(read.csv(file.choose(), stringsAsFactors = FALSE))

# convert the rate from string into numbers
data[, average_rate_per_night := as.numeric(gsub(",", "", 
       substr(average_rate_per_night, 2, nchar(average_rate_per_night))))]

# generate bins for the x, y coordinates
xbreaks <- seq(floor(min(data$latitude)), ceiling(max(data$latitude)), by = 0.01)
ybreaks <- seq(floor(min(data$longitude)), ceiling(max(data$longitude)), by = 0.01)

# allocate the data points into the bins
data$latbin <- xbreaks[cut(data$latitude, breaks = xbreaks, labels=F)]
data$longbin <- ybreaks[cut(data$longitude, breaks = ybreaks, labels=F)]

# Summarise the data for each bin
datamat <- data[, list(average_rate_per_night = mean(average_rate_per_night)), 
                 by = c("latbin", "longbin")]

# Merge the summarised data with all possible x, y coordinate combinations to get 
# a value for every bin
datamat <- merge(setDT(expand.grid(latbin = xbreaks, longbin = ybreaks)), datamat, 
                 by = c("latbin", "longbin"), all.x = TRUE, all.y = FALSE)

# Fill up the empty bins 0 to smooth the contour plot
datamat[is.na(average_rate_per_night), ]$average_rate_per_night <- 0

# Plot the contours
ggmap(map, extent = "device") +
  stat_contour(data = datamat, aes(x = longbin, y = latbin, z = average_rate_per_night, 
               fill = ..level.., alpha = ..level..), geom = 'polygon', binwidth = 100) +
  scale_fill_gradient(name = "Price", low = "green", high = "red") +
  guides(alpha = FALSE)

You can then play around with the bin size and the contour binwidth to get the desired result but you could additionally apply a smoothing function on the grid to get an even smoother contour plot.

这篇关于根据值通过R中的ggmap生成空间热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆