为了实现我对电动汽车充电需求的预测模型的可视化,我想创建显示多个城市充电需求热点的热图。我想使用两种不同类型的热图 - 轮廓热图和密度热图以六边形分布在地图上(我将数据分成六边形单元进行预测)。
不幸的是,我只设法创建了具有计数但不是实际(充电需求)值的热图。我尝试吐痰/切割数据,确保只有观察>使用0并尝试限制x / y轴 - 到目前为止没有任何工作。我希望这里有人对如何做到这一点有一个很好的想法。下面是一段样本数据和两个热图代码,它们现在产生计数热图而不是值热图(lon / lat是我的六边形单元的质心,数据被聚合到其中):
+---+------------+-------------+----------------+
| | 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 |
+---+------------+-------------+----------------+
快速复制:
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))
六角形热图(计数)示例:
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"))
轮廓热图(计数)示例:
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)
将数据切成1000件也无济于事:
df.data$demand_cut <- cut(df.data$charging_demand, breaks = 1000)