R - 图中线交点的坐标

标签 r ggplot2 plot coordinates

的结构
数据以下是

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")

enter image description here

一些线相交,但这些交点可能不在数据框中。是否有可能找出交叉点的坐标?

最佳答案

如果将数据设为 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')

enter image description here

我们看到只有 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')


给我们这个情节:

enter image description here

关于R - 图中线交点的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59613207/

相关文章:

r - 将 decorate_annotation 添加到 ComplexHeatmap 包中的垂直连接扰乱图

r - ggplot2 - R - 如何修复图例中气泡的大小

python - 如何使用箱线图绘制relplot,然后仅绘制seaborn中指定的频率

matlab - 与图上的移动线同步播放音频文件,MATLAB

r - 使用 tryCatch 保存错误回溯

regex - R grep匹配点

r - 如何避免在散点图/ggplot 中具有相同数据点的标签重叠?

r - 如何使用 facet_grid 在 ggplot2 中显示不同次数的多项式拟合?

r - 调整水平图例中文本之间的间距

r - R中随机森林的并行执行