I have two sets of latitude & longitude variables for a large number of rows in my data frame (~100,000). I am trying to make a plot that connects those two sets of coordinates (i.e, ~100,000 lines that go from latitude1,longitude1 to latitude2,longitude2), using geom_segment, with a very low alpha to make the lines transparent because there are so many lines.
I would like to emphasize the starting points and end points of those lines, and I reckoned the best way to do that would would be to have a colour gradient from start to end (let's say green to red).
Is it possible to draw a geom_segment line with a colour gradient? If not, do you know another way to emphasize start vs end with so many lines?
(I realize that it could end up looking messy because there are so many lines, but I suspect that many of them go in the same direction..)
Here is some example data of 5 rows (but in reality I have ~100,000, so it should be somewhat computationally efficient):
example.df <- as.data.frame(matrix(c(1,435500,387500,320000,197000,
2,510500,197500,513000,164000,
3,164500,40500,431000,385000,
4,318500,176500,316000,172000,
5,331500,188500,472000,168000),
nrow=5, ncol=5, byrow = TRUE))
colnames(example.df) <- c("ID","longitude.1","latitude.1",
"longitude.2","latitude.2")
library(ggforce)
ggplot(example.df, aes(longitude.1, latitude.1))+
geom_link(aes(x=longitude.1, y=latitude.1,
xend=longitude.2, yend=latitude.2,
alpha=0.5), col="black")+
coord_equal()
This produces these five lines:
I would like these lines to start as blue at their first longitute-latitude coordinate point and end as red at the second longitute-latitude coordinate point.