gpt4 book ai didi

r - ggmap R 中带有数量的热图

转载 作者:行者123 更新时间:2023-12-02 04:50:44 24 4
gpt4 key购买 nike

我想绘制一个热图,但有数量,而不是我在教程 (here) 中看到的单一输入。我也看过 this 示例,但它本身并不是热图。您知道如何在热图中整合每个位置的数量吗?我不知道应该把它放在哪里。

我的代码是:

#Get the Massachusetts map
ma_map <- get_map(location = "Massachusetts", maptype = "terrain", zoom = 8)

#Create the map with the companies
ggmap(ma_map, extent = "device") + geom_point(aes(x = lon, y = lat), size = (heatdata$Quantity),
colour = "red", alpha = 0.5, data = heatdata)


#Create heat map
ggmap(ma_map, extent = "device") + geom_density2d(data = heatdata,
aes(x = lon, y = lat), size = 0.3) + stat_density2d(data = heatdata,
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)

我的数据集是:

structure(list(Quantity = c(1L, 3L, 2L, 1L, 2L, 1L, 3L, 11L, 
1L, 1L, 6L, 2L, 2L, 1L, 1L, 1L, 2L, 9L, 7L, 1L, 1L, 6L, 1L, 1L,
3L, 1L, 2L, 3L, 1L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 3L,
4L, 1L, 1L, 3L, 2L, 1L, 2L, 3L, 3L, 6L, 1L, 3L, 1L, 8L, 1L, 1L,
2L, 2L, 1L, 4L, 1L, 1L, 2L, 3L, 1L, 1L, 3L, 1L, 1L, 1L, 2L, 4L,
1L, 5L, 1L, 5L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 7L, 1L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L, 5L, 1L, 1L, 2L, 1L, 1L,
1L, 1L, 2L, 2L, 3L, 1L, 1L, 5L, 16L, 2L), lon = c(-72.6148117,
-71.1367953, -71.1564729, -71.4633956, -71.8356271, -71.2471097,
-71.5899054, -71.2760089, -71.4745881, -70.880049, -71.2689461,
-71.0588801, -71.5167139, -71.0758905, -70.9750541, -71.0183787,
-71.1211635, -71.1956205, -71.1097335, -71.1447732, -71.970074,
-71.3672838, -71.166187, -72.6078672, -71.6829081, -70.8036544,
-71.3489484, -70.932122, -71.1676536, -71.615556, -72.5125863,
-71.1550451, -71.8022955, -71.396725, -71.998133, -70.6619888,
-73.362008, -72.5884222, -70.9744964, -70.889759, -71.4245049,
-72.6162009, -71.5411747, -71.5225646, -71.566139, -70.2881124,
-71.1631137, -71.2289641, -71.5128022, -71.3161718, -71.066163,
-71.2190578, -70.8578024, -71.5522874, -71.4495058, -71.3967256,
-71.1908924, -71.5163049, -71.761522, -71.35, -71.2377548, -70.934205,
-70.8772751, -71.2092214, -71.2205981, -71.3250563, -71.1350575,
-71.2845035, -71.383617, -71.6969985, -71.1869963, -71.1989695,
-71.8647577, -70.9286609, -70.8092, -73.2453824, -70.6672621,
-70.616142, -71.042551, -70.9161551, -70.8967155, -71.6495176,
-71.7128471, -72.7300929, -71.5245087, -72.0333905, -72.589811,
-71.7606293, -71.0999719, -71.102778, -72.0787059, -70.9175562,
-71.2342248, -72.3356375, -70.9495053, -71.4244224, -71.0723391,
-71.2495096, -71.2356113, -71.1036688, -70.7197342, -71.2923877,
-71.1359393, -71.2250533, -72.6203675, -70.7603143, -71.6161294,
-72.749538, -71.4378411, -71.2244987, -71.6661783, -71.1724467,
-71.1522765, -71.8022934), lat = c(42.0695391, 42.6583356, 42.4153925,
42.2612067, 42.1945385, 42.3479782, 42.5611947, 42.4906231, 42.0867608,
42.5584283, 42.5584218, 42.3600825, 42.4834197, 42.7695348, 41.9903519,
42.0834335, 42.3317642, 42.5047161, 42.3736158, 42.1584324, 42.13565,
42.5998139, 42.3303798, 42.1487043, 42.4167635, 42.2417675, 42.4603719,
42.5750009, 42.2436085, 42.537222, 42.0645396, 41.7014912, 42.5834228,
42.0834313, 42.5750883, 42.6159285, 42.1959798, 42.3417565, 42.5862053,
42.2418172, 42.2000966, 42.2042586, 42.1306522, 42.2286954, 42.391736,
41.6524911, 42.7070354, 42.4430372, 42.5372893, 42.6334247, 42.4250964,
42.0334565, 42.4999582, 42.3459271, 42.4334903, 42.1417641, 42.7262016,
42.1398577, 42.1920719, 42.283333, 42.2809285, 41.6362152, 42.8125913,
42.3370413, 42.3121798, 42.1195426, 42.6987024, 42.5834258, 42.6396055,
42.2356407, 41.9667703, 42.1943909, 42.1167606, 42.5278731, 42.0714925,
42.4500845, 41.9584457, 41.6862173, 42.1619739, 42.1306563, 42.51954,
42.5437035, 42.2959267, 42.2292572, 42.3056501, 42.0751065, 42.1014831,
42.4374156, 42.4802469, 42.125, 42.1084122, 42.4709437, 42.6106478,
42.1875933, 42.6375941, 42.6766696, 42.5039395, 42.1417442, 42.3764852,
42.7567572, 41.7614511, 42.296797, 42.4249311, 42.3497948, 42.1070383,
41.7898254, 42.2695216, 42.1250929, 42.5792583, 42.2139873, 42.1112075,
42.5481714, 42.4792618, 42.2625932)), .Names = c("Quantity",
"lon", "lat"), row.names = c(NA, -114L), class = "data.frame")

最佳答案

不确定是否有办法传递数量?也许只是创建一个重复以下要点的新数据框:

library(plyr)
temp <- apply(heatdata, 1, function(x) { data.frame(lon = rep(x[2], x[1]),
lat = rep(x[3], x[1]))})
heatdata2 <- ldply(temp, rbind)


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

关于r - ggmap R 中带有数量的热图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28905089/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com