-1

I tried to create a plot grid that shows all the interactions from my model in separate panels with ggarrange from the ggpubr package and with plot_grid from the cowplot package. In both cases I get the error message: "Aesthetics must be either length 1 or the same as the data (1068): colour, linetype". Is there a way to arrange plots with different aeshetic lengths?

Here is a code sample for two plots and my attempt at arranging them. Each plot looks fine on its own, but can't be arranged together. The data used for the first have 8 rows and those used for plot 2 have 1068 rows:

library(ggplot2)
library(cowplot)
library(ggpubr)

# Interaction 1
plot1 = ggplot(aes(x=age,y=pred),data=newdat)+
  geom_line(data=newdat,aes(x=age,y=pred,colour = newdat$weaned,linetype = newdat$weaned),size=1)+
  scale_color_manual(values = c("grey20", "blue"))+
  scale_linetype_manual(values=c("dashed", "solid"))

# Interaction 2
plot2 = ggplot(aes(x=NPI,y=pred),data=newdat2)+ fig+
  geom_line(data=newdat2,aes(x=NPI,y=pred,colour = as.factor(newdat$density),linetype = as.factor(newdat$density)),size=1)+ 
  scale_color_manual(values = c("blue", "grey20", "black"))

#   
# Try to combine 2 plots in a grid 
plot_grid(plot2, plot1,labels = c("A","B"), align = "h", ncol = 2)

ggarrange(plot2, plot1,labels = c("A", "B"),ncol = 2, nrow = 2)
  • Hi Benjamin, welcome to Stack Overflow. Questions should provide a *minimal reproducible* example. *Minimal* in that it is the smallest possible part of your code. *Reproducible* in the sense that others can *run* your code. I think it is very unlikely anyone can help you because your code is far too long, and more importantly, we cannot run it. Please reformat your question to include the absolute minimum part you need help with as well as a sample of your data such that it can be run. See [How to make a reproducible example](https://stackoverflow.com/questions/5963269/) for more info. – Ian Campbell May 15 '20 at 15:50

1 Answers1

0

You can use the patchwork package to combine multiple plots. It uses a very intuitive approach to combining plots. FOr eg. if you have plot1 and plot2, then plot1 + plot2 will produce a new plot with these two side by side, while plot1 / plot2 will produce a plot with one below the other.

Check it out: Getting Started with Patchwork

Vishal Katti
  • 532
  • 2
  • 6