2

我正在绘制一张地图,该地图应该覆盖数据集中的几个(> 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循环,但显然发生的事情是每次添加一个层('i'增加一个),所有以前的层都会消失。

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

非常感激任何的帮助...

4

1 回答 1

7

这是通过向您的呼叫添加ends="first"选项来实现的解决方案。arrow()此外,还需要通过重新分配等x来反转线段方向。xend

归功于谷歌和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)

在此处输入图像描述

于 2013-09-11T23:38:36.580 回答