data visualization - Generating spatial heat map via ggmap in R based on a value -


i'd generate choropleth map using following data points:

  • longitude
  • latitude
  • price

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

i map show areas price higher , price lower. should (sample image):

enter image description here

here 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 following error message:

2: computation failed in `stat_contour()`: contour requires single `z` @ each combination of `x` , `y`.  

i'd appreciate on how can fixed or other method generate type of heatmap. please note i'm interested in weight of price, not density of records.

if insist on using contour approach need provide value every possible x,y coordinate combination have in data. achieve highly recommend grid space , generate summary statistics per bin.

i attach working example below based on data provided:

library(ggmap) library(data.table)  map <- get_map(location = "austin", zoom = 12) data <- setdt(read.csv(file.choose(), stringsasfactors = false))  # convert rate string numbers data[, average_rate_per_night := as.numeric(gsub(",", "",         substr(average_rate_per_night, 2, nchar(average_rate_per_night))))]  # generate bins x, y coordinates xbreaks <- seq(floor(min(data$latitude)), ceiling(max(data$latitude)), = 0.01) ybreaks <- seq(floor(min(data$longitude)), ceiling(max(data$longitude)), = 0.01)  # allocate data points bins data$latbin <- xbreaks[cut(data$latitude, breaks = xbreaks, labels=f)] data$longbin <- ybreaks[cut(data$longitude, breaks = ybreaks, labels=f)]  # summarise data each bin datamat <- data[, list(average_rate_per_night = mean(average_rate_per_night)),                   = c("latbin", "longbin")]  # merge summarised data possible x, y coordinate combinations  # value every bin datamat <- merge(setdt(expand.grid(latbin = xbreaks, longbin = ybreaks)), datamat,                   = c("latbin", "longbin"), all.x = true, all.y = false)  # fill empty bins 0 smooth contour plot datamat[is.na(average_rate_per_night), ]$average_rate_per_night <- 0  # plot 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) 

enter image description here

you can play around bin size , contour binwidth desired result additionally apply smoothing function on grid smoother contour plot.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -