wilkox / treemapify

🌳 Draw treemaps in ggplot2
http://wilkox.org/treemapify
214 stars 18 forks source link

Error running treemapify with subgroups #38

Closed jonathan-g closed 4 years ago

jonathan-g commented 4 years ago

See the following reprex: If I render the data frame with treemapify without subgroups it works fine, but if I render it with a subgroup, I get a very unclear error message "Error:imust have one dimension, not 2."

library(tidyverse)
library(treemapify)

nations <- tibble(country=c('USA','Japan','EU','Canada','Australia',
                            'Other Enthusiastic', 'Brazil', 'China','India',
                            'Indonesia','Other Reluctant',
                            'Small Islands', 'Other Vulnerable',
                            'Russia', 'Other Exporters'),
                  type = c(rep_len('Enthusiastic', 6), rep_len('Reluctant',5),
                            rep_len('Vulnerable',2), rep_len('Exporters',2)),
                  emissions = c(17.3, 3.9, 14.2, 2.0, 1.7, 0.6,
                                5.10001, 20.5, 5.4, 1.7, 5.1,
                                0.9, 7.3,
                                6.1, 8.1))

nations <- nations %>%
  mutate(type = ordered(type, levels = c('Enthusiastic', 'Reluctant', 
                                         'Exporters', 'Vulnerable')),
         country = ordered(country, levels = country)) %>%
  arrange(type, desc(emissions), country)

treemapify(nations, area = "emissions")
#> # A tibble: 15 x 6
#>    country            type          ymax  ymin  xmin  xmax
#>    <ord>              <ord>        <dbl> <dbl> <dbl> <dbl>
#>  1 China              Reluctant    0.542 0     0     0.378
#>  2 USA                Enthusiastic 1     0.542 0     0.378
#>  3 EU                 Enthusiastic 0.359 0     0.378 0.774
#>  4 Other Exporters    Exporters    0.359 0     0.774 1    
#>  5 Other Vulnerable   Vulnerable   0.575 0.359 0.378 0.717
#>  6 Russia             Exporters    0.575 0.359 0.717 1    
#>  7 India              Reluctant    0.794 0.575 0.378 0.626
#>  8 Brazil             Reluctant    1     0.794 0.378 0.626
#>  9 Other Reluctant    Reluctant    0.816 0.575 0.626 0.838
#> 10 Japan              Enthusiastic 0.816 0.575 0.838 1    
#> 11 Canada             Enthusiastic 1     0.816 0.626 0.734
#> 12 Australia          Enthusiastic 0.908 0.816 0.734 0.919
#> 13 Indonesia          Reluctant    1     0.908 0.734 0.919
#> 14 Small Islands      Vulnerable   0.926 0.816 0.919 1    
#> 15 Other Enthusiastic Enthusiastic 1     0.926 0.919 1

treemapify(nations, area = "emissions", subgroup = "type")
#> Error: `i` must have one dimension, not 2.

Created on 2020-04-13 by the reprex package (v0.3.0)

Here is my session info. I get the same error with the CRAN version, but I installed the latest GitHub version to be sure the bug hadn't been fixed in the development verion.

> sessionInfo() 
R version 3.6.3 (2020-02-29) 
Platform: x86_64-w64-mingw32/x64 (64-bit) 
Running under: Windows 10 x64 (build 18363)  

Matrix products: default  

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

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

other attached packages:
 [1] forcats_0.5.0         stringr_1.4.0         dplyr_0.8.5          
 [4] purrr_0.3.3           readr_1.3.1           tidyr_1.0.2          
 [7] tibble_3.0.0          ggplot2_3.3.0         tidyverse_1.3.0      
[10] treemapify_2.5.3.9001 reprex_0.3.0         

loaded via a namespace (and not attached):
 [1] tidyselect_1.0.0 xfun_0.12        haven_2.2.0      lattice_0.20-41 
 [5] colorspace_1.4-1 vctrs_0.2.4      generics_0.0.2   htmltools_0.4.0 
 [9] rlang_0.4.5      pillar_1.4.3     withr_2.1.2      glue_1.4.0      
