Adding a bunch of arrows to a ggmap
Asked Answered
F

1

2

I'm plotting a map, which is supposed to have an overlay of several (> 400) arrows from a dataset, which has lat/lon pairs for both start and end of each arrow. Here is a subset of the data using dput:

df <- structure(list(Lat = c(49.34054, 49.34068, 49.3409, 49.34106, 
49.34116, 49.34133, 49.34138, 49.34144, 49.34155, 49.34164, 49.34168, 
49.34178, 49.34179, 49.34187, 49.34199, 49.34202, 49.3421, 49.34219, 
49.34226, 49.34236, 49.3424), Lon = c(-117.76365, -117.76433, 
-117.76474, -117.76575, -117.76646, -117.76607, -117.76643, -117.76676, 
-117.76611, -117.76638, -117.76678, -117.76612, -117.7671, -117.76678, 
-117.76776, -117.76745, -117.76706, -117.76815, -117.76778, -117.76762, 
-117.76812), LatEnd = c(49.3404216917208, 49.3404813977525, 49.3407696999527, 
49.3409218055133, 49.3408834255181, 49.3411571438575, 49.3411444104952, 
49.3412068979592, 49.3413453850968, 49.3414853385912, 49.3414067819334, 
49.3415646398153, 49.3415782191525, 49.3416671210859, 49.341769715577, 
49.3418702688525, 49.3418749917805, 49.3419107724121, 49.3418905356976, 
49.3421097403974, 49.3419881060831), LonEnd = c(-117.76319214364, 
-117.763951728004, -117.76423214535, -117.765201260725, -117.766005995062, 
-117.765592930919, -117.765831425366, -117.766367701412, -117.765558298351, 
-117.765915748408, -117.766324336458, -117.765721708226, -117.766709104693, 
-117.766420637063, -117.767340559198, -117.767000983228, -117.766699658212, 
-117.767827633167, -117.767235785716, -117.767302080608, -117.767699155441
)), .Names = c("Lat", "Lon", "LatEnd", "LonEnd"), row.names = c(244L, 
263L, 293L, 313L, 330L, 351L, 359L, 369L, 390L, 409L, 414L, 426L, 
427L, 435L, 442L, 443L, 448L, 450L, 455L, 457L, 459L), class = "data.frame")

I've worked with ggmap before, and therefore this was my first try:

library(ggplot2)
library(ggmap)

prep <- get_googlemap(
center = c(-117.7670, 49.34027), #Long/lat of centre, or "Edinburgh"
zoom = 17, 
maptype = 'hybrid', #also hybrid/terrain/roadmap/satellite
scale = 2)

map <- ggmap(prep, 
    size = c(100, 200),
    extent='device', 
    darken = 0.5,
    legend = "bottom",
    base_layer = ggplot(data = df, aes(x = Lon, y = Lat))) 

p <- map +
    geom_segment(aes(xend = LonEnd, yend = LatEnd),
            arrow = arrow(length = unit(0.2,"cm"), angle = 15), size = 0.3, colour = "white") 

Which produced a very odd result - some of the arrows had their heads plotted in the wrong direction.

Then I proceeded to try and use geom_path instead. Which corrected for the direction of the heads, but required that I input every line of the dataset separately as input to the geom_path. I made a for loop, but apparently what happened there was that every time a layer was added ('i' upped by one), all previous layers would disappear.

Lons <- cbind(df$Lon, df$LonEnd)
Lats <- cbind(df$Lat, df$LatEnd)

map <- ggmap(prep, 
    size = c(100, 200),
    extent='device', 
    darken = 0.5,
    legend = "bottom",
    base_layer = ggplot(data = df[1,], aes(x = Lon, y = Lat))) 


p <- map + geom_path(aes(x = Lons[10,], y = Lats[10,]),
                arrow = arrow(length = unit(0.2,"cm"), angle = 15), size = 0.3, colour = "white")

for(i in 2:nrow(Lats)){
        p <- p + geom_path(aes(x = Lons[i,], y = Lats[i,]),
        arrow = arrow(length = unit(0.2,"cm"), angle = 15), size = 0.3, colour = "white") 
                } #i

Any help would be most appreciated...

Fumigator answered 11/9, 2013 at 17:33 Comment(0)
A
7

Here is a solution achieved by adding the ends="first" option to your arrow() call. Also, it is necessary to then reverse the the segment direction by reassigning x to xend, etc.

Credit goes to google and https://collab.firelab.org/svn/big-butte/forecast_tools/plotObsVectors.R

Your original code using geom_segment works fine when used in a standard ggplot plot, so I presume that ggmap is doing something unintended with the arrows and segments. It may be worth notifying the authors.

library(grid) # provides `arrow` function.

p0 <- ggplot() +
      geom_segment(data=df, aes(x=Lon, y=Lat, xend=LonEnd, yend=LatEnd),
          arrow = arrow())

map <- ggmap(prep, size=c(100, 200), extent="device", darken=0.5, 
             legend="bottom") 

p1 <- map + 
     geom_segment(data=df, aes(x=LonEnd, y=LatEnd, xend=Lon, yend=Lat), 
         arrow=arrow(ends="first"), colour="white") 

library(gridExtra)
ggsave(arrangeGrob(p0, p1, nrow=1), file="plots.png", 
    width=12, height=6, dpi=150)

enter image description here

Anglicanism answered 11/9, 2013 at 23:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.