仅在 R Markdown 中使用 ggplot2 时出现"Faceting variables must have at least one value"错误;这是什么意思?



我正在使用 for 循环来创建多个箱线图作为 R Markdown 报告的一部分。 我的大多数箱线图都打印成功,而其他箱线图由于某种原因无法打印,而是给我以下错误:"错误:分面变量必须至少有一个值"。

这是我的一个箱线图的数据,它给出了一个错误:

structure(list(target_id = c("ENST00000390304", "ENST00000390304",
"ENST00000390304", "ENST00000390304", "ENST00000390304", "ENST00000390304",
"ENST00000390304", "ENST00000390304", "ENST00000390304", "ENST00000390304",
"ENST00000390304", "ENST00000390304", "ENST00000390304", "ENST00000390304",
"ENST00000390304", "ENST00000390304", "ENST00000390304", "ENST00000390304",
"ENST00000390304", "ENST00000390304", "ENST00000390304"), tpm = c(0.0805275227988995,
0, 0, 0, 13.82501606306, 0, 0.899651016904527, 0, 3.97161098682224,
25.4470126325026, 14.1717230551304, 8.47283747191736, 11.9239827469873,
1.15292796701229, 4.89325150388971, 38.1766608035109, 42.117834900921,
5.04752697725949, 14.6501301179727, 2.31319103570497, 0.822267881088558
), condition = c("A", "A", "A", "B",
"B", "A", "B", "A", "C",
"D", "D", "C", "C", "C", "C", "C",
"D", "D", "D", "D", "C"), hgnc_symbol = c("IGLV3-27",
"IGLV3-27", "IGLV3-27", "IGLV3-27", "IGLV3-27", "IGLV3-27", "IGLV3-27",
"IGLV3-27", "IGLV3-27", "IGLV3-27", "IGLV3-27", "IGLV3-27", "IGLV3-27",
"IGLV3-27", "IGLV3-27", "IGLV3-27", "IGLV3-27", "IGLV3-27", "IGLV3-27",
"IGLV3-27", "IGLV3-27")), .Names = c("target_id", "tpm", "condition",
"hgnc_symbol"), row.names = c(218969L, 218970L, 218971L, 218972L,
218973L, 218974L, 218975L, 218976L, 1739400L, 1739401L, 1739402L,
1739403L, 1739404L, 1739405L, 1739406L, 1739407L, 1739408L, 1739409L,
1739410L, 1739411L, 1739412L), class = "data.frame")

这是我的代码:

genes <- head(results_table$ens_gene, 25) 
for (i in 1:length(genes)) {
  curr_gene <- genes[i]
  curr_data <- final_output[which(final_output$ensembl_gene_id==curr_gene),]
  curr_data <- subset(curr_data, select=c(target_id, tpm, condition, hgnc_symbol))
  hgnc_symbol <- unique(curr_data$hgnc_symbol)
  print({
    ggplot(curr_data, aes(x = condition, y = tpm, fill=condition)) + 
    geom_boxplot(outlier.colour=NA, lwd=0.2, color="grey18") + 
    stat_boxplot(geom ='errorbar', color="grey18") + 
    geom_jitter(size=0.8, width=0.2) + 
    facet_wrap(~target_id) + 
    guides(fill=FALSE) + 
    theme_bw() +  
    labs(title=paste(hgnc_symbol, "_", curr_gene, sep="")) + 
    labs(x="condition") + labs(y="TPM") + 
    theme(text = element_text(size=9), 
      strip.text.x = element_text(size = 10), 
      axis.text.x = element_text(angle = 90, hjust = 1, size=12),
      axis.text.y = element_text(size=9),
      title = element_text(size=12),
      axis.title.x = element_text(size=12),
      axis.title.y = element_text(size=12))})
}

我知道,对于我上面给出的数据的箱线图,分面并不是绝对必要的,因为该图只有一个分面;但是,这与该代码生成的其他一些箱线图相关,这就是为什么它在代码中。

当我打印相同的绘图作为.png(而不是作为R Markdown报告的一部分)时,它打印成功,这让我相信我的问题与R Markdown有关。 以前有没有人遇到过这个问题并找到了解决方法?

如果它对任何人有帮助,我遇到了这个问题,并通过重新启动我的 R 会话解决了这个问题。

如果在编织过程中发生错误,但您可以在 R markdown 文件中运行块并且它成功绘制,请尝试清空与 Rmd 文件关联的缓存文件夹。这对我有用。重新启动我的 R 会话不起作用。

相关内容

最新更新