rggplot2rgooglemaps

Adding an arrow and marker on a map in R


I have made a map using ggplot2 package of R and also a geom_path visualized on it. Now, I want to add a few markers and just one arrow in order to show the direction of the path. For arrows there is a package called grid which has arrow() function, but if I use this function in my geom_path aesthetics, it will draw arrows for each point of my path. So, I just want to draw an arrow (just one arrow) in order to visualize the direction of the path. The second issue is how to add few markers on my map. I've read there is a nice package RGoogleMaps, which allows that, but the problem is that I am using ggplot2. Any idea how can I add an arrow and markers to my map? Thanks in advance!

here is my code:

require(move)
require(ggmap)
require(mapproj)
gps <- move(x=temp$GPS_x, y=temp$GPS_y,time=as.POSIXct(temp$GPS_timeDate,        format="%d/%m/%Y %H:%M:%S", tz="UTC"), proj=CRS("+proj=longlat +ellps=WGS84"), animal='unknown', sensor='NoName')
gps_df <- as(gps, "data.frame")
m <- get_map(bbox(extent(gps)*1.1), source="osm", zoom=14)
ggmap(m)+geom_path(data=gps_df[1:nrow(soc_long),], aes(x=temp$GPS_x[1:nrow(soc_long)],    y=temp$GPS_y[1:nrow(soc_long)],colour=soc_long$dsoc),size=1.5,lineend="round")+scale_color_gradient(low='green',high='red')

The output is the following:

enter image description here This is the part of temp data set:

GPS_timeDate       GPS_x        GPS_y
13/11/13 14:54:15   10.17936802 54.33073984
13/11/13 14:54:16   10.17936768 54.33073967
13/11/13 14:54:17   10.17936735 54.33073951
13/11/13 14:54:18   10.17936702 54.33073934
13/11/13 14:54:19   10.17936668 54.33073917
13/11/13 14:54:20   10.17936635 54.33073901
13/11/13 14:54:21   10.17936601 54.33073884
13/11/13 14:54:22   10.17936568 54.33073867
13/11/13 14:54:23   10.17936535 54.33073851
13/11/13 14:54:24   10.17936501 54.33073834
13/11/13 14:54:25   10.17936547 54.33073849
13/11/13 14:54:26   10.17936597 54.33073866
13/11/13 14:54:27   10.17936646 54.33073882
13/11/13 14:54:28   10.17936696 54.33073899
13/11/13 14:54:29   10.17936745 54.33073915
13/11/13 14:54:30   10.17936794 54.33073931
13/11/13 14:54:31   10.17936844 54.33073948
13/11/13 14:54:32   10.17936893 54.33073964
13/11/13 14:54:33   10.17936943 54.33073981
13/11/13 14:54:34   10.17936992 54.33073997
13/11/13 14:54:35   10.17936943 54.33074
13/11/13 14:54:36   10.17936875 54.33074
13/11/13 14:54:37   10.17936807 54.33074
13/11/13 14:54:38   10.17936739 54.33074
13/11/13 14:54:39   10.17936671 54.33074
13/11/13 14:54:40   10.17936603 54.33074
13/11/13 14:54:41   10.17936535 54.33074
13/11/13 14:54:42   10.17936467 54.33074
13/11/13 14:54:43   10.17936399 54.33074

soc_long data set's values are used for color gradient:

    dsoc
1   0.348334562
2   0.348334562
3   0.348334562
4   0.348334562
5   0.348334562
6   0.348334562
7   0.348334562
8   0.348334562
9   0.348334562
10  0.348334562
11  0.367477325
12  0.367477325
13  0.367477325
14  0.367477325
15  0.367477325
16  0.367477325
17  0.367477325
18  0.367477325
19  0.367477325
20  0.367477325
21  0.248041791
22  0.248041791
23  0.248041791
24  0.248041791
25  0.248041791
26  0.248041791
27  0.248041791
28  0.248041791
29  0.248041791
30  0.248041791

And finally, the following pints are the point that I want to add as markers to the path:

lat           lon
54.31545667 10.168515
54.30135833 10.14450333
54.30305167 10.137645
54.3072 10.13840667
54.31827833 10.13377
54.32098833 10.12690667
54.32025833 10.12314
54.31826833 10.12340333
54.31540667 10.12799167
54.31492167 10.13006833

Solution

  • You can give data to individual plotting functions (you need to make sure the global aesthetics (those mentioned in ggplot, ggmap, etc) match up though. So you have data.frames the first is gps_df[1:nrow(soc_long),] and the second is (call it markers) the lat and lon of the markers. This list has no direction information. So you need to calculate direction information by shifting the columns up to calculate a direction

    markers$lat.after<-c(markers$lat[-1],NA)
    markers$lon.after<-c(markers$lon[-1],NA)
    markers$lat.dir<-with(markers,lat.after-lat)
    markers$lon.dir<-with(markers,lon.after-lon)
    

    Now you have the positions and directions (you don't need directions now but it might be useful for something else) so you can simply add the following term to your code

    +geom_segment(data=markers,aes(x=lon,xend=lon.after,y=lat,yend=lat.after),arrow=arrow(length=unit(0.3,"cm")))
    

    This might be too coarse for arrow direction, so you could instead do the same with the gps_df field and then just plot a portion of it

    +geom_segment(data=gps_df[seq(1,nrow(gps_df),5),...
    

    which would only plot every 5 points as an arrow