0

I am having a strange problem within Rmarkdown. The following code works flawlessly if it has to plot for only 3-4 datasets (tested) but as soon the number of datasets increase to 9 or more. I get a strange error.

code:

```{r scRNALoadData, echo=FALSE, message=FALSE, warning=FALSE, include=TRUE, warnings=FALSE, eval=TRUE, results="asis"}
# Loading of libraries 
suppressPackageStartupMessages(library(scater))
suppressPackageStartupMessages(library(mvoutlier))
suppressPackageStartupMessages(library(Rtsne))
suppressPackageStartupMessages(library(limma))
suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(library(repr))
suppressPackageStartupMessages(library(cowplot))
suppressPackageStartupMessages(library(knitr))
suppressPackageStartupMessages(library(Matrix))
suppressPackageStartupMessages(library(rmarkdown))
suppressPackageStartupMessages(library(SingleCellExperiment))
suppressPackageStartupMessages(library(scran))
suppressPackageStartupMessages(library(scRNAseq))
#suppressPackageStartupMessages(library(iSEE))
suppressPackageStartupMessages(library(SCopeLoomR))
options(stringsAsFactors = FALSE)
# Loading expression data
loadSCE <- function(path){
  sce <- scater::read10XResults(path)
  #sce <- normalize(sce) # Data normalization based on scran
  mitochondrialGenes <- as.character(rowData(sce)[startsWith(rowData(sce)$symbol, "mt-"),]$id)
  isSpike(sce, "mt") <- rownames(sce) %in% mitochondrialGenes
  sce <- calculateQCMetrics(sce, 
                            feature_controls = list(
                              MT =  isSpike(sce, "mt")
                            ))
}
# Read-in expression sample scRNA matrix from directory
paths <- list.dirs(path = "/Abhi/test/test2/", recursive = FALSE)
for (i in 1:length(paths))
  assign(paste0("sce_",i), loadSCE(paths[i]))
sce=0
for (i in 1:length(paths))
  sce[i]<-print(noquote(paste0("sce_",i)))
t_list <- list()
t_list <- mget(ls(pattern="sce_\\d+"))
for(i in seq_along(t_list))
{
  metadata(t_list[[i]])["name"] <- paste0("iMates-",i)
}
```
&nbsp;&nbsp;

### Library size

``` {r scRNALibrarySize, echo=FALSE, message=FALSE, warning=FALSE, include=TRUE, warnings=FALSE, eval=TRUE, results="asis"}
      # Single Cell RNA plot library histogram
      plotLibrarySize <- function(sce, cutoffPoint){
        options(repr.plot.width=4, repr.plot.height=4)
        hist(
          sce$total_counts,
          breaks = 100
        )
        abline(v = cutoffPoint, col = "red")
      }
      # Single Cell RNA plot library box
      plotLibrarySize.Box <- function(sce, cutoffPoint){
        options(repr.plot.width=4, repr.plot.height=4)
        (sce$total_counts)
      }
      # Summary stats
      for (i in 1:length(paths))
        print(sum(get(sce[i])$total_counts))
      # Plot histograms library size
      l<-length(paths)
      ll<-round(l/2, digits=0)
      par(mfrow=c(ll,2))
      for  (i in 1:length(paths)) {
        plotLibrarySize(get(paste0("sce_",i)), 2500) }
      # Plot box library size
      l<-length(paths)
      par(mfrow=c(ll,2))
      for  (i in 1:length(paths)) {
        b<-plotLibrarySize.Box(get(paste0("sce_",i)),2500)
        boxplot(b)
      }
```

The error that I get is

Quitting from lines 48-79 (tq.Rmd) 
Error in plot.new() : figure margins too large
Calls: <Anonymous> ... hist -> hist.default -> plot -> plot.histogram -> plot.new
Execution halted

Where line 48 is beginning of the second block of R code.

Can anyone help me in fixing this error.

I have seen other posts as well

"Error in plot.new() : figure margins too large"

I have executed the code in both console and within rstudio.

Many thanks in advance.

Angelo
  • 4,829
  • 7
  • 35
  • 56
  • In general when you use `par(mfrow=c(...))` there are only so many plots you can pack into a rectangle of fixed size before the entire area will be consumed with margins and axes, leaving no room for the actual plot area. I would recommend either increasing the `repr.plot.width` and `repr.plot.height` or if this is still inadequate to split the plot into several plots each with just 3 or 4 panels. – qdread Dec 18 '18 at 14:56
  • @qdread: Thank you for replying. Could you please explain the splitting plot into several llots f each 3 or 4 panels fix with an example here. – Angelo Dec 18 '18 at 15:07
  • It's tough for me to do that without having a reproducible example of your code. I would first set `par=mfrow(c(2,2))` then I would replace the final `for` loop in your code with multiple loops, i.e. the first loop would be `for (i in 1:4)` then the second would be `for (i in 5:length(paths))` if you had 8 paths. – qdread Dec 18 '18 at 15:18

0 Answers0