Search code examples
rggplot2colorsggmapkernel-density

Manually setting scale_alpha for stat_density2d plot


I want to plot monthly maps of local construction specifically using stat_density2d from ggplot2. I am using the following code:

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")

which produces enter image description here and enter image description here

How can I manually set the stat_density() fill parameter to give the same legend for both plots? According to this question, the ..level.. parameter is calculated by calling MASS::kde2d. Manually calling this gives a list:

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 ...

This question is pretty close, but complains about Error: Unknown parameters: breaks for stat_density2d() in ggplot2 version 2.1.0


Solution

  • Add the same limits argument to scale_fill_gradient in both plots. For example, scale_fill_gradient(low = "green", high = "red", limits=c(0,70000)) (or whatever range you wish). Then the same colors will be mapped to the same values in each plot.

    Here's what the plots look like with the limits I added:

    enter image description here

    (Note: The title of your question mentions scale_alpha, but the body of your question asks how to get the same fill legend. I've assumed you want the same fill legend in each plot, but please let me know if you wanted something different.)