向ggmap添加一堆箭头

编程入门 行业动态 更新时间:2024-10-25 18:23:30
本文介绍了向ggmap添加一堆箭头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在绘制一张地图,该地图应该有一个来自数据集的多个(> 400)箭头叠加,每个箭头的起始和结束都有经纬度对。这里是使用dput的数据子集:

df < - structure(list(Lat = c(49.34054,49.34068, 49.34104,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.34210 ),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 (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 center ,或爱丁堡 zoom = 17,地图类型='混合',#也混合/地形/路线图/卫星比例= 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,color =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,$ b $ (aes(x = Lons [b]),其中b是数据库中的数据, 10,],y = Lats [10,]), arrow =箭头(length =单位(0.2,cm),angle = 15),size = 0.3,color =white) (i = 2:nrow(Lats)){p < - p + geom_path(aes(x = Lons [i,],y = Lats [i,]), arrow =箭头(长度=单位(0.2,cm),角度= 15),size = 0.3,color =white)} #i

任何帮助都将得到最多赞赏...

解决方案

^ h ere是通过将 ends =first选项添加到箭头()调用中实现的解决方案。此外,有必要通过将 x 重新分配给 xend 等来反转分段方向。

积分归功于Google和 https ://collab.firelab/svn/big-butte/forecast_tools/plotObsVectors.R 您的原始代码使用 geom_segment 在标准 ggplot 图中使用时效果很好,所以我认为 ggmap 在做与箭头和分段无意识的东西。可能值得通知作者。

library(grid)#提供`arrow`函数。 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),color =white) library(gridExtra) ggsave(arrangeGrob(p0,p1,nrow = 1),file =plots.png , width = 12,height = 6,dpi = 150)

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...

解决方案

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 collab.firelab/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)

更多推荐

向ggmap添加一堆箭头

本文发布于:2023-11-11 21:18:23,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:箭头   ggmap

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!