1

I am playing with RGooglemaps, and have been able to plot lines on a map. I loaded my lats and longs from a csv into a coords object.

I wanted to imply direction using: PlotArrowsOnStaticMap

defined as:

    PlotArrowsOnStaticMap(MyMap, lat0, lon0, lat1 = lat0, lon1 = lon0, TrueProj = TRUE, FUN = arrows, add = FALSE, verbose = 1,...)

I define lat0 as something like coords[,'lat']. How do I give the lat1?

The value is the next row in the file - but how do I describle that relatively? (coords[+1,'lat'] in pseudocode.

Is there some basic reading I should be doing?

gaijintendo
  • 423
  • 2
  • 14

2 Answers2

4

An inelegant work around is to create new columns for your lat and long that are upshifted by one row compared to the initial rows. The value of the first row is wrapped to the bottom (or replaced by NA if this does not make sense).

coords$lat.1<-coords$lat[c(2:length(coords$lat), 1)]
coords$lon.1<-coords$lon[c(2:length(coords$lon), 1)]

You now have two columns for lat (lat and lat1) and two columns for long (lon, lon1).

with(coords, PlotArrowsOnStaticMap(lat0=lat, lon0=lon, lat1=lat1, lon1=lon1...)

Etienne Low-Décarie
  • 13,063
  • 17
  • 65
  • 87
  • I wouldn't say 'inelegant', it beats doing it in Excel - and it will solve the problem. I will give that a spin over lunch. Thanks – gaijintendo Mar 21 '12 at 11:36
  • If you're going to do this often, consider writing a tiny function which creates a temporary matrix per @Etienne's code and creates the plot. That way you don't end up with extra columns in your actual data file. – Carl Witthoft Mar 21 '12 at 12:32
  • @gaijintendo, if this answer satisfies you, use the green check mark to accept the response. – Etienne Low-Décarie Mar 25 '12 at 23:36
  • It is a good working solution, but what I really wanted to know was whether you can avoid doing exactly this; whether you could reference things relatively. As I understand it you can't, as I am passing a column in effect. – gaijintendo Mar 26 '12 at 14:46
1

Some of the functions commonly used to do this include head, tail, and embed:

> tmp <- 1:10
> cbind( head(tmp,-1), tail(tmp,-1) )
      [,1] [,2]
 [1,]    1    2
 [2,]    2    3
 [3,]    3    4
 [4,]    4    5
 [5,]    5    6
 [6,]    6    7
 [7,]    7    8
 [8,]    8    9
 [9,]    9   10
> embed(tmp, 2)
      [,1] [,2]
 [1,]    2    1
 [2,]    3    2
 [3,]    4    3
 [4,]    5    4
 [5,]    6    5
 [6,]    7    6
 [7,]    8    7
 [8,]    9    8
 [9,]   10    9
Greg Snow
  • 48,497
  • 6
  • 83
  • 110