为stat_density2d绘图手动设置scale_alpha

时间:2016-05-04 22:43:56

标签: r ggplot2 colors ggmap kernel-density

我想使用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")

生成enter image description hereenter image description here

如何手动设置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()的中断

1 个答案:

答案 0 :(得分:2)

在两个图中为limits添加相同的scale_fill_gradient参数。例如,scale_fill_gradient(low = "green", high = "red", limits=c(0,70000))(或您希望的任何范围)。然后相同的颜色将映射到每个图中的相同值。

以下是我添加限制的情节:

enter image description here

(注意:你问题的标题提到了scale_alpha,但你问题的正文询问如何获得相同的填充图例。我假设你想要在每个情节中使用相同的填充图例,但是如果你想请告诉我想要不同的东西。)