1

I am making a density map in R using ggmap and stat_density2d. The code looks like this:

riverside <- get_map('Riverside, IL', zoom = 14 , color = 'bw' ) 
RiversideMap <- ggmap(riverside, extent = 'device', legend = 'topleft')

# make the map: 
RiversideMap +  
stat_density2d(aes(x = lon, y = lat,
fill = ..level.. , alpha = ..level..),size = .01, bins = 16,
data = myData, geom = 'polygon') + 
scale_fill_gradient(low = "yellow", high = "blue") +  
scale_alpha(range = c(.0, 0.3), guide = FALSE)

The density shown in the map's color legend is normalized in stat_density2d by requiring the integral of the density over area equals 1.

In the map, the units of the x and y axes are decimal degrees. (For example, a point is specified by the coordinates lat = 41.81888 and lon = -87.84147).

For ease of interpretation, like to make two changes to the values of the density as displayed in the map legend.

First, I'd like the integral of the density to be N (the number of data points - or addresses - in the data set) rather than 1. So the values displayed in the legend need to be multiplied by N = nrow(myData).

Second, I'd like the unit of distance to be kilometers rather than decimal degrees. For the latitudes and longitudes that I am plotting, this requires dividing the values displayed in the legend by 9203.

With the default normalization of density in stat_density2d, I get these numbers in the legend: c(2000,1500,1000,500).

Taking N = 1600 and performing the above re-scalings, this becomes c(348, 261, 174, 87) (= 1600/9203 * 2000 etc). Obviously, these are not nice round numbers, so it would be even better if the legend numbers were say c(400,300,200,100) with their locations in the legend color bar adjusted accordingly.

The advantage of making these re-scalings is that the density in the map becomes easy to interpret: it is just the number of people per square km (rather than the probability density of people per square degree).

Is there an easy way to do this? I am new to ggmap and ggplot2. Thanks in advance.

maj
  • 2,479
  • 1
  • 19
  • 25
J Miller
  • 11
  • 3
  • Your question looks quite long - if you'd like people to answer your question anyway, you should maybe add some formatting, a graphical expression of what your "desired output" should look like or a short summary. And just in case you didn't know them, here are the 2 main pages to skim before asking your first question: [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask), [How to make a great R reproducible example?](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – maj Aug 21 '15 at 20:06
  • I am making a density map in R using ggmap and stat_density2d. The plot produces a legend which associates colors with numbers. I want to leave the plot unchanged but change the numbers that appear in the legend next to the colors. For example, the numbers in the default legend might be (2000,1500,1000,500). I'd like to override this default and make the numbers (348, 261, 174, 87). In addition, I would also like to force the legend to show the numbers (400,300,200,100) and to shift the location of the numbers slightly in the legend so that they line up with the appropriate colors. – J Miller Aug 22 '15 at 22:15

1 Answers1

1

In brief, use:

scale_fill_continuous(labels = scales::unit_format(unit = "k", scale = 1e-3))

This link is great help for managing scales, axes and labels: https://ggplot2-book.org/scales.html

סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68