Heatmaps generation with R, Php and Mongodb

2019-01-29 13:28发布

问题:

I'm new to R. i want to generate a heat map using PHP, MongoDB and R. i want to plot geo coordinates(Lat, Lng) on World Map. Below is the sample code i'm trying to use. please let me know how to plot 4 million of lat and lng points on static map.

# loading the required packages
require(ggplot2)
require(ggmap)

# creating a sample data.frame with your lat/lon points
lon <- c(78.381270, 78.136352, 77.179950)
lat <- c(17.440229, 10.104529, 28.680417)
df <- as.data.frame(cbind(lon,lat))

# getting the map
mapgilbert <- get_map(location = c(lon = mean(df$lon), lat = mean(df$lat)), zoom = 4,
                      maptype = "roadmap", scale = 2)

# plotting the map with some points on it
ggmap(mapgilbert) +
  geom_point(data = df, aes(x = lon, y = lat, fill = "red", alpha = 0.8), size = 3, shape = 21) +
  guides(fill=FALSE, alpha=FALSE, size=FALSE)

Please provide me the appropriate solution. Thanks In advance.

回答1:

ggmap(mapgilbert) + 
  stat_density2d(data = df, aes(x = lon, y = lat, fill = ..level.., 
                                alpha = ..level..), size = 0.01, 
                                bins = 16, geom = "polygon") + 
  scale_fill_gradient(low = "green", high = "red") + 
  scale_alpha(range = c(0, 0.3), guide = FALSE)

I think 4 million points are too computationally expensive. Maybe you should use a subset or sample from your data.