user14880462
user14880462

Reputation: 17

R: ggarrange() "No graphics to replay" error

I'm using ggarrange() to combine 3 ggplots and am getting the following error message:

"In grid.echo.recordedplot(dl, newpage, prefix) : No graphics to replay"

My code of one of the plots (they are all the same just different values) and the ggarrange() are below:

All_data_80<-All_data[All_data$rh.x == "80H",]
All_data_80<-All_data[All_data$rh_std_num == "80",]
All_data_80<-na.omit(All_data_80)
plot_80<-ggplot
ggplot(data=All_data_80 , aes(x = `Name`, y=`q`, fill=`Name`)) +
  theme_bw()+
  ggtitle("Plot Title")+
  geom_col(data=All_data_80[All_data_80$Number=="5",],position=position_dodge(width=0.5))+
  geom_col(data=All_data_80[All_data_80$Number=="14",],position=position_dodge(width=0.5))+
  geom_col(data=All_data_80[All_data_80$Number==="95",],position=position_dodge(width=0.5))+
  xlab("Name")+
  ylab("Percentage")+
  theme(axis.text.x = element_blank(),
        axis.text.y = element_text(size=8),
        legend.text=element_text(size=12),
        legend.title=element_blank(),
        legend.position = "bottom",
        axis.title=element_text(size=12,hjust = 0.5),
        plot.title = element_text(face="bold",size=14, hjust = 0.5),
        plot.caption=element_text(size=12,hjust = 0.0),
        strip.text = element_text(size=12),
        panel.grid.major = element_blank())
plot_80

ggarrange(plot_40, plot_60, plot_80, labels=c("A","B","C"), nrow=1, ncol=3)

The output looks like: enter image description here

Could anyone help me understand where I am going wrong? Thank you!

Upvotes: 0

Views: 376

Answers (1)

slamballais
slamballais

Reputation: 3235

Answer

You never store any plots in plot_40, plot_60, or plot_80, so there is nothing to plot. You are storing the actual ggplot function in them.

Reproducible example

test <- ggplot2::ggplot
ggpubr::ggarrange(test)

This yields:

Warning message:
In grid.echo.recordedplot(dl, newpage, prefix) : No graphics to replay

Reasoning

In your code, this arises because you wrote:

plot_80 <- ggplot
*ggplot call*
plot_80

So, you store the ggplot function in plot_80, then you output a plot (but never store it), and then you call plot_80 (which contains the content of the ggplot function). I think you meant to write it as:

plot_80 <- *ggplot call*

Upvotes: 1

Related Questions