I'm using a for loop to create several boxplots as part of an R Markdown report. Most of my boxplots print successfully, while others for some reason fail to print, instead giving me the following error: "Error: Faceting variables must have at least one value".
Here is my data for one of my boxplots that gave an error:
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")
Here is my code:
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))})
}
I know that, for the boxplot of the data I've given above, faceting is not strictly necessary, since the plot will only have one facet; however, this is relevant to some of the other boxplots generated by this code, which is why it is in the code.
When I print this same plot as a .png (not as part of an R Markdown report), it prints successfully, which leads me to believe that my issue has to do with R Markdown. Has anyone encountered this problem before and figured out a fix for it?