3

I'm trying to draw a line between two cities, on a world map, using R (using map and wrld_simpl).

How can I get a smooth line between the two points? If I drew a line between US and Australia, it has three different segments

require(rworldmap)

map("world", col="#f2f2f2", fill=TRUE, bg="white", lwd=0.05)

data(wrld_simpl)

US_lat = wrld_simpl$LAT[wrld_simpl$NAME == 'United States']
US_lon = wrld_simpl$LON[wrld_simpl$NAME == 'United States']

australia_lat = wrld_simpl$LAT[wrld_simpl$NAME == 'Australia']
australia_lon = wrld_simpl$LON[wrld_simpl$NAME == 'Australia']

lines(c(US_lon, US_lat), c(australia_lon, australia_lat))

This draws a line, but it isn't correctly between aus and US. what am I doing wrong?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • 1
    What do you mean by 'smooth'? You can get a straight line just by using lines(x,y), ie with just the coordinates (dont use gcIntermediate, which does great circles). – Spacedman Dec 11 '11 at 00:08
  • oh I thought I was supposed to use gcIntermediate. what other function should I use? –  Dec 11 '11 at 00:10
  • gcIntermediate draws a great circle - shortest distance. If that involves crossing the -180 line and your map is mercator, centred on 0, then it will probably break it into bits (can't tell, you've not given us a reproducible example). If you just do lines(c(US_lon,aus_lon),c(US_lat,aus_lat)) then you should get a straight line between the points. If that's what you want. – Spacedman Dec 11 '11 at 00:26
  • US_lat = wrld_simpl$LAT[wrld_simpl$NAME == 'United States'] US_lon = wrld_simpl$LON[wrld_simpl$NAME == 'United States'] australia_lat = wrld_simpl$LAT[wrld_simpl$NAME == 'Australia'] australia_lon = wrld_simpl$LON[wrld_simpl$NAME == 'Australia'] lines(c(US_lon, US_lat), c(australia_lon, australia_lat)) That doesn't draw the line correctly. What am I doing wrong? –  Dec 11 '11 at 00:35
  • can you edit your q so we can see this code formatted properly? – Spacedman Dec 11 '11 at 00:38

2 Answers2

2

The method lines takes a vector x of x-coordinates and y of y-coordinates. Therefore your line should look like this:

lines(c(US_lon, australia_lon), c(US_lat, australia_lat))

If you want a great circle, the following will work:

require(geosphere)
gc <- gcIntermediate(c(US_lon, US_lat), c(australia_lon, australia_lat), breakAt=TRUE, n=1000)
invisible(lapply(gc, lines, col='red', lwd=2))
pete
  • 2,327
  • 2
  • 15
  • 23
  • It's one of those 'invisible' mistakes. You'll never see it unless you're not the person who made it. – pete Dec 11 '11 at 02:09
1

You can also draw a "curve" using grid.curve():

grid.curve(US_lon, australia_lon, US_lat, australia_lat)

... and you can define the "look" of the curve.

Dan
  • 113
  • 1
  • 5