Te-Yo
Te-Yo

Reputation: 129

Seurat, interfering with ggplot2/plotly output

I am writing an R script in Rstudio looking at single cell data and generating various graphs. The package I am using is ggplot2. It generates nice graph outputs like this when the Seurat library is not loaded: Pure ggplot2 graph

Then when the Seurat library is imported, the graph reverts to this ugliness: Seurat Interfered Plot Here is a list of the imports that Seurat brings upon being included:

Imports: methods, ROCR, stringr, mixtools, lars, fastICA, tsne, Rtsne,
        fpc, ape, VGAM, pbapply, igraph, FNN, caret, plyr, dplyr,
        RColorBrewer, MASS, Matrix, irlba, reshape2, gridExtra, gplots,
        gdata, Rcpp, RcppEigen, RcppProgress, tclust, ranger

Any thoughts on how to have both libraries present without the alterations in the graph output?

Solutions Tried:

Upvotes: 2

Views: 768

Answers (1)

Brian
Brian

Reputation: 8305

A comment asked me to post my fix as an answer, so I did a little digging. When unexpected behavior happens after you call library(package), that's because that package has a function called .onLoad or .onAttach. These are automatically run upon loading or attaching the package, and are usually used to set options, print helpful messages, etc. Best practices say you should undo any actions with a complement function, .onUnload. See: https://r-pkgs.org/r.html#when-you-do-need-side-effects

Occasionally, packages do need side-effects. This is most common if your package talks to an external system — you might need to do some initial setup when the package loads. ... If you use .onLoad(), consider using .onUnload() to clean up any side effects. By convention, .onLoad() and friends are usually saved in a file called zzz.R.

So I went looking in the Github repo for the package Seurat, and didn't see any ggplot2-related options (https://github.com/satijalab/seurat/blob/master/R/zzz.R). Next up was to check that package's DESCRIPTION, which shows me the packages that are loaded that Seurat uses. It's a long list, but I recognized a somewhat common custom graphics package on there, cowplot. If you then go to that repo (https://github.com/wilkelab/cowplot/blob/master/R/setup.R):

.onAttach <- function(libname, pkgname) {
  # switch the default theme to theme_cowplot
  # ggplot2::theme_set(theme_cowplot())
  packageStartupMessage("\n********************************************************")
  packageStartupMessage("Note: As of version 1.0.0, cowplot does not change the")
  packageStartupMessage("  default ggplot2 theme anymore. To recover the previous")
  packageStartupMessage("  behavior, execute:\n  theme_set(theme_cowplot())")
  packageStartupMessage("********************************************************\n")
}

From these messages, you can see it used to be the case that loading cowplot changed the default theme for ggplot2. Out of curiosity I looked at the commits to see when this change happened at the source (although not when it was pushed to CRAN):

https://github.com/wilkelab/cowplot/commit/158ccdfa45d6c3e99c8568afaa73f7d68cd6c9c2#diff-c9a33000e506e95bca8089aa91efd8b2

clauswilke committed on Mar 29, 2018

So, this question is no longer reproducible, as of roughly a year ago. However, this hunting process may still be useful to future readers when unexpected behavior occurs.


TL;DR:

ggplot2-specific fix: The original default theme is theme_gray(), so you can always manually add that to affected plots to revert to the previous behavior, if you can't isolate the package affecting your code.

Upvotes: 1

Related Questions