13758

For each type of function call to route & geom_leg

Question:

I'm building up a route with multiple way points using ggmap but the route function doesn't seem to handle way points. I can only get it working with a single from/to.

To overcome this I thought of creating the route leg by leg, with a call to route() and geom_leg() for each leg. This isn't ideal due to code repetition.

I wonder if it's possible to vectorise this.

As an example.

<ol><li>Plot Base map</li> <li>For each vector of start/dest call route/geom_leg</li> </ol>

I've created sample code below. What's the ideal way to reduce the repetition?

start<-c("CH1 6JS","CH1 1RS","CH1 2HT") dest<-c("CH1 1RS","CH1 2HT","CH1 3DX") leg <-function(start, dest) return (route(from=start,to=dest,mode = c("walking"),structure = c("legs"))) l<-leg(start[1], dest[1]) base<-qmap('Chester, UK', zoom = 15, maptype = 'road', base_layer = ggplot(aes(x = startLon, y = startLat), data = l)) b<-geom_leg(aes(x = startLon, y = startLat, xend = endLon, yend = endLat), alpha = 2/4, size = 2, data = l, colour = 'red') l<-leg(start[2], dest[2]) c<-geom_leg(aes(x = startLon, y = startLat, xend = endLon, yend = endLat), alpha = 2/4, size = 2, data = l, colour = 'red') l<-leg(start[3], dest[3]) d<-geom_leg(aes(x = startLon, y = startLat, xend = endLon, yend = endLat), alpha = 2/4, size = 2, data = l, colour = 'red') base+b+c+d

Answer1:

I ended up using a loop.

start<-c("CH1 6JS","CH1 1RS","CH1 2HT") dest<-c("CH1 1RS","CH1 2HT","CH1 3DX") df<-data.frame(start,dest) leg <-function(start, dest){ r<- route(from=start,to=dest,mode = c("walking"),structure = c("legs")) c<- geom_leg(aes(x = startLon, y = startLat, xend = endLon, yend = endLat), alpha = 2/4, size = 2, data = r, colour = 'blue') return (c) } a<-qmap('Chester, UK', zoom = 15, maptype = 'road') for (n in 1:3){ #l<-leg(start[n], dest[n]) l<-leg(as.character(df[n,1]), as.character(df[n,2])) a<-a+l } a

Recommend

  • read csv file without header lines in python
  • SQl Query for concating multiple rows with same ids but with different values which having same date
  • Copy 6 byte array to long long integer variable
  • How to get latest unique entries from sqlite db with the counter of entries via Django ORM
  • Activating an input field based on checkbox flag in SAPUI5
  • sorting strings using bubblesort in java
  • Finding Minimum hamming distance of a set of strings in python
  • ViewPager with Fragments holding GridView
  • ggplot2 plot function with several arguments
  • To split a string and to check if they are anagram to each other
  • HOW select min from cast varchar to int in mysql
  • XSL variable memory usage
  • XQuery get Random Text from a List
  • How to serialize class hierarchies whose classes refer to each to other, but avoiding XmlInclude?
  • bridging templates with runtime arguments
  • Matplotlib: Custom colormap with three colors [duplicate]
  • How to deal with SpiderWebPlot in JFreeChart?
  • Add vcard to Contacts with Mail rules and Applescript
  • preg_replace speed optimisation
  • Copy folder content with Ansible
  • Thrust filter by key value
  • Geom_jitter colour based on values
  • runtime-check whether an instance (Base*) override a parent function (Base::f())
  • Is there a package like bigmemory in R that can deal with large list objects?
  • How to create a 2D image by rotating 1D vector of numbers around its center element?
  • In matplotlib, how do you change the fontsize of a single figure?
  • Spring: No transaction manager has been configured
  • accepts_nested_attributes_for practical form use for in Rails 3
  • Object and struct member access and address offset calculation
  • How to determine if there are bytes available to be read from boost:asio:serial_port
  • Initializer list vs. initialization method
  • Modifying destination and filename of gulp-svg-sprite
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • python regex in pyparsing
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Android Google Maps API OnLocationChanged only called once
  • How can I remove ASP.NET Designer.cs files?
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?