R-图中线的交点坐标 [英] R - Coordinates of lines intersections in a plot

查看:627
本文介绍了R-图中线的交点坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


数据的结构如下

df1 <- structure(list(V2 = 1:10, V1 = c(1.4, 1.5, 1.9, 4.5, 6.7, 7.8, 
8.1, 8.2, 8.3, 8.9)), class = "data.frame", row.names = c(NA, -10L))

df2 <- structure(list(V2 = 1:10, V1 = c(1.43390152077191, 2.30610947613604, 
2.23775280718692, 5.41628585802391, 7.05710641788319, 8.77536501311697, 
8.48437852263451, 8.68867353517562, 8.7907762312796, 8.91225462416187
)), row.names = c(NA, -10L), class = "data.frame")

df3 <- structure(list(V2 = 1:10, V1 = c(2.04147320063785, 2.01257497165352, 
2.22035211822949, 5.08143315766938, 7.31734440829605, 8.23827453767881, 
8.27036898061633, 8.91508049662225, 9.04778654868715, 9.74391470812261
)), row.names = c(NA, -10L), class = "data.frame")

I构建图并接收以下图像。

I build a plot and receive the following image.

dplyr::bind_rows(df1 = df1, df2 = df2, df3 = df3, .id = "id") %>%
  ggplot() +  aes(V2, V1, color = id) + 
  geom_line() + 
  theme(legend.position = "bottom")

某些线相交,但这些相交可能不在数据框中。

Some of the lines intersect, but those intersections are probably not in the dataframes. Is it possible to find out the coordinates of intersections?

推荐答案

如果将数据设为sf对象,则可以找到坐标。 ,并将其视为空间数据。

You can find the coordinates if you to make the data an sf object, and treat it as spatial data.

添加到您发布的代码中:

Adding on to the code you posted:

library(sf)

df4 <- dplyr::bind_rows(df1 = df1, df2 = df2, df3 = df3, .id = "id")

df4_sf <- df4 %>%
  st_as_sf(coords = c('V2', 'V1')) %>%
  group_by(id) %>% 
  summarise(zz = 1) %>%  ## I'm not sure this line is needed.
  st_cast('LINESTRING')

# > df4_sf
# Simple feature collection with 3 features and 2 fields
# geometry type:  LINESTRING
# dimension:      XY
# bbox:           xmin: 1 ymin: 1.4 xmax: 10 ymax: 9.743915
# epsg (SRID):    NA
# proj4string:    NA
# # A tibble: 3 x 3
# id       zz                                                                                  geometry
# * <chr> <dbl>                                                                              <LINESTRING>
# 1 df1       1                   (1 1.4, 2 1.5, 3 1.9, 4 4.5, 5 6.7, 6 7.8, 7 8.1, 8 8.2, 9 8.3, 10 8.9)
# 2 df2       1 (1 1.433902, 2 2.306109, 3 2.237753, 4 5.416286, 5 7.057106, 6 8.775365, 7 8.484379, 8...
# 3 df3       1 (1 2.041473, 2 2.012575, 3 2.220352, 4 5.081433, 5 7.317344, 6 8.238275, 7 8.270369, 8...

现在有三行,每行代表其中一个

Now there are three rows, each representing one of the original df's.

使用geom_sf进行的绘图显示仍然相同:

A plot using geom_sf showing that it's still the same:

 ggplot(df4_sf) + geom_sf(aes(color = id)) + theme(legend.position = 'bottom')

我们看到只有2& 3相交,所以我们来看

We see that only 2 & 3 intersect, so we'll look at just those two.

intersections <- st_intersections(df4_sf[2,], df4_sf[3,])
st_coordinates(intersections)

#            X        Y L1
#[1,] 1.674251 2.021989  1
#[2,] 4.562692 6.339562  1
#[3,] 5.326387 7.617924  1
#[4,] 7.485925 8.583651  1

最后绘制所有内容

ggplot() +
  geom_sf(data = df4_sf, aes(color = id)) + 
  geom_sf(data = intersections) +
  theme(legend.position = 'bottom')

为我们提供以下情节:

这篇关于R-图中线的交点坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