将一堆箭头添加到ggmap

时间:2013-09-11 17:33:03

标签: r ggplot2 ggmap

我正在绘制一张地图,该地图应该具有来自数据集的几个(> 400)箭头的叠加,该数据集具有每个箭头的开始和结束的纬度/经度对。以下是使用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")

之前我曾使用ggmap,因此这是我第一次尝试:

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

这产生了一个非常奇怪的结果 - 一些箭头的头部绘制方向错误。

然后我继续尝试使用geom_path代替。它校正了磁头的方向,但要求我分别输入数据集的每一行作为geom_path的输入。我做了一个for循环,但显然发生了什么,每次添加一个图层('我'加一个),所有以前的图层都会消失。

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

任何帮助都会非常感激......

1 个答案:

答案 0 :(得分:7)

通过在ends="first"电话中添加arrow()选项,可以获得以下解决方案。此外,有必要通过将x重新分配给xend等来反转分段方向。

积分转到Google和https://collab.firelab.org/svn/big-butte/forecast_tools/plotObsVectors.R

使用geom_segment的原始代码在标准ggplot图中使用时效果很好,因此我假设ggmap正在做一些无意中使用箭头和线段的内容。可能值得通知作者。

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