ggtern breaks ggplots plot

Issue #13 new
Daniel Victoria created an issue

If I load ggtern after loading ggplot, my standard plots gets messed up and no axis or labels are shown.

# ggplot error when loading ggtern

library(tidyverse)

n <- 500
data <- data.frame(x = runif(n),
                   y = runif(n),
                   z = runif(n))

ggplot(data) +
  geom_point(aes(x, y, col = z))

# produces the expected plot

# now loading ggtern

library(ggtern)
#> Registered S3 methods overwritten by 'ggtern':
#>   method           from   
#>   grid.draw.ggplot ggplot2
#>   plot.ggplot      ggplot2
#>   print.ggplot     ggplot2
#> --
#> Remember to cite, run citation(package = 'ggtern') for further info.
#> --
#> 
#> Attaching package: 'ggtern'
#> The following objects are masked from 'package:ggplot2':
#> 
#>     aes, annotate, ggplot, ggplot_build, ggplot_gtable, ggplotGrob,
#>     ggsave, layer_data, theme_bw, theme_classic, theme_dark,
#>     theme_gray, theme_light, theme_linedraw, theme_minimal, theme_void

ggplot(data) +
  geom_point(aes(x, y, col = z))

# Plot is messed up

sessionInfo()
#> R version 3.6.3 (2020-02-29)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 18.04.4 LTS
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
#> 
#> locale:
#>  [1] LC_CTYPE=pt_BR.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=pt_BR.UTF-8        LC_COLLATE=en_US.UTF-8    
#>  [5] LC_MONETARY=pt_BR.UTF-8    LC_MESSAGES=en_US.UTF-8   
#>  [7] LC_PAPER=pt_BR.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=pt_BR.UTF-8 LC_IDENTIFICATION=C       
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#>  [1] ggtern_3.3.0    forcats_0.5.0   stringr_1.4.0   dplyr_0.8.5    
#>  [5] purrr_0.3.4     readr_1.3.1     tidyr_1.0.2     tibble_3.0.1   
#>  [9] 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    
#>  [4] assertthat_0.2.1    digest_0.6.25       R6_2.4.1           
#>  [7] cellranger_1.1.0    plyr_1.8.6          backports_1.1.6    
#> [10] reprex_0.3.0        evaluate_0.14       httr_1.4.1         
#> [13] highr_0.8           pillar_1.4.3        rlang_0.4.5        
#> [16] compositions_1.40-5 readxl_1.3.1        rmarkdown_2.1      
#> [19] proto_1.0.0         labeling_0.3        munsell_0.5.0      
#> [22] broom_0.5.6         compiler_3.6.3      modelr_0.1.6       
#> [25] xfun_0.13           pkgconfig_2.0.3     htmltools_0.4.0    
#> [28] tidyselect_1.0.0    tensorA_0.36.1      gridExtra_2.3      
#> [31] fansi_0.4.1         crayon_1.3.4        dbplyr_1.4.3       
#> [34] withr_2.2.0         MASS_7.3-51.5       grid_3.6.3         
#> [37] nlme_3.1-144        bayesm_3.1-4        jsonlite_1.6.1     
#> [40] gtable_0.3.0        lifecycle_0.2.0     DBI_1.1.0          
#> [43] magrittr_1.5        scales_1.1.0        cli_2.0.2          
#> [46] stringi_1.4.6       farver_2.0.3        fs_1.4.1           
#> [49] robustbase_0.93-6   xml2_1.3.1          ellipsis_0.3.0     
#> [52] generics_0.0.2      vctrs_0.2.4         latex2exp_0.4.0    
#> [55] tools_3.6.3         glue_1.4.0          DEoptimR_1.0-8     
#> [58] hms_0.5.3           yaml_2.2.1          colorspace_1.4-1   
#> [61] rvest_0.3.5         knitr_1.28          haven_2.2.0

Comments (10)

  1. William Gearty

    This happens to me before I even do anything in RStudio. I boot up a brand new session and get the Registered S3 methods overwritten by 'ggtern' message (but not in RGui).

  2. Daniel Victoria reporter

    Right now I’m just postponing the use of `ggtern` to the end of my analysis or creating a separate script for the plots. Not exactly a workaround. But if this situation persists, I’ll try the ternary package.

  3. Grégoire Leleu

    Same issue here, not sure why ggtern needs to overwrite so much of the ggplot2 code.

    @William Gearty I have seen that when using markdown/reloading a saved workspace => having to redraw the plot/having to load the objects loads the packages silently and voila…

    forcing the right method by doing

    ggplot2:::print.ggplot(p)
    

    works, but it’s a pain.

  4. Grégoire Leleu

    A bit dirty but

    devtools::unload("ggtern")
    
    R.methodsS3::setMethodS3("print", "ggplot", ggplot2:::print.ggplot)
    R.methodsS3::setMethodS3("plot", "ggplot", ggplot2:::plot.ggplot)
    R.methodsS3::setMethodS3("grid.draw", "ggplot", ggplot2:::grid.draw.ggplot)
    

    Seems to undo the damage until this is fixed (haven’t tested with base R package unload).

    Still need to reapply your theme afterwards

  5. Mario Becerra

    Any solution? Grégoire’s solution is very much appreciated, but it’s not ideal. I am out of ideas now. 😞

  6. Benjamin LeRoy

    Thanks to the discussion above, @Shannon Gallagher and I ended up developing the following hack for our R package EpiCompare. Deep down we just over-wrote the print.ggplot function again using the code in the image below (for some reason I can’t create a code block). This code can be found in our package here. I can imagine - if you’re just using ggtern / ggplot for analytic purposes - that you build a super basic utility package that just contains this function.

  7. Emil OW Kirkegaard

    RStudio does some implicit loading, so even having ggtern code mentioned somewhere makes RStudio load it partially, and causes the ggplot2 axes to disappear. My solution is to move the ggtern code to a separate R notebook, which you compile separately from the other code.

  8. Log in to comment