[13] DBI_1.1.0        dbplyr_1.4.2     readxl_1.3.1     modelr_0.1.6    
[17] lifecycle_0.2.0  ggfittext_0.8.1  cellranger_1.1.0 munsell_0.5.0   
[21] gtable_0.3.0     rvest_0.3.5      evaluate_0.14    knitr_1.28      
[25] callr_3.4.3      ps_1.3.2         fansi_0.4.1      broom_0.5.5     
[29] Rcpp_1.0.4.6     clipr_0.7.0      backports_1.1.6  scales_1.1.0    
[33] jsonlite_1.6.1   fs_1.4.1         hms_0.5.3        packrat_0.5.0   
[37] digest_0.6.25    stringi_1.4.6    processx_3.4.2   grid_3.6.3      
[41] cli_2.0.2        tools_3.6.3      magrittr_1.5     crayon_1.3.4    
[45] whisker_0.4      pkgconfig_2.0.3  ellipsis_0.3.0   xml2_1.3.1      
[49] lubridate_1.7.8  httr_1.4.1       assertthat_0.2.1 rmarkdown_2.1.2 
[53] rstudioapi_0.11  R6_2.4.1         nlme_3.1-145     compiler_3.6.3 
wilkox commented 4 years ago

Thanks for the report. I'm afraid I'm a bit stumped by this one – I don't get this error on my computer with the same version of R and of the package:

library(tidyverse)
library(treemapify)

nations <- tibble(country=c('USA','Japan','EU','Canada','Australia',
                            'Other Enthusiastic', 'Brazil', 'China','India',
                            'Indonesia','Other Reluctant',
                            'Small Islands', 'Other Vulnerable',
                            'Russia', 'Other Exporters'),
                  type = c(rep_len('Enthusiastic', 6), rep_len('Reluctant',5),
                            rep_len('Vulnerable',2), rep_len('Exporters',2)),
                  emissions = c(17.3, 3.9, 14.2, 2.0, 1.7, 0.6,
                                5.10001, 20.5, 5.4, 1.7, 5.1,
                                0.9, 7.3,
                                6.1, 8.1))

nations <- nations %>%
  mutate(type = ordered(type, levels = c('Enthusiastic', 'Reluctant', 
                                         'Exporters', 'Vulnerable')),
         country = ordered(country, levels = country)) %>%
  arrange(type, desc(emissions), country)

treemapify(nations, area = "emissions")
#> # A tibble: 15 x 6
#>    country            type          ymax  ymin  xmin  xmax
#>    <ord>              <ord>        <dbl> <dbl> <dbl> <dbl>
#>  1 China              Reluctant    0.542 0     0     0.378
#>  2 USA                Enthusiastic 1     0.542 0     0.378
#>  3 EU                 Enthusiastic 0.359 0     0.378 0.774
#>  4 Other Exporters    Exporters    0.359 0     0.774 1    
#>  5 Other Vulnerable   Vulnerable   0.575 0.359 0.378 0.717
#>  6 Russia             Exporters    0.575 0.359 0.717 1    
#>  7 India              Reluctant    0.794 0.575 0.378 0.626
#>  8 Brazil             Reluctant    1     0.794 0.378 0.626
#>  9 Other Reluctant    Reluctant    0.816 0.575 0.626 0.838
#> 10 Japan              Enthusiastic 0.816 0.575 0.838 1    
#> 11 Canada             Enthusiastic 1     0.816 0.626 0.734
#> 12 Australia          Enthusiastic 0.908 0.816 0.734 0.919
#> 13 Indonesia          Reluctant    1     0.908 0.734 0.919
#> 14 Small Islands      Vulnerable   0.926 0.816 0.919 1    
#> 15 Other Enthusiastic Enthusiastic 1     0.926 0.919 1

