用不同的颜色绘制add_segments

时间:2017-08-18 13:31:10

标签: r plotly

`dput(head(vert.cord))
structure(list(name = c("2994", "2996", "2997", "2999", "3000", 
"3001"), Latitude = c(23.077242, 19.862728, 19.088686, 23.287828, 
15.859286, 22.336164), Longitude = c(72.63465, 75.398114, 72.867919, 
69.670147, 74.618292, 73.226289), node.degree = c(18, 4, 86, 
2, 2, 4)), .Names = c("name", "Latitude", "Longitude", "node.degree"
), row.names = c(NA, 6L), class = "data.frame")`

`dput(head(Indian_new_routes))
structure(list(V1 = c("2994", "2994", "2994", "2994", "2994", 
"2994"), V2 = c("2997", "3007", "3017", "3043", "3093", "3098"
), weight = c(2653.62642910095, 861.186965956404, 1030.6558395446, 
3245.152405393, 4531.03569783294, 524.579803792743), dist = c(442.271071516825, 
861.186965956404, 515.327919772298, 1622.5762026965, 755.17261630549, 
524.579803792743), dist.inv = c(0.00226105676903165, 0.00116118803410992, 
0.00194051197622255, 0.000616303874257576, 0.001324200558135, 
0.00190628764731303), dist.inv.weight = c(0.000376842794838609, 
0.00116118803410992, 0.000970255988111277, 0.000308151937128788, 
0.0002207000930225, 0.00190628764731303), color = c("red", "red", 
"red", "red", "red", "red"), From.lat = c(23.077242, 23.077242, 
23.077242, 23.077242, 23.077242, 23.077242), From.lon = c(72.63465, 
72.63465, 72.63465, 72.63465, 72.63465, 72.63465), To.lat = c(19.088686, 
15.380833, 18.582111, 22.654739, 28.5665, 26.824192), To.lon = c(72.867919, 
73.831422, 73.919697, 88.446722, 77.103088, 75.812161)), .Names = c("V1", 
"V2", "weight", "dist", "dist.inv", "dist.inv.weight", "color", 
"From.lat", "From.lon", "To.lat", "To.lon"), row.names = c(NA, 
6L), class = "data.frame")`

我正在尝试创建一个带有节点的机场网络,因为某些城市的lat,long和edge连接着两个不同的城市。我可以得到网络,但我想要一些不同颜色的边缘。我在Indian_new_routes数据框中有一个颜色属性。但是生成的新地图显示了相同颜色的所有边缘。任何帮助将不胜感激。

geo <- list(
  scope = 'asia',
  projection = list(type = 'natural earth'),
  showland = TRUE,
  landcolor = toRGB("gray95"),
  countrycolor = toRGB("gray80")
  )
p <- plot_geo(locationmode = 'INDIA') %>%
  add_markers(
     data = vert.cord, x = ~Longitude, y = ~Latitude, text = ~name,
     size = ~node.degree, hoverinfo = "text", alpha = 0.5,  color = I("black")
  ) %>%

  add_segments(
    data = Indian_new_routes,
    x = ~From.lon, xend = ~To.lon,
    y = ~From.lat, yend = ~To.lat,
    alpha = 0.3, size = I(1), hoverinfo = "none", color = ~color
    ) %>%
  layout(
    title = 'Jan. 2015 Indian Domestic flight paths<br>(Hover for airport names)',
    geo = geo, showlegend = FALSE, height=800
    )

1 个答案:

答案 0 :(得分:2)

根据here给出的建议,一个可能的解决方案是:

library(ggmap)
library(dplyr)
library(plotly)  

vert.cord <- structure(list(name = c("2994", "2996", "2997", "2999", "3000", 
"3001"), Latitude = c(23.077242, 19.862728, 19.088686, 23.287828, 
15.859286, 22.336164), Longitude = c(72.63465, 75.398114, 72.867919, 
69.670147, 74.618292, 73.226289), node.degree = c(18, 4, 86, 
2, 2, 4)), .Names = c("name", "Latitude", "Longitude", "node.degree"
), row.names = c(NA, 6L), class = "data.frame")

Indian_new_routes <- structure(list(V1 = c("2994", "2994", "2994", "2994", "2994", 
"2994"), V2 = c("2997", "3007", "3017", "3043", "3093", "3098"
), weight = c(2653.62642910095, 861.186965956404, 1030.6558395446, 
3245.152405393, 4531.03569783294, 524.579803792743), dist = c(442.271071516825, 
861.186965956404, 515.327919772298, 1622.5762026965, 755.17261630549, 
524.579803792743), dist.inv = c(0.00226105676903165, 0.00116118803410992, 
0.00194051197622255, 0.000616303874257576, 0.001324200558135, 
0.00190628764731303), dist.inv.weight = c(0.000376842794838609, 
0.00116118803410992, 0.000970255988111277, 0.000308151937128788, 
0.0002207000930225, 0.00190628764731303), color = c("red", "red", 
"red", "red", "red", "red"), From.lat = c(23.077242, 23.077242, 
23.077242, 23.077242, 23.077242, 23.077242), From.lon = c(72.63465, 
72.63465, 72.63465, 72.63465, 72.63465, 72.63465), To.lat = c(19.088686, 
15.380833, 18.582111, 22.654739, 28.5665, 26.824192), To.lon = c(72.867919, 
73.831422, 73.919697, 88.446722, 77.103088, 75.812161)), .Names = c("V1", 
"V2", "weight", "dist", "dist.inv", "dist.inv.weight", "color", 
"From.lat", "From.lon", "To.lat", "To.lon"), row.names = c(NA, 
6L), class = "data.frame")

Indian_new_routes$color[c(1,3,5)] <- "blue"

geo <- list(
  scope = 'asia',
  projection = list(type = 'natural earth'),
  showland = TRUE,
  landcolor = toRGB("gray95"),
  countrycolor = toRGB("gray80")
  )
p <- plot_geo(locationmode = 'INDIA', height=800) %>%
  add_markers(
     data = vert.cord, x = ~Longitude, y = ~Latitude, text = ~name,
     size = ~node.degree, hoverinfo = "text", alpha = 0.5,  color = I("black")
  )  
for (k in 1:nrow(Indian_new_routes)) {
  p <- add_segments(p,
    data = Indian_new_routes[k,],inherit=FALSE, 
    x = ~From.lon, xend = ~To.lon,
    y = ~From.lat, yend = ~To.lat,
    alpha = 0.3, size = I(1), hoverinfo = "none", line=list(color=~color)
    ) 
}
p <- p %>%
  layout(
    title = 'Jan. 2015 Indian Domestic flight paths<br>(Hover for airport names)',
    geo = geo, showlegend = FALSE 
    )  
p

enter image description here