plot.new()中的错误:Rmarkdown中的图边距太大

时间:2018-12-18 14:34:08

标签: r r-markdown

我在Rmarkdown中遇到一个奇怪的问题。如果下面的代码仅需要绘制3-4个数据集(经过测试),但只要将数据集的数量增加到9个或更多,就可以完美地工作。我收到一个奇怪的错误。

代码:

```{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)
      }
```

我得到的错误是

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

第48行是R代码第二个块的开头。

任何人都可以帮助我解决此错误。

我也看到了其他帖子

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

我已经在控制台和rstudio中执行了代码。

非常感谢。

0 个答案:

没有答案