Search code examples
rplotgraph-visualization

ggalluvial (r package) example gives error


Am i missing something? the example in the ggalluvial package gives this error:

> library(ggalluvial)
> ggplot(as.data.frame(Titanic),
+        aes(weight = Freq,
+            axis1 = Class, axis2 = Sex, axis3 = Age,
+            fill = Survived)) +
+   geom_alluvium() +
+   scale_x_continuous(breaks = 1:3, labels = c("Class", "Sex", "Age"))
Error: Invalid column specification

UPDATE 2: as per DanHall's request:

sessionInfo()
R version 3.4.3 (2017-11-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_Canada.1252  LC_CTYPE=English_Canada.1252    LC_MONETARY=English_Canada.1252 LC_NUMERIC=C                   
[5] LC_TIME=English_Canada.1252    

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

other attached packages:
 [1] ggalluvial_0.6.0 ggthemes_3.4.0   alluvial_0.1-2   dplyr_0.5.0      purrr_0.2.2      readr_0.2.2      tidyr_0.6.1     
 [8] tibble_1.3.4     ggplot2_2.2.1    tidyverse_1.1.1 

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.14     compiler_3.4.3   plyr_1.8.4       base64enc_0.1-3  forcats_0.2.0    tools_3.4.3      digest_0.6.12   
 [8] evaluate_0.10.1  jsonlite_1.5     lubridate_1.5.6  gtable_0.2.0     nlme_3.1-128     lattice_0.20-33  rlang_0.1.4     
[15] psych_1.6.4      DBI_0.6          yaml_2.1.14      parallel_3.4.3   haven_1.0.0      stringr_1.2.0    httr_1.3.1      
[22] knitr_1.19       xml2_1.1.1       hms_0.3          rprojroot_1.2    grid_3.4.3       R6_2.2.2         readxl_0.1.1    
[29] rmarkdown_1.8    reshape2_1.4.2   modelr_0.1.0     magrittr_1.5     backports_1.1.1  htmltools_0.3.6  scales_0.5.0    
[36] rsconnect_0.8.5  assertthat_0.1   mnormt_1.5-4     rvest_0.3.2      colorspace_1.3-2 labeling_0.3     stringi_1.1.6   
[43] lazyeval_0.2.1   munsell_0.4.3    broom_0.4.1  

Solution

  • See below, this code works on another machine. When something that is working for other people isn't working for you, it can be useful to run update.packages() and follow the instructions to update any outdated packages you may have installed. This turned out to be the solution here.

    It works on my machine as is:

    ggplot(as.data.frame(Titanic),
          aes(weight = Freq, 
              axis1 = Class, axis2 = Sex, axis3 = Age,
              fill = Survived)) +
      geom_alluvium() +
        scale_x_continuous(breaks = 1:3, labels = c("Class", "Sex", "Age"))
    

    enter image description here

    It also works when calling example(geom_alluvium, package = "ggalluvial").

    Here's another usage example (from the vignette).

    ggplot(as.data.frame(Titanic),
           aes(weight = Freq,
               axis1 = Survived, axis2 = Sex, axis3 = Class)) +
      geom_alluvium(aes(fill = Class),
                    width = 0, knot.pos = 0, reverse = FALSE) +
      guides(fill = FALSE) +
      geom_stratum(width = 1/8, reverse = FALSE) +
      geom_text(stat = "stratum", label.strata = TRUE, reverse = FALSE) +
      scale_x_continuous(breaks = 1:3, labels = c("Survived", "Sex", "Class")) +
      coord_flip() +
      ggtitle("Titanic survival by class and sex")