0

I'm creating two maps, one with contour lines and the other is a heatmap. I'm using the geom_density2d function to create the one with contour lines, and stat_density2d to create the heatmap.

Note that geom_density2d does not seem to have the same parameters as geom_density, which now includes a bw argument to specify the bandwidth.

I understand (after a lot of searching) that both of these functions are defaulting to "Silverman's rule of thumb" bw.nrd0 to calculate the bandwidth, as described in this blog post. However I would like to change the bandwidth calculation to use the Sheather and Jones method instead bw = "SJ" but have no idea how to do this as this parameter is not accepted by ggplot2 (neither the geom nor the stat functions).

Example data is provided here.

The code I'm using to create the contour map is below:

# Create the contour map with geom_density2d:
contourmap <- ggplot2::ggplot() + 
  geom_sf(data = phecll, 
          colour = "black", 
          fill = "#004529",
          alpha = 0.5,
          size = 0.75) +
  coord_sf() + 
  geom_density2d(data = rdmdata, 
                 mapping = aes(x = home_long, 
                               y = home_lat, 
                               alpha = 0.5),
                 inherit.aes = FALSE, 
                 contour_var = "density")

And for the heatmap:

# Create the heatmap with stat_density2d:
heatmap <- ggplot2::ggplot() + 
  geom_sf(data = phecll, 
          colour = "black", 
          fill = "#004529",
          alpha = 0.5,
          size = 0.75) +
  coord_sf() + 
  stat_density2d(data = rdmdata, 
                 mapping = aes(x = home_long, 
                               y = home_lat, 
                               fill = ..level.., 
                               alpha = ..level..), 
                 size = 0.01,  
                 bins = 50,
                 geom = "polygon", 
                 inherit.aes = FALSE) + 
  scale_fill_gradient(low = "blue", 
                      high = "red", 
                      guide = "none") + 
  scale_alpha(range = c(0, 0.5),
              guide = "none")

How can I change the bandwidth calculation from nrd0 to sj?

Amy M
  • 967
  • 1
  • 9
  • 19
  • 1
    One of the 26 hits to an SO search on "ggplot2 bandwidth density" is this one which I think answers the question: https://stackoverflow.com/questions/24985361/understanding-bandwidth-smoothing-in-ggplot2 If you do not want the question closed for lack of data or as a duplicate you should [edit] you question to explain why it's not a duplicate and put up a link to a place where your data (or something very much like it) can be obtained. – IRTFM Dec 11 '21 at 22:30
  • I have edited to include data - shape files not included but the dataset has coordinates called `home_lat` and `home_long` which should be sufficient to reproduce the contour lines and hotspots. The referenced question deals with geom_density which is not quite the same as geom_density2d in terms of allowed parameters - have updated my post to explain this. – Amy M Dec 11 '21 at 23:55
  • All that remains before work can begin is a precise description and implementation of the "Sheather and Jones method". Make sure your implementation is compatible with an avaialble density estimation algorithm. (I've only seen an SJ bandwidth selection method mentioned for 1d methods so far, but I'm not an expert so I may not have found relevant code that you know exists. – IRTFM Dec 12 '21 at 00:40
  • Further question: your geom_density2d uses a different dataset so first assigning the example dataset to `phecll` now throws a missing data error in the second stage of that plotting process. – IRTFM Dec 12 '21 at 00:44

0 Answers0