Black Lives Matter. Please consider donating to Black Girls Code today.
Dash Enterprise delivers an incredible 21x cost savings 💸Download the e-book!

Flight Paths Map - For Other Countries?

I am wondering if it possible to create a map similar to the flight paths map for the U.S. for other countries? https://plot.ly/r/lines-on-maps/

Instead of having the U.S. as a background, I am interested in showing just Afghanistan, but I am not clear if plotly supports the other countries and how exactly to code it with options like “locationmode” and “scope”.

Hey @localh85,

In that example it shows North America as the scope. Thus, you could set scope to world (https://plot.ly/r/reference/#layout-geo-scope), center it, and scale accordingly. Something like:

p <- plot_geo(lat = c(34.5553,31.6349554), lon = c(69.2075,65.7151502)) %>%
  add_markers(color = I("red"), size = I(4)) %>%
  add_lines(color = I("red"), size = I(2)) %>%
  layout(
    title = 'Afghanistan',
    showlegend = FALSE,
    geo = list(
      resolution = 50,
      showframe = F,
      showland = TRUE,
      showlakes = TRUE,
      showcountries = TRUE,
      landcolor = toRGB("grey80"),
      countrycolor = 'black',
      lakecolor = toRGB("white"),
      projection = list(type = "equirectangular", scale = 2),
      coastlinewidth = 2,
      lataxis = list(
        range = c(20, 50),
        showgrid = F,
        tickmode = "linear",
        dtick = 10
      ),
      lonaxis = list(
        range = c(40, 90),
        showgrid = F,
        tickmode = "linear",
        dtick = 20
      )
    )
  ) 

You can achieve something similar with Mapbox too or, alternatively, you could create the boundaries yourself (with the correct data) using polygons. See here for an example https://plot.ly/r/county-level-choropleth/#creating-polygon-boundaries

Also here is an example using polygons https://plot.ly/~bdun9/2128/

library(plotly)
library(maps)
library(mapdata)

m <- map('worldHires', 'Afghanistan')

blank_layer <- list(
  title = "",
  showgrid = F,
  showticklabels = F,
  zeroline = F)

p <- plot_ly(
    x = m$x,
    y = m$y,
    name = 'Border',
    fillcolor = 'white',
    hoverinfo = "none") %>%
  add_polygons(
    line = list(color = 'black', width = 0.5)) %>%
  add_markers(
    y = c(34.5553,31.6349554), x = c(69.2075,65.7151502), marker = list(color = 'red'), name = 'location') %>%
  add_lines(
    y = c(34.5553,31.6349554), x = c(69.2075,65.7151502), line = list(color = "red"), name = 'path') %>%
  layout(
    xaxis = blank_layer,
    yaxis = blank_layer,
    title = 'Afghanistan')

Thank you for the different options to play with. With respect to the first example, I cannot seem to get it to work, it just draws the title.

For the second, that one is working and I am going to play with them some more to see what I can do. I really appreciate the help and the code examples!

No problems.

With the one that only shows you the title, can you open it in an external browser? Also, are you using the latest version of RStudio (or RStudio preview)? Possibly on a Windows machine?

Thanks for any info.

Ah it works if I save it as a html and open it up independent of my R studio