1

I have a list of start longidudes,latitudes and end longitudes and latitudes in a .csv file which i need to plot on a map and join with lines. The coordinates are for Manchester in England.

.csv example:

Timestamp    Start Description  End Description  Start(lon,lat)      End(lon,lat)
24/10/2016       Wimslow          Simonsway      53.371535,-2.23148   53.32803,-2.246991 
14/10/2016     Horwich Park       M1 3BG         53.58194,-2.53801    53.47837,-2.23296  

etc.

reuben
  • 91
  • 1
  • 2
  • 12
  • OK, what have you tried? Where did you get stuck? – erc Jan 31 '17 at 11:49
  • @beetroot ive just started using R, so have been playing with a few libraries, (ggmap, map). Haven't really gotten past getting a map. Cant find material anywhere to do what i need. Code so far: ` library(ggmap) map=get_map(location='Manchester',zoom=12,scale=2)` – reuben Jan 31 '17 at 11:57

1 Answers1

2

Assuming your data is called dat you can first create seperate columns for Lon and Lat (careful, the values are in opposite order of the variable names), then use geom_segment to plot the lines.

library(tidyr)
library(ggmap)
library(ggplot2)

map <- get_map(location = 'Manchester', zoom = 9, scale = 2)
dat <- dat %>%
  separate(Start.lon.lat., c("Start.Lat", "Start.Lon"), sep =",") %>%
  separate(End.lon.lat., c("End.Lat", "End.Lon"), sep =",")

ggmap(map) +
  geom_segment(data = dat, aes(x = as.numeric(Start.Lon), 
                               y = as.numeric(Start.Lat), 
                               xend = as.numeric(End.Lon), 
                               yend = as.numeric(End.Lat)))

enter image description here

erc
  • 10,113
  • 11
  • 57
  • 88
  • Thanks for the help. Im sure itll work when i get the errors sorted, atm the code is coming up with : `Error: GeomRasterAnn was built with an incompatible version of ggproto. Please reinstall the package that provides this extension.` so I downloaded the packages from github which should have solved it however this came up instead: `Warning: namespace ‘ggplot2’ is not available and has been replaced by .GlobalEnv when processing object ‘x’ Warning: namespace ‘ggplot2’ is not available and has been replaced by .GlobalEnv when processing object ‘x'` Do you know how to get it working ? – reuben Jan 31 '17 at 13:21
  • 1
    No, sorry, if you tried the answers posted to [this question](http://stackoverflow.com/questions/40642850/ggmap-error-geomrasterann-was-built-with-an-incompatible-version-of-ggproto) I don't have any idea. – erc Jan 31 '17 at 14:32