tylermorganwall / rayshader

R Package for 2D and 3D mapping and data visualization
https://www.rayshader.com/
2.06k stars 214 forks source link

Continuous color palettes map incorrectly in `generate_polygon_overlay` #221

Closed SimonTopp closed 2 years ago

SimonTopp commented 2 years ago

Continuous color palettes map based on the order of the values in the sf dataframe rather than sequentially based on the value of the attribute provided in data_column_fill. This is problematic if your data column is quantitative. An easy fix might be just arranging the cropped sf polygon by the data column somewhere in the function.

Reproducible example

library(rayshader)
library(tidyverse)

water_palette = colorRampPalette(c("darkblue", "dodgerblue", "lightblue"))(200)
bathy_hs = height_shade(montereybay, texture = water_palette)

generate_polygon_overlay(monterey_counties_sf, palette = rainbow, 
                         extent = attr(montereybay,"extent"), data_column_fill = 'AWATER', heightmap = montereybay) %>%
  add_overlay(generate_altitude_overlay(bathy_hs, montereybay, start_transition = 0)) %>%
  plot_map()

generate_polygon_overlay(monterey_counties_sf %>% arrange(AWATER), palette = rainbow, 
                         extent = attr(montereybay,"extent"), data_column_fill = 'AWATER', heightmap = montereybay) %>%
  add_overlay(generate_altitude_overlay(bathy_hs, montereybay, start_transition = 0)) %>%
  plot_map()

Resulting maps image image

tylermorganwall commented 2 years ago

This bug was actually a little worse than that: It wasn't mapping the continuous palette at all, due to a 0-index bug resulting in basically random values being assigned. The behavior was supposed to be what you described. If you try the latest version (8790eabf2ed9187c8d500e9a77107b29dafb9d68), you should see the actual correct continuous mapping.

tylermorganwall commented 2 years ago

Also, thanks for the bug report!

SimonTopp commented 2 years ago

Thanks for developing and maintaining such a cool package!