SymbolixAU / mapdeck

R interface to Deck.gl and Mapbox
https://symbolixau.github.io/mapdeck/articles/mapdeck.html
363 stars 40 forks source link

Arc/animatedarc manual legend display #315

Closed l90tdr closed 4 years ago

l90tdr commented 4 years ago

Hi, how may I help?

Hi,

New with mapdeck and getting used to the awesome features here.

I have an issue with arc/animated arc legends. Arcs produced from the east and west dataset are coloured with one hex colour (see below code). I don't need any additional info from the points, thus I'd like to reproduce a legend similar to that of #301 where the legend highlights the two hex colours and their respective direction. Is that possible for arcs?

I tried creating this;

l1 <- legend_element(variables = c("Eastward", "Westward")
                     ,colours = c("#FFFFFF", "#00FF00")
                     ,colour_type = "fill"
                     ,variable_type = "category"
                     ,title = "Direction of Movement"
                     )

js <- mapdeck_legend(l1)
js

but it didn't work.

Note east and west have been separated so that the tilt argument works such that the arc direction avoids going over land. Below code works perfectly for visualisation, but all attempts at adding the legend didn't work.

mapdeck( token = key, style = 'mapbox://styles/mapbox/dark-v9', pitch = 0 ) %>%
  add_arc(
    data = east
    , layer_id = "east"
    , origin = c("start_lon", "start_lat")
    , destination = c("end_lon", "end_lat")
    , stroke_from = "#FFFFFF"
    , stroke_to = "#FFFFFF"
    , tilt = -30
          )  %>%
  add_arc( 
    data = west
           , layer_id = "west"
           , origin = c("start_lon", "start_lat")
           , destination = c("end_lon", "end_lat")
           , stroke_from = "#00FF00"
           , stroke_to = "#00FF00"
           , tilt = +30 
             )

Appreciate any insight and apologies in advance is this is an easy fix.

Cheers

dcooley commented 4 years ago

You need to include legend = js in one of the layers

l90tdr commented 4 years ago

Hi,

Ye I had already tried positioning legend = js in both layers as well as one or the other, but it still doesn't render it on the browser viewer. Sorry for omitting in the original post. Any ideas?

mapdeck( token = key, style = 'mapbox://styles/mapbox/dark-v9', pitch = 0 ) %>%
  add_arc(
    data = east
    , layer_id = "east"
    , origin = c("start_lon", "start_lat")
    , destination = c("end_lon", "end_lat")
    , stroke_from = "#FFFFFF"
    , stroke_to = "#FFFFFF"
    , tilt = -30
    , legend = js
           )  %>%
  add_arc( 
    data = west
           , layer_id = "west"
           , origin = c("start_lon", "start_lat")
           , destination = c("end_lon", "end_lat")
           , stroke_from = "#00FF00"
           , stroke_to = "#00FF00"
           , tilt = +30
           , legend = js
        )
dcooley commented 4 years ago

Does this example work for you

url <- 'https://raw.githubusercontent.com/plotly/datasets/master/2011_february_aa_flight_paths.csv'
flights <- read.csv(url)
flights$id <- seq_len(nrow(flights))
flights$stroke <- sample(1:3, size = nrow(flights), replace = T)
flights$info <- paste0("<b>",flights$airport1, " - ", flights$airport2, "</b>")

l1 <- legend_element(variables = c("Eastward", "Westward")
                     ,colours = c("#FFFFFF", "#00FF00")
                     ,colour_type = "fill"
                     ,variable_type = "category"
                     ,title = "Direction of Movement"
)

js <- mapdeck_legend(l1)
js

mapdeck( style = mapdeck_style("dark"), pitch = 45 ) %>%
  add_animated_arc(
    data = flights
    , layer_id = "arc_layer"
    , origin = c("start_lon", "start_lat")
    , destination = c("end_lon", "end_lat")
    , stroke_from = "#FFFFFF"
    , stroke_to = "#FFFFFF"
    , stroke_width = "stroke"
    , trail_length = 10
    , legend = js
  )
l90tdr commented 4 years ago

Rookie alert... Both manual legend examples work, just remember to scroll down on viewer if it's out of sight... Thanks dcooley for providing quick assistance! Closing

dcooley commented 4 years ago

hah - that's a new one!