地图上的线连接到Longitud 0 Latitud 0

时间:2017-01-03 21:02:35

标签: r plot plotly

我想用剧情创建一个飞行路径图。当遵循名为' Lines on Maps'的情节教程时,我没有得到预期的输出。虽然确实绘制了所有飞行路径,但由于某种原因,许多线似乎连接到原点(经度== 0,纬度== 0)。 Picture showing the problem here。有什么问题?

library(plotly)
library(dplyr)
# airport locations
air <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv')
# flights between airports
flights <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_aa_flight_paths.csv')
flights$id <- seq_len(nrow(flights))

# map projection
geo <- list(
  scope = 'world',
  projection = list(type = 'azimuthal equal area'),
  showland = TRUE,
  landcolor = toRGB("gray95"),
  countrycolor = toRGB("gray80")
)

p <- plot_geo(locationmode = 'USA-states', color = I("red")) %>%
  add_markers(
    data = air, x = ~long, y = ~lat, text = ~airport,
    size = ~cnt, hoverinfo = "text", alpha = 0.5
  ) %>%
  add_segments(
    data = group_by(flights, id),
    x = ~start_lon, xend = ~end_lon,
    y = ~start_lat, yend = ~end_lat,
    alpha = 0.3, size = I(1), hoverinfo = "none"
  ) %>%
  layout(
    title = 'Feb. 2011 American Airline flight paths<br>(Hover for airport names)',
    geo = geo, showlegend = FALSE, height=800
  )

ggplotly(p)

1 个答案:

答案 0 :(得分:1)

您可以将split参数与id变量一起使用,以停止在data.frame的每一行之间绘图:

  add_segments(
    data = group_by(flights, id),
    x = ~start_lon, xend = ~end_lon,
    y = ~start_lat, yend = ~end_lat, split=~id,
    alpha = 0.3, size = I(1), hoverinfo = "none"
  ) %>%

enter image description here