qfes / rdeck

Deck.gl widget for R
https://qfes.github.io/rdeck
MIT License
97 stars 0 forks source link

scale_color_power colour on map and legend do not agree #22

Closed MilesMcBain closed 3 years ago

MilesMcBain commented 3 years ago

image

For example, a purplish hex here has tooltip 3.18 which should be yellow, by the legend.

This is based on your radio tower map code.

    add_h3_hexagon_layer(
      data = hex_data,
      id = "demand",
      line_width_units = "pixels",
      get_line_width = 0.05,
      opacity = 0.5,
      get_fill_color = scale_color_power(
        vehicle_hours,
        palette = plasma(6),
        exponent = 0.11111111
        ),
      extruded = FALSE,
      pickable = TRUE,
      tooltip = TRUE
    )
MilesMcBain commented 3 years ago

Also I'm not really clear on how the breaks args works for scale_color_power/log. They don't seem to affect the legend.

anthonynorth commented 3 years ago

LinearGradient differs from values sampled from the scale. Error increases as number of palette colours decreases.

Screen captures below show the difference between LinearGradient (left) and scale sampling (right).

hex_data <- tibble(
  hexagon = get_res0()[1:7],
  vehicle_hours = c(3.18, 1.21, 27.29, 274.52, 1721.07, 7900.27, 29072.2),
  exponent = 0.11111111,
  vehicle_hours_pow = trans(vehicle_hours, exponent)
)

rdeck(map_style = NULL) %>%
  add_h3_hexagon_layer(
    data = hex_data,
    id = "demand",
    line_width_units = "pixels",
    get_line_width = 0.05,
    opacity = 0.5,
    get_fill_color = scale_color_power(
      vehicle_hours,
      palette = plasma(6),
      exponent = 0.11111111,
      n_ticks = 7,
      limits = c(0.01, 29072.2)
    ),
    extruded = FALSE,
    pickable = TRUE,
    tooltip = TRUE
  )

image

rdeck(map_style = NULL) %>%
  add_h3_hexagon_layer(
    data = hex_data,
    id = "demand",
    line_width_units = "pixels",
    get_line_width = 0.05,
    opacity = 0.5,
    get_fill_color = scale_color_power(
      vehicle_hours,
      palette = plasma(30),
      exponent = 0.11111111,
      n_ticks = 7,
      limits = c(0.01, 29072.2)
    ),
    extruded = FALSE,
    pickable = TRUE,
    tooltip = TRUE
  )

image

anthonynorth commented 3 years ago

Also I'm not really clear on how the breaks args works for scale_color_power/log. They don't seem to affect the legend.

Breaks creates a piecewise continuous scale and should've been transforming the colour gradient --- it was broken. Fixed in 7867634. Example piecewise sqrt scale, with 1 break:

hex_data <- tibble(
  hexagon = get_res0()[1:7],
  vehicle_hours = 1:7
)

rdeck(map_style = NULL) %>%
  add_h3_hexagon_layer(
    data = hex_data,
    id = "demand",
    line_width_units = "pixels",
    get_line_width = 0.05,
    opacity = 0.5,
    get_fill_color = scale_color_power(
      vehicle_hours,
      palette = c("#ff0000", "#dddddd", "#0000ff"),
      exponent = 0.5,
      breaks = 4,
      n_ticks = 7
    ),
    extruded = FALSE,
    pickable = TRUE,
    tooltip = TRUE
  )

image