【问题标题】:Lines on Maps Connecting to Longitud 0 Latitud 0地图上连接经度 0 纬度 0 的线
【发布时间】:2017-01-03 21:02:35
【问题描述】:

我想使用 plotly 创建一个飞行路径图。遵循名为“地图上的线条”的情节教程时,我没有得到预期的输出。虽然确实绘制了所有飞行路径,但由于某种原因,许多线似乎连接到原点(经度==0,纬度==0)。 。怎么了?

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)

【问题讨论】:

  • 对我不起作用。我注意到add_paths 没有 x 和 xend 选项,也没有 y 和yend 选项,这与 add_segments 不同。

标签: r plot plotly


【解决方案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"
  ) %>%

【讨论】:

    猜你喜欢
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-26
    • 2016-11-11
    • 1970-01-01
    • 2018-01-05
    相关资源
    最近更新 更多