使用 R 中的 ggplot2 将网格外的点绘制为指向数据的箭头

Plot points outside grid as arrows pointing to data with ggplot2 in R

我正在生成具有世界比例数据的地图,然后放大到某些区域。在放大视图中,我想通过放置从框中心指向数据点在外部世界的位置的箭头来显示边界框之外还有其他数据点。

注意: 我不需要它是 "great circle" 路径,只是墨卡托投影中的 XY 向量,因为我想这对 "normal" 情节也是如此。

例如,这是显示数据范围的世界地图:

这里是放大视图,手动添加了洋红色箭头以显示我想要生成的内容。

下面是我用来生成这两个基本图的代码和数据。我需要的是一种生成箭头的方法。

require(ggplot2)

te = structure(list(lat = c(33.7399, 32.8571, 50.2214, 36.96263, 33.5835, 
33.54557, 47.76147, 48, 59.40289, 35.93411, 32.87962, 38.3241, 
50.03844, 37.44, 50.07774, 50.26668, 36.5944), lng = c(-118.37608, 
-117.25746, -5.3865, -122.00809, -117.86159, -117.79805, -124.45055, 
-126, -146.35157, -122.931472, -117.25285, -123.07331, -5.26339, 
25.4, -5.709894, -3.86828, -121.96201)), .Names = c("lat", "lng"
), class = "data.frame", row.names = c(NA, -17L))

all_states = map_data("world")

# world version:
wp = ggplot() + 
      geom_polygon(data = all_states, aes(x = long, y = lat, group = group), colour = "gray",
                   fill = "gray") +
      coord_cartesian(ylim = c(0, 80), xlim = c(-155, 45)) + 
      geom_point(data = te, aes(x = lng, y = lat), color = "blue", size = 5,alpha = 0.6)

print(wp)

#states plot
sp = ggplot() +
      geom_polygon(data = all_states, aes(x = long, y = lat, group = group), colour = "gray", fill = "gray") +
      coord_cartesian(ylim = c(30, 52), xlim = c(-128, -114)) + 
      geom_point(data = te, aes(x = lng, y = lat), color = "blue", size = 5, alpha = 0.6) 

print(sp)

这是我的尝试。这是我得到的最接近的。我使用 gcIntermediate() 来计算美国地图中心点与位于 bbox 之外的数据点之间的最短距离。因此,箭头位置可能不是您想要的。我希望其他人会基于此尝试提供更好的解决方案。

我首先将你的 df(即 te)排列在美国缩放地图的中心点。然后我选择了不在美国地图 bbox 中的数据点。然后,添加两列以指示美国地图的中心点。重命名两列并使用 gcIntermediate 计算最短距离。

library(dplyr)
library(ggplot2)
library(geosphere)

filter(te, !between(lng, -128, -114) | !between(lat, 30, 52)) %>%
mutate(start_long = (-128 - 114) / 2,
       start_lat = (30 + 52) / 2) %>%
rename(end_lat = lat, end_long = lng) %>%
do(fortify(as(gcIntermediate(.[,c("start_long", "start_lat")],
                             .[,c("end_long", "end_lat")],
                             100,
                             breakAtDateLine = FALSE,
                             addStartEnd = TRUE,
                             sp = TRUE), "SpatialLinesDataFrame"))) -> foo

foo 包含 100 个数据点以绘制相应的线。我选择了靠近 bbox 边界的数据点。我专门为每一行寻找两个数据点,以便以后可以使用 geom_segment()。我承认我稍微玩了一下过滤条件。最后,在这种情况下,我没有使用 lat 对数据进行子集化。

filter(foo, between(long, -128, -126.5) | between(long, -115.5, -114)) %>%
group_by(group) %>%
slice(c(1,n())) -> mydf

下一步,我根据this link

重新排列了数据框
mutate(mydf, end_long = lag(long), end_lat = lag(lat)) %>%
slice(n()) -> mydf2

最后我画了带箭头的地图。我希望这会为您提供某种基础。也希望其他SO用户提供更好的解决方案

ggplot() +
geom_polygon(data = all_states, aes(x = long, y = lat, group = group),
             colour = "gray", fill = "gray" ) +
coord_cartesian(ylim = c(30, 52), xlim = c(-128,-114)) +
geom_point(data = te, aes(x = lng,y = lat), color = "blue", size = 5,alpha = 0.6) +
geom_segment(data = mydf2, aes(x = end_long, xend = long,
                               y = end_lat, yend = lat, group = group),
                               arrow = arrow(length = unit(0.2, "cm"), ends = "last"))

此解决方案使用 sprgeos 包来处理空间数据,主要症结在于相交线和框多边形以获得箭头的边缘点。然后,如果您使用 geom_segment 和零宽度绘制箭头,则线条不可见,只剩下箭头。

此函数计算线盒交点:

boxint <- function(xlim, ylim, xp, yp){
    ## build box as SpatialPolygons
    box = cbind(xlim[c(1,2,2,1,1)],
        ylim[c(1,1,2,2,1)])
    box <- sp::SpatialPolygons(list(sp::Polygons(list(sp::Polygon(box)),ID=1)))

    ## get centre of box
    x0=mean(xlim)
    y0=mean(ylim)

    ## construct line segments to points
    sl = sp::SpatialLines(
        lapply(1:length(xp),
               function(i){
                   sp::Lines(list(sp::Line(cbind(c(x0,xp[i]),c(y0,yp[i])))),ID=i)
               }
               )
        )
    ## intersect lines segments with boxes to make points
    pts = rgeos::gIntersection(sl, as(box, "SpatialLines"))
    as.data.frame(sp::coordinates(pts), row.names=1:length(xp))
}

还有这个returns带箭头的geom

wherelse <- function(xlim, ylim, points){
    ## get points outside bounding box
    outsides = points[!(
        points$lng>=xlim[1] &
            points$lng <= xlim[2] &
                points$lat >= ylim[1] &
                    points$lat <= ylim[2]),]
    npts = nrow(outsides)
    ## get centre point of box
    x = rep(mean(xlim),npts)
    y = rep(mean(ylim),npts)

    ## compute box-point intersections
    pts = boxint(xlim, ylim, outsides$lng, outsides$lat)
    pts$x0=x
    pts$y0=y
    ## create arrow segments as invisible lines with visible arrowheads
    ggplot2::geom_segment(data=pts, aes(x=x0,y=y0,xend=x,yend=y),
       lwd=0, arrow=grid::arrow(length=unit(0.5,"cm"),
       type="closed"),col="magenta")
}

所以你的例子,基本情节是:

sp = ggplot() + 
  geom_polygon(
   data=all_states, 
    aes(x=long, y=lat, group = group),colour="gray",fill="gray" ) + 
    coord_cartesian(ylim=c(30, 52), xlim=c(-128,-114)) + 
    geom_point(data=te,aes(x=lng,y=lat),color="blue",size=5,alpha=0.6)

然后添加箭头:

sp + wherelse(c(-128,-114), c(30,52), te)

不确定是否有一个选项可以完全按照您想要的方式绘制箭头!