treemapify(nations, area = "emissions", subgroup = "type")
#> # A tibble: 15 x 6
#>    country            type          ymax  ymin  xmin  xmax
#>    <ord>              <ord>        <dbl> <dbl> <dbl> <dbl>
#>  1 USA                Enthusiastic 0.512 0     0     0.338
#>  2 EU                 Enthusiastic 0.325 0     0.338 0.776
#>  3 Japan              Enthusiastic 0.512 0.325 0.338 0.546
#>  4 Canada             Enthusiastic 0.512 0.325 0.546 0.653
#>  5 Australia          Enthusiastic 0.463 0.325 0.653 0.776
#>  6 Other Enthusiastic Enthusiastic 0.512 0.463 0.653 0.776
#>  7 China              Reluctant    1     0.512 0     0.421
#>  8 India              Reluctant    0.808 0.512 0.421 0.603
#>  9 Brazil             Reluctant    0.808 0.512 0.603 0.776
#> 10 Other Reluctant    Reluctant    1     0.808 0.421 0.687
#> 11 Indonesia          Reluctant    1     0.808 0.687 0.776
#> 12 Other Exporters    Exporters    0.362 0     0.776 1    
#> 13 Russia             Exporters    0.634 0.362 0.776 1    
#> 14 Other Vulnerable   Vulnerable   0.960 0.634 0.776 1    
#> 15 Small Islands      Vulnerable   1.00  0.960 0.776 1

sessionInfo()
#> R version 3.6.3 (2020-02-29)
#> Platform: x86_64-apple-darwin19.3.0 (64-bit)
#> Running under: macOS Catalina 10.15.4
#> 
#> Matrix products: default
#> BLAS/LAPACK: /usr/local/Cellar/openblas/0.3.9/lib/libopenblasp-r0.3.9.dylib
#> 
#> locale:
#> [1] en_AU.UTF-8/en_AU.UTF-8/en_AU.UTF-8/C/en_AU.UTF-8/en_AU.UTF-8
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#>  [1] treemapify_2.5.3.9001 forcats_0.5.0         stringr_1.4.0        
#>  [4] dplyr_0.8.5           purrr_0.3.3           readr_1.3.1          
#>  [7] tidyr_1.0.2           tibble_3.0.1          ggplot2_3.3.0        
#> [10] tidyverse_1.3.0       nvimcom_0.9-83       
#> 
#> loaded via a namespace (and not attached):
#>  [1] tidyselect_1.0.0     xfun_0.12            haven_2.2.0         
#>  [4] lattice_0.20-38      colorspace_1.4-1     vctrs_0.2.4         
#>  [7] generics_0.0.2       htmltools_0.4.0      yaml_2.2.1          
#> [10] utf8_1.1.4           rlang_0.4.5          pillar_1.4.3        
#> [13] glue_1.4.0           withr_2.2.0          DBI_1.1.0           
#> [16] dbplyr_1.4.2         modelr_0.1.6         readxl_1.3.1        
#> [19] lifecycle_0.2.0      ggfittext_0.8.1.9002 munsell_0.5.0       
#> [22] gtable_0.3.0         cellranger_1.1.0     rvest_0.3.5         
#> [25] evaluate_0.14        knitr_1.28           fansi_0.4.1         
#> [28] highr_0.8            broom_0.5.5          Rcpp_1.0.4.6        
#> [31] scales_1.1.0         backports_1.1.6      jsonlite_1.6.1      
#> [34] fs_1.3.2             hms_0.5.3            digest_0.6.25       
#> [37] stringi_1.4.6        grid_3.6.3           cli_2.0.2           
#> [40] tools_3.6.3          magrittr_1.5         crayon_1.3.4        
#> [43] pkgconfig_2.0.3      ellipsis_0.3.0       xml2_1.2.2          
#> [46] reprex_0.3.0         lubridate_1.7.4      assertthat_0.2.1    
#> [49] rmarkdown_2.1        httr_1.4.1           R6_2.4.1            
#> [52] nlme_3.1-144         compiler_3.6.3

Created on 2020-04-25 by the reprex package (v0.3.0)

As you pointed out the error is cryptic so I don't even know where to start looking for a potential cause. I assume it's a subsetting operation somewhere. Have you had any progress on this since you first posted it?

jonathan-g commented 4 years ago

I've upgraded to R 4.0.0 and I no longer get this error. I will close the issue since it is no longer a problem for me.