我想使用ggplot2中的stat_density2d
专门绘制本地构造的月度地图。我使用以下代码:
library(ggmap)
options(stringsAsFactors=T)
mar_2016 <- structure(list(Latitude = c(47.6064785, 47.6086266, 47.61556306,
47.5335102, 47.53424442, 47.62629031, 47.66231885, 47.55882248,
47.63847192, 47.63847192, 47.63847192, 47.63846822, 47.66045328,
47.64787497, 47.6774562, 47.61126358, 47.61126358, 47.62041964,
47.56586818, 47.56600353), Longitude = c(-122.3313647, -122.32373921,
-122.33276181, -122.3473748, -122.29267751, -122.32268625, -122.28147251,
-122.36343703, -122.36995433, -122.36995433, -122.36995433, -122.36979987,
-122.30463329, -122.37841989, -122.31846715, -122.31515559, -122.31515559,
-122.35567747, -122.3694264, -122.36942834)), .Names = c("Latitude",
"Longitude"), row.names = c(NA, -20L), class = "data.frame")
apr_2016 <- structure(list(Latitude = c(47.62373953, 47.61304116, 47.57543057,
47.59216, 47.66411106, 47.71560842, 47.55499461, 47.55878986,
47.5224678, 47.5224678, 47.63419063, 47.70380892, 47.6309837,
47.60766995, 47.52517771, 47.68700879, 47.68495153, 47.6733502,
47.61578904, 47.66961751, 47.62981265, 47.62126347, 47.60219778,
47.6086266, 47.52835462, 47.67514813, 47.67514813, 47.66513236,
47.56720557, 47.66590858), Longitude = c(-122.30442288, -122.34060091,
-122.39628552, -122.29016643, -122.34337082, -122.31338271, -122.38583914,
-122.26472731, -122.35949442, -122.35949442, -122.35408737, -122.37250127,
-122.3076156, -122.32802388, -122.36489489, -122.38298825, -122.36237138,
-122.35121936, -122.33727143, -122.36591715, -122.29125798, -122.32173169,
-122.33348074, -122.32373921, -122.39195911, -122.28949534, -122.28949534,
-122.28937395, -122.39185006, -122.2962011)), .Names = c("Latitude",
"Longitude"), row.names = c(412L, 416L, 417L, 426L, 427L, 428L,
429L, 434L, 437L, 438L, 454L, 456L, 475L, 489L, 490L, 491L, 492L,
493L, 494L, 495L, 496L, 507L, 510L, 516L, 525L, 526L, 527L, 528L,
529L, 541L), class = "data.frame")
denny_park = get_map(location=c(-122.3437739, 47.6193031), zoom=15)
ggmap(denny_park) +
geom_density2d(data = apr_2016, aes(x = Longitude, y = Latitude), size = 0.3) +
stat_density2d(data = apr_2016,
aes(x = Longitude, y = Latitude, 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) +
ggtitle("April 2016")
ggmap(denny_park) +
geom_density2d(data = mar_2016, aes(x = Longitude, y = Latitude), size = 0.3) +
stat_density2d(data = mar_2016,
aes(x = Longitude, y = Latitude, 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) +
ggtitle("March 2016")
如何手动设置stat_density()
填充参数以为两个图提供相同的图例?根据此question,..level..
参数是通过调用MASS::kde2d
来计算的。手动调用它会给出一个列表:
R> apr_2016_kde <- MASS::kde2d(apr_2016$Longitude, apr_2016$Latitude)
R> str(apr_2016_kde)
List of 3
$ x: num [1:25] -122 -122 -122 -122 -122 ...
$ y: num [1:25] 47.5 47.5 47.5 47.5 47.6 ...
$ z: num [1:25, 1:25] 23.2 27 30.2 32.7 34.2 ...
此question非常接近,但抱怨错误:未知参数:ggplot2版本2.1.0中stat_density2d()
的中断