的结构
数据以下是
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")
我建立了一个情节并收到以下图像。
dplyr::bind_rows(df1 = df1, df2 = df2, df3 = df3, .id = "id") %>%
ggplot() + aes(V2, V1, color = id) +
geom_line() +
theme(legend.position = "bottom")
一些线相交,但这些交点可能不在数据框中。是否有可能找出交叉点的坐标?
最佳答案
如果将数据设为 sf 对象,则可以找到坐标,并将其视为空间数据。
添加到您发布的代码中:
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...
现在有三行,每行代表原始 df 之一。
使用 geom_sf 显示它仍然相同的图:
ggplot(df4_sf) + geom_sf(aes(color = id)) + theme(legend.position = 'bottom')
我们看到只有 2 和 3 相交,所以我们只看那两个。
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 - 图中线交点的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59613207/