gpt4 book ai didi

r - 向 ggmap 添加一堆箭头

转载 作者:行者123 更新时间:2023-12-01 21:24:44 27 4
gpt4 key购买 nike

我正在绘制一张 map ,该 map 应该覆盖来自数据集的多个(> 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

任何帮助将不胜感激......

最佳答案

这是通过将 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

关于r - 向 ggmap 添加一堆箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18747706/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com