Using this as a reference, I\'m trying to plot a map of the lower forty-eight and add layers to visualize flow between states.
library(ggplot2)
library(maps)
gcIntermediate
returns different column names (since origin and destination is identical for i=2):
for (i in 1:3) {
inter <- as.data.frame(gcIntermediate(c(geo[i,]$orig_lon, geo[i,]$orig_lat),
c(geo[i,]$dest_lon, geo[i,]$dest_lat),
n=50, addStartEnd=TRUE))
print(head(inter, n=2))
}
lon lat
1 -119.7 36.17
2 -119.6 36.13
V1 V2
1 -119.7 36.17
2 -119.7 36.17
lon lat
1 -119.7 36.17
2 -119.5 36.24
The following lines should work:
for (i in 1:3) {
inter <- as.data.frame(gcIntermediate(c(geo[i,]$orig_lon, geo[i,]$orig_lat),
c(geo[i,]$dest_lon, geo[i,]$dest_lat),
n=50, addStartEnd=TRUE))
names(inter) <- c("lon", "lat")
p <- p + geom_line(data=inter, aes(x=lon, y=lat), color='#FFFFFF')
}
What strikes me as odd is that you refer to the longitude in two different ways: long
in the beginning of the script, and lon
in the end. You need to get these names consistent if you expect multiple geom's to work together.
In addition, adding identical geom's with for loops is almost never needed. Just add a single geom_line
and use the color
aesthetic to draw multiple lines.
There is a really simple solution using ggplot2
. There is simple tutorial of how to plot flow maps in R
using ggplot2
, here.
p +
geom_segment(data = geo, aes(x = orig_lon, y = orig_lat,
xend = dest_lon, yend = dest_lat,
color="#FFFFFF")) + coord_equal()