3

I'm clearly struggling with this problem for a day now and can't seem to find a nice solution to it. I would really appreciate some help and I'm really a novice in R (since last week).

Problem 1:

I have a set CSV representing grid points which I can parse into a data frame (pointname, latitude, longitude).

Eg:

name,latitude,longitude
x0y0,35.9767,-122.605
x1y0,35.9767,-122.594
x2y0,35.9767,-122.583
x0y1,35.9857,-122.605
x1y1,35.9857,-122.594
x2y1,35.9857,-122.583
x0y2,35.9947,-122.605
x1y2,35.9947,-122.594
x2y2,35.9947,-122.583

The points in this file represent the lower left corner and are arranged in row major format, meaning lowest horizontal grid points first. Each point is a certain great circle distance away from its neighbors (1km). I want to create a grid overlay on a map which I've plotted using ggmap.

What I've tried or considered:

  1. map.grid() - this is really not useful to me as I'm not looking for any kind of projection.
  2. geom_vline() and geom_hline(). These look good but I don't have constant x and y intercepts on a plane. Moreover, once I create a grid, I'd like to use the grid to color against a density.
  3. geom_rect() and geom_tile(). These look really promising and may be what I want. But I'm not able to find a good way of working with these.

I'd like to fill these grid boxes later with another parameter. Any suggestions on how I can create such a grid? This may be a trivial question but I don't know a lot of R yet.

Problem 2:

How can I store or hold such a grid so that I given a point (lat,lon), I can quickly get to that grid. In fact my whole back end is in C++ and can directly output the grid name x<n>y<n> directly against a given search point. I somehow am finding it difficult to count such points against grid points so that I can fill grid with a representative color.

I'm not sure if everything of what I'm saying is clear. Please tell me if I've to clarify something.

Also note that I've Googled quite a lot and not found relevant answers although some looked close.

Eg: This, ThisToo

Thanks for the help!

Community
  • 1
  • 1
asterx
  • 85
  • 1
  • 8
  • `ggplot(df) + geom_raster(aes(longitude, latitude, fill = name))` probably, although you'll need to calculate midpoints if you want the corners at the coordinates you specify. For Problem 2, use `cut` with breaks at your grid coordinates so you can make a table of occurrences. – alistaire Jun 21 '16 at 05:17
  • @alistaire: Yep, exactly the issue. I forgot to mention that I did consider `geom_raster` as well, but the center-point finding needs quite a bit of computation. About using `cut`, do you have any example that I can look into? – asterx Jun 21 '16 at 05:25
  • If the points are a consistent distance apart, you can just add half of that in appropriate units. If you're covering enormous distances you may need to worry about curvature, but at a kilometer scale you should be fine. Working backwards to find the difference using `dplyr`, `df %>% mutate_each(funs(. + unique(round(diff(sort(unique(.))), 4))/2), -name) %>% ggplot() + geom_raster(aes(longitude, latitude, fill = name))`. For `cut` start with just `?cut`. Alternatively you could find the minimum distance from one of the centroids and aggregate that way, if it makes more sense. – alistaire Jun 21 '16 at 05:50
  • I'll try these. I need to take care of curvatures because in all I'm dealing with areas close to 700 sq. miles. Thanks though! – asterx Jun 21 '16 at 16:43

0 Answers0