SymbolixAU / mapdeck

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

Hexagon layer elevation #306

Closed wolfeclw closed 4 years ago

wolfeclw commented 4 years ago

I am trying to add a hexagon layer and control the height of the hexagons based on the value of a column in my data frame. The elevation = argument in add_hexagon and add_pointcloud seem to behave differently. I understand that the hexagon height is based on the density of points, but I thought that assigning a value to elevation would override this?

I am working with air pollution data from a mobile monitoring study. What I would like to do is create something like a spatial bar chart where every coordinate is a column and the height (and color) represent the value of the air pollution measurement. Essentially, I would like to create a layer that behaves like a pointcloud, but each coordinate is a column.

Below is an example of the different behavior. I expected the hexagon layer to behave like the pointcloud layer, but each capital is the same height and shown as a column.

df <- capitals
df$z <- sample(10000:10000000, size = nrow(df))

mapdeck(style = mapdeck_style("dark"), pitch = 45) %>%
  add_pointcloud(
    data = df
    , lon = 'lon'
    , lat = 'lat'
    , elevation = 'z'
    , layer_id = 'point'
    , fill_colour = "country"
    , tooltip = 'country'
  )

mapdeck(style = mapdeck_style("streets"), pitch = 45) %>%
  add_hexagon(
    data = df
    , lat = "lat"
    , lon = "lon"
    , layer_id = "hex_layer"
    , elevation = "z",
    , radius = 10000
    , auto_highlight = TRUE,
  )

Session info:

R version 4.0.0 (2020-04-24)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.4

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices datasets  utils     methods   base     

other attached packages:
 [1] pufpR_0.0.1     mapdeck_0.3.3   forcats_0.5.0   stringr_1.4.0   dplyr_0.8.5     purrr_0.3.4    
 [7] readr_1.3.1     tidyr_1.1.0     tibble_3.0.1    ggplot2_3.3.0   tidyverse_1.3.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.4.6       lubridate_1.7.8    lattice_0.20-41    class_7.3-16       zoo_1.8-8         
 [6] assertthat_0.2.1   digest_0.6.25      packrat_0.5.0      R6_2.4.1           cellranger_1.1.0  
[11] backports_1.1.6    reprex_0.3.0       evaluate_0.14      e1071_1.7-3        httr_1.4.1        
[16] pillar_1.4.4       rlang_0.4.6        readxl_1.3.1       rstudioapi_0.11    geosphere_1.5-10  
[21] pkgcond_0.1.0      rmarkdown_2.1      htmlwidgets_1.5.1  munsell_0.5.0      broom_0.5.6       
[26] compiler_4.0.0     modelr_0.1.7       xfun_0.13          colourvalues_0.3.5 pkgconfig_2.0.3   
[31] htmltools_0.4.0    tidyselect_1.1.0   fansi_0.4.1        crayon_1.3.4       dbplyr_1.4.3      
[36] withr_2.2.0        sf_0.9-3           grid_4.0.0         jsonify_1.1.1      nlme_3.1-147      
[41] jsonlite_1.6.1     gtable_0.3.0       lifecycle_0.2.0    DBI_1.1.0          magrittr_1.5      
[46] units_0.6-6        scales_1.1.1       KernSmooth_2.23-16 cli_2.0.2          stringi_1.4.6     
[51] renv_0.10.0        fs_1.4.1           sp_1.4-2           xml2_1.3.2         ellipsis_0.3.1    
[56] generics_0.0.2     vctrs_0.3.0        tools_4.0.0        glue_1.4.1         hms_0.5.3         
[61] yaml_2.2.1         colorspace_1.4-1   classInt_0.4-3     rvest_0.3.5        knitr_1.28        
[66] haven_2.2.0       

Thanks!

dcooley commented 4 years ago

would add_column() suite your needs, with a disk_resolution = 6 to give you hexagons?

df <- capitals
df$z <- sample(1000:10000, size = nrow(df))
mapdeck(style = mapdeck_style("dark"), pitch = 45) %>%
  add_column(
    data = df
    , lon = 'lon'
    , lat = 'lat'
    , elevation = "z"
    , disk_resolution = 6
    , layer_id = 'point'
    , fill_colour = "country"
    , tooltip = 'country'
  )
wolfeclw commented 4 years ago

Thats perfect! Sorry, I didn't see the add_column() function in the examples and hadn't looked deeper in the documentation yet. Thanks!