3

All, I am trying to create a map with "Great Circles" similar to what Nathan Yau did in this posting. However, I am trying to do it for the whole world and all coming into a single location. I seem to be having problems with the loop section of it. If I just use one lat/long combo, everything works. As soon as I build out my table larger I get errors (Error in .pointsToMatrix(p1) : Wrong length for a vector, should be 2) I am a 100% newb at R and would love some help

lat.txt

LAT,LONG  
39.164141,-121.640625

R commands

library(maps)  
library(geosphere)  
lat_me <- 45.213004  
lon_me <- -68.906250  
map("world", col="#f2f2f2", plot = TRUE, fill=TRUE, bg="white")  
data <- read.csv("/Users/blah/R/latlon/lat.csv",sep=",", header=TRUE)  
for (i in 1:length(data)) {  
  inter <- gcIntermediate(c(data$LONG, data$LAT), c(lon_me, lat_me), n=50, addStartEnd=TRUE)  
  lines(inter,col="red")  
}
Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
breadly
  • 85
  • 1
  • 8
  • Sorry, that was actually just cut+paste from R. The + symbols are just graphically representing the "for" loop I believe – breadly Jun 26 '13 at 21:04
  • I suggest you don't use a loop, since `gcIntermediate` is already vectorised. – Andrie Jun 26 '13 at 21:15
  • @Andrie Can you elaborate a little? I am doing my research right now on why I what I could do instead, but for this I was following the example given by Nathan. – – breadly Jun 26 '13 at 21:45
  • Some time ago I posted some code using `lattice` graphics [here](http://procomun.wordpress.com/2011/05/20/great-circles/). – Oscar Perpiñán Jul 24 '13 at 16:13

2 Answers2

6

I see that you index your loop with i but dont include that anywhere inside the loop. I imagine that you want to loop over the rows of your data. So change the index range to 1:nrow(data), and include the index for the row you want do draw for each i.

for( i in 1:nrow(data)){
inter <- gcIntermediate(c(data$LONG[i], data$LAT[i]), 
                        c(lon_me, lat_me), 
                        n=50, 
                        addStartEnd=TRUE)
...
}
Seth
  • 4,745
  • 23
  • 27
  • Wow... that certainly worked. That solved my exact question perfectly – breadly Jun 26 '13 at 21:19
  • @bread555, welcome to S.O. If this answer solved your question, please make sure to indicate by clicking on the green check-mark next to the question. – Ricardo Saporta Jun 26 '13 at 21:23
  • @RicardoSaporta I certainly will, I was just waiting to see if anything else came in. However, I'll select it now. – breadly Jun 26 '13 at 21:36
4

gcIntermediate is vectorsied (as pointed out by @Andrie in the comments), so you do not need a loop:

me <- c( 45.213004 , -68.906250   )

set.seed(123)
pts <- data.frame( x = runif(4,-180,180) , y = runif(4,-90,90) )
pts
#          x          y
#1 -76.47209  79.284111
#2 103.78985 -81.799830
#3 -32.76831   5.058988
#4 137.88627  70.635428

#  Just supply the two column data.frame - no need for loops!
#  Also return as 'SpatialLines' object to make plotting easier
inter <- gcIntermediate( pts , me , n=50 , addStartEnd=TRUE , sp = TRUE) 

map("world", col="#f2f2f2", plot = TRUE, fill=TRUE, bg="white") 
plot(inter ,add=T , col = "red" , lty = 2 )

enter image description here

Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
  • @Simon0101 wow. Thanks for the further explanation. Looks like I am going to be looking alot of those terms/commands up because I haven't seen them before. Specifically what do you mean by using SpatialLines for easier plotting? – breadly Jun 27 '13 at 02:55