我正在使用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有关。有没有人之前遇到过这个问题,并找到了解决方法?
答案 0 :(得分:0)
万一它对任何人有帮助,我都会遇到这个问题,并通过重新启动R会话解决了该问题。