我正在用飞行路径/线路绘制一些地图,我最近更新了我正在使用的java版本之类的东西,我得到了以下地图......你可以说它是正确的。每个点都连接到大西洋(0,0)某处的第二个点。 Plotly版本4.5.6和4.5.2。
library(plotly)
lat1 <- runif(10, 40,50)
lat2 <- runif(10, 40,50)
long1 <- runif(10, 40,50)
long2 <- runif(10, 40,50)
df <- data.frame(lat1, lat2, long1, long2)
plot_geo(locationmode = 'USA-states') %>%
add_markers(data = df, x = ~long1, y = ~lat1, color = I("red")) %>%
add_markers(data = df, x = ~long2, y = ~lat2, color = I("blue")) %>%
add_segments(
data = df,name = "Paths",
x = ~long1, xend = ~long2,
y = ~lat1, yend = ~lat2,
alpha = 0.3, size = I(3), hoverinfo = 'none'
)
这给出了以下图表......所有段彼此连接并且与纬度/长度0,0相连。任何人都可以帮我找出原因吗?
答案 0 :(得分:2)
您需要使用split
拆分细分。您可以在每行上使用ID,如下所示:
df <- data.frame(id=1:10,lat1, lat2, long1, long2)
plot_geo(locationmode = 'USA-states') %>%
add_markers(data = df, x = ~long1, y = ~lat1, color = I("red")) %>%
add_markers(data = df, x = ~long2, y = ~lat2, color = I("blue")) %>%
add_segments(
data = df,name = "Paths",
x = ~long1, xend = ~long2,
y = ~lat1, yend = ~lat2, split=~id,
alpha = 0.3, size = I(3), hoverinfo = 'none'
)