With ggplot, or a different package, how do I plot multiple graphs together (possibly as a saved file) without squishing my plots?

Viewed 46

I have five graphs plotted, each with one slight variable change, the randmod function as seen below.


library(spatstat)
library(ggplot2)
library(dplyr)
library(ggpubr)
library(tidyr)

set.seed(4)

dim <- 2000
radiusCluster<-100
lambdaParent<-.02
lambdaDaughter<-30
hosts<-900
randmod<-0 #this is the variable that changes
delta.t <- 1
iterations <- 1000
sigma <- 0.1 
beta <- 1 
theta <- 10
b <- .4

numbparents<-rpois(1,lambdaParent*dim)

xxParent<-runif(numbparents,0+radiusCluster,dim-radiusCluster)
yyParent<-runif(numbparents,0+radiusCluster,dim-radiusCluster)

numbdaughter<-rpois(numbparents,(lambdaDaughter))
sumdaughter<-sum(numbdaughter)

theta<-2*pi*runif(sumdaughter)
rho<-radiusCluster*sqrt(runif(sumdaughter))

xx0=rho*cos(theta)
yy0=rho*sin(theta)

xx<-rep(xxParent,numbdaughter)
yy<-rep(yyParent,numbdaughter)

xx<-xx+xx0

yy<-yy+yy0
cds<-data.frame(xx,yy)
is_outlier<-function(x){
  x > dim| x < 0
}
cds<-cds[!(is_outlier(cds$xx)|is_outlier(cds$yy)),]
sampleselect<-sample(1:nrow(cds),hosts,replace=F)
cds<-cds%>%slice(sampleselect)

randfunction<-function(x){
  x<-runif(length(x),0,dim)
}
randselect<-sample(1:nrow(cds),floor(hosts*randmod),replace=F)
cds[randselect,]<-apply(cds[randselect,],1,randfunction)
landscape<-ppp(x=cds$xx,y=cds$yy,window=owin(xrange=c(0,dim),yrange=c(0,dim)))
plot1<-ggplot(data.frame(landscape))+geom_point(aes(x=x,y=y))+coord_equal()+theme_minimal()+ggtitle("Rf=0")

plot1

This produces a graph identical to this:

enter image description here

I repeat this process for 4 other values of randmod, i.e.:

set.seed(4)

dim <- 2000
radiusCluster<-100
lambdaParent<-.02
lambdaDaughter<-30
hosts<-900
randmod<-0.25 #change in randmod
delta.t <- 1
iterations <- 1000
sigma <- 0.1 
beta <- 1 
theta <- 10
b <- .4

numbparents<-rpois(1,lambdaParent*dim)

xxParent<-runif(numbparents,0+radiusCluster,dim-radiusCluster)
yyParent<-runif(numbparents,0+radiusCluster,dim-radiusCluster)

numbdaughter<-rpois(numbparents,(lambdaDaughter))
sumdaughter<-sum(numbdaughter)

theta<-2*pi*runif(sumdaughter)
rho<-radiusCluster*sqrt(runif(sumdaughter))

xx0=rho*cos(theta)
yy0=rho*sin(theta)

xx<-rep(xxParent,numbdaughter)
yy<-rep(yyParent,numbdaughter)

xx<-xx+xx0

yy<-yy+yy0
cds<-data.frame(xx,yy)
is_outlier<-function(x){
  x > dim| x < 0
}
cds<-cds[!(is_outlier(cds$xx)|is_outlier(cds$yy)),]
sampleselect<-sample(1:nrow(cds),hosts,replace=F)
cds<-cds%>%slice(sampleselect)

randfunction<-function(x){
  x<-runif(length(x),0,dim)
}
randselect<-sample(1:nrow(cds),floor(hosts*randmod),replace=F)
cds[randselect,]<-apply(cds[randselect,],1,randfunction)
landscape<-ppp(x=cds$xx,y=cds$yy,window=owin(xrange=c(0,dim),yrange=c(0,dim)))
plot2<-ggplot(data.frame(landscape))+geom_point(aes(x=x,y=y))+coord_equal()+theme_minimal()+ggtitle("Rf=0.25")

plot2

Producing the graph below:

enter image description here

My problem is this, when I use ggarrange, the graphs become squished together and very unclear.

ggarrange(plot1,plot2,plot3,plot4,plot5,nrow=3,ncol=2)

enter image description here

I've tried other packages such as "egg" and "cowplot" to produce a graph that is at least reasonable in the plotting frame but without success. I have also tried:

ggsave("arrange.png", arrangeGrob(grobs = l))

But this also produces the same squished plot. Is it possible to either increase the scale of the plots within the equivalent of ggarrange, or possibly save the plots to a separate file that will maintain their original size?

I need to present this information clearly so that is why the graph as it stands is unacceptable.

1 Answers

Try with patchwork:

library(patchwork)
#Code
G <- wrap_plots(list(plot1,plot2,plot3,plot4,plot5),nrow=3,ncol=2)

Output:

enter image description here

Related