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

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With