SymbolixAU / mapdeck

R interface to Deck.gl and Mapbox

Home Page:https://symbolixau.github.io/mapdeck/articles/mapdeck.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Reverse legend orientation

chrislim5 opened this issue · comments

Is it possible to reverse the order of the legend (highest values on top, lowest on bottom) without manually building a new legend? I tried adding legend_format = list(stroke_colour = rev), but that only changes values on the legend and not the colors.

commented

Using a manual legend is the only 'easy' way to do this. But it's not difficult once you know how colourvalues works

library(mapdeck)

set_token(secret::get_secret("MAPBOX"))

colours <- colourvalues::color_values( df$val, n_summaries = 5 )

df <- capitals
df$val <- rnorm(nrow(df))
df$colour <- colours$colours

l <- legend_element(
	variables = rev(colours$summary_values)
	, colours = rev(colours$summary_colours)
	, colour_type = "fill"
	, variable_type = "gradient"
)

mapdeck() %>%
	add_scatterplot(
		data = df
		, fill_colour = "colour"
		, tooltip = "val"
		, radius = 100000
		, legend = mapdeck::mapdeck_legend(l)
	)

Screen Shot 2020-10-06 at 12 08 00 pm

Great, thanks! That helped a lot. If I want to use a reversed color palette with this method, what is the best way to do this? Previously I had been reversing the palette using palette = colourvalues::get_palette("inferno")[256:1, ] when adding a layer. This seems to work:

colors <- colourvalues::color_values(data$percentile, palette = "inferno", n_summaries = 5)

data$colour <- colors$colours

leg <- 
  legend_element(
    variables = rev(colors$summary_values), 
    colours = colors$summary_colours, 
    colour_type = "stroke", 
    variable_type = "gradient"
  )

mapdeck(style = mapdeck_style("light"), zoom = 11)  %>%  
  add_path(data = data, 
           stroke_colour = "percentile",
           stroke_width = 5,
           palette = colourvalues::get_palette("inferno")[256:1,],
           legend = mapdeck_legend(leg),
           tooltip = "label_text",
           layer_id = "path_layer")

Is there a way to reverse the palette when creating the colors in color_values()?

commented

yeah, supply the reversed palette to the palette argument

colourvalues::colour_values(
  x = 1:20
  , palette = colourvalues::get_palette("inferno")[256:1, ]
)