Search code examples
rfor-loopdictionarygreat-circle

Creating great circles map with R


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")  
}

Solution

  • 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)
    ...
    }