6

In my example I am plotting a shapefile for all England & Wales, however I'm interested in the greater London area. If I allow ggplot to simply default and display the entire shapefile then the heatmapping generated by stat_density2d isn't very useful for me, I need to change the scale to show the Greater London area.

To address this I need to set the xlim and ylim to effectively zoom in. I can do this in one of two ways:

  1. Use the xlim & ylim standalone functions in ggplot
  2. Use xlim & ylim within coord_map

The former causes the heatmapping to scale correctly (ie size of the tiles reduce to give a meaningful indication of density in a smaller area) but the shapefile data gets truncated to the plot leading to some very odd looking polygons.

The latter causes the heatmapping to remain at the scale of the whole country (ie the tiles are massive and so not useful for showing activity density when the map is zoomed in) but the polygons display correctly.

Is there a way to get ggplot to change the xlim/ylim values of a plot without truncating the shapefile/polygon data so that it also rescales the tiles generated by geom_density2d?

I know I could fudge this by only plotting a subset of the shapefile polygons but this seems...messy. It would be way cooler if I could choose anywhere in the country to zoom/centre on and have everything scale correctly without having to chop and change the shapefile data.

This is a follow up to my previous question: How do I create a heatmap in ggplot?

Edit 1: I'm currently investigating a few undocumented arguments that stat_density2d passes to the kde2d 2d density function from the MASS package. There are a few of these but I get the impression a combination of n and lims may help. Basically if I pass a higher value to n (this dictates the number of rows and columns there are in the matrix produced) then the 'resolution' of the 2d density estimation increases at the expense of processing time (this is applied to the entire bounding area of the shapefile rather than just the window I'm limiting the plot to using coord_map). I then use lims to try and limit the matrix to just the plotting area but that doesn't seem to have much effect.

closed as off-topic by Andre Silva, underdark Dec 9 '16 at 18:00

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking help to debug/write/improve code must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Providing a clear problem statement and evidence of a code attempt will help others to help you. See: How to create a Minimal, Complete, and Verifiable example." – Andre Silva, underdark
If this question can be reworded to fit the rules in the help center, please edit the question.

  • Why don't you want to use QGIS for map creation? It is far more simpler then in R! R is great for analysis and graph plotting, but not for map creation (unless it is something very simple). You should not use one tool for all your tasks. – SS_Rebelious Jul 26 '13 at 10:27
  • 2
    I already use QGIS for a lot of stuff, this is more a proof of concept right now. If I nail this one scaling quirk then I can write some very simple scripts that take care of the vast majority of my mapping needs and which can be rerun very easily. I can create a script that allows dynamic scaling based on parameters which generate maps as image files which can then be supplied to end users. Also I think the way you can define palettes in ggplot is way better than in QGIS (though I am a big QGIS fan). – Tumbledown Jul 26 '13 at 10:34
  • 1
    I'm going to have to agree with the original poster here. I use QGIS every now and again (mostly to fix broken geometry) but for reproducible creation of multiple maps with a common theme or common data I find R to be a far more useful tool. – SlowLearner Jul 26 '13 at 21:38
  • 1
    Please provide a reproducible example - e.g. gis.stackexchange.com/questions/78618/… then we can provide specific answers in the language of R rather than ambiguous answers in the language of English. – RobinLovelace Mar 29 '14 at 10:42
  • THis question appears to have been abandoned after Robin's comment. – kttii Oct 17 '16 at 16:02

Browse other questions tagged or ask your own question.