For the visualization of my predictive models on electric vehicle charging demand I would like to create heatmaps showing the hotspots of charging demand across multiple cities. I would like to use two different types of heatmaps - contour heatmaps and density heatmaps spread over the map in hexagons (I split my data into hexagonal cells for prediction).
Unfortunately I only managed to create heatmaps with counts but not the actual (charging demand) values. I tried spitting/cutting the data, made sure that only observations > 0 were used and tried to limit the x/y axis - nothing has worked so far. I hope someone here has a nifty idea on how to do this. Below a piece of sample data and both heatmap codes that now produce count-heatmaps instead of value-heatmaps (lon/lat are the centroids of my hexagonal cells into which the data is aggregated):
+---+------------+-------------+----------------+
| | lon | lat |charging_demand |
+---+------------+-------------+----------------+
| 1 | 12.65783 | 55.58421 | 653.7 |
| 2 | 12.67671 | 55.59255 | 24.1 |
| 3 | 12.66813 | 55.59422 | 376.2 |
+---+------------+-------------+----------------+
For quick replication:
df.data <- data.frame(lon = c(12.64753, 12.64924, 12.55998, 12.51878, 12.52221, 12.58401, 12.55655, 12.57371, 12.59088, 12.58058),
lat = c(55.63091, 55.62924, 55.67594, 55.78267, 55.78601, 55.68261, 55.66593, 55.67927, 55.67927, 55.70596),
charging_demand = c(663.7, 627.4, 597.4, 14.5, 396.3, 300.0, 0.00, 256.9, 228.5, 0.2))
Hexagonal heatmap (count) example:
map <- get_map(location = "Copenhagen", zoom = 12)
ggmap(map) +
coord_cartesian() +
geom_hex(data = df.data, aes(x = lon, y = lat, colour = charging_demand),
alpha = 0.6, color = "black", show.legend = TRUE, stat = "binhex") +
guides(fill = FALSE, alpha = FALSE) +
scale_fill_gradientn (colours = c("green","red"))
Contour heatmap (count) example:
ggmap(map, extent = "device") +
geom_density2d(data = df.data, aes(x = lon, y = lat), size = 0.3) +
stat_density2d(data = df.data, aes(x = lon, y = lat, fill = ..level.., alpha = ..level..),
size = 0.01, bins = 10, geom = "polygon") +
scale_fill_gradient(low = "green", high = "red") +
scale_alpha(range = c(0, 0.3), guide = FALSE)
Cutting the data even into 1000 pieces didn't help either:
df.data$demand_cut <- cut(df.data$charging_demand, breaks = 1000)