I have been using the code below to create 3 separate plots and then using ggarrange in the ggpubr package to combine them into one plot and saving it as an image. However, once I created an Rmd of my script this function is very temperamental and gives me the error "Faceting variables must have at least one value" even when it runs fine outside of the Rmd. Is there a different way to get the same results using something in the ggplot2 package? or another simpler way? Edit: I would like to maintain the girds in my graph and not use a complex way to get a common legend that cowplot requires.
graph_1 <- ggplot(subset(data_p, Target == "1"), aes(x=as.integer(volume), y=percent_yield, color=Tech.Rep))+
geom_point()+
geom_smooth(se=FALSE)+
facet_wrap(~sample)+
ggtitle(paste("Graph 1"))+
ylab("PERCENT YIELD")+
scale_x_discrete(limits= levels(data_p$volume))+
ylim(min=-150, max=150)+
theme(axis.text.x=element_text(size=10,angle=85,hjust=1,vjust=1))+
theme(legend.position="none")+
geom_ribbon(aes(ymax=100, ymin=50), alpha = 0.2, fill="green", col="green")+
geom_ribbon(aes(ymax=0, ymin=0), alpha = 0.2, fill="black", col="black", size=1.0)
graph_2 <- ggplot(subset(data_p, Target == "2"), aes(x=as.integer(volume), y=percent_yield, color=Tech.Rep))+
geom_point()+
geom_smooth(se=FALSE)+
facet_wrap(~sample)+
ggtitle(paste("Graph 2"))+
ylab("PERCENT YIELD")+
scale_x_discrete(limits= levels(data_p$volume))+
ylim(min=-150, max=150)+
theme(axis.text.x=element_text(size=10,angle=85,hjust=1,vjust=1))+
theme(legend.position="none")+
geom_ribbon(aes(ymax=100, ymin=50), alpha = 0.2, fill="green", col="green")+
geom_ribbon(aes(ymax=0, ymin=0), alpha = 0.2, fill="black", col="black", size=1.0)
graph_3 <- ggplot(subset(data_p, Target == "3"), aes(x=as.integer(volume), y=percent_yield, color=Tech.Rep))+
geom_point()+
geom_smooth(se=FALSE)+
facet_wrap(~sample)+
ggtitle(paste("Graph 3"))+
ylab("PERCENT YIELD")+
scale_x_discrete(limits= levels(data_p$volume))+
ylim(min=-150, max=150)+
theme(axis.text.x=element_text(size=10,angle=85,hjust=1,vjust=1))+
theme(legend.position="none")+
geom_ribbon(aes(ymax=100, ymin=50), alpha = 0.2, fill="green", col="green")+
geom_ribbon(aes(ymax=0, ymin=0), alpha = 0.2, fill="black", col="black", size=1.0)
ggarrange(graph_1, graph_2, graph_3, ncol=3, nrow=1, common.legend=TRUE, legend= "bottom")
Here is an example of what a single plot would look like:
and here is what the combined plots look like: