[R] multiple graphs with a single legend and trellis graph

David Winsemius dwinsemius at comcast.net
Wed Jul 8 05:05:31 CEST 2015


On Jul 7, 2015, at 2:45 PM, Rosa Oliveira wrote:

> Iam trying to plot 6 graphs in one single plot and I was able to, nonetheless I wanted that all graphs had just 1 common legend, as the legend is the same for all the 6 graphs and there is no sense in repeating it 6 times and even more, the legends in each graph sometimes don’t fit the graph.
> 
> Is there a way to put just one legend for all the 6 graphs ate the same time?
> 
> I was told to use a trellis graph, but after days of trying to do that I wasn’t able to.
> 
> Can anyone help me? 
> 
> 
> library(ggplot2)
> library(reshape)


> library(lattice)

Why did you load those packages above? As far as I can see you did not use any lattice or ggplot2 functions. (Also see no reshape or reshape2 functions in use.)

> par(mfrow=c(2,3))
> mse.alpha1 <-read.csv(file="graphs_mse_alpha1.csv",head=TRUE,sep=",")

And there you lose us. We are unable to see your data. The `legend` function can put the legend anywhere. You may need to set par(xpd=TRUE) if the location is outside the current plot area. If you wnated just one legend then you mus ask yourself why you are issuing multiple legend calls. I see the token-`legend` a total of 12 times in the code below.


> attach(mse.alpha1)
> names(mse1000.alpha1)
> mse.alpha2 <-read.csv(file="graphs_mse_alpha2.csv",head=TRUE,sep=",")
> attach(mse.alpha2)
> names(mse.alpha2)
> nsample==50
> 
> plot(mse.alpha1$lambda[mse.alpha1$nsample==50],
> mse.alpha1$mse.naive[mse.alpha1$nsample==50],
> xlab=expression(paste(lambda)),ylab="MSE",type="l",col=4,lty=4, 
> xlim=c(.6,1), ylim=c(0,1), cex.lab=1.5
> )
> lines(mse.alpha1$lambda[mse.alpha1$nsample==50],mse.alpha1$mse.RegCal[mse.alpha1$nsample==50],col=2,lty=2)
> lines(mse.alpha1$lambda[mse.alpha1$nsample==50],mse.alpha1$mse.PL[mse.alpha1$nsample==50],col=3,lty=3)
> title ( expression (paste ("Mean squared error for ", alpha[1])), cex.main=1.5)
> title("\n\n sample size=50")
> legend(.7,1, legend= c("Naive", "Regression Calibration", "Pseudo Likelihood"), bty = "n",col=c(4,2,3),lty=c(4,2,3))
> 
> plot(mse.alpha1$lambda[mse.alpha1$nsample==250],
> mse.alpha1$mse.naive[mse.alpha1$nsample==250],
> xlab=expression(paste(lambda)),ylab="MSE",type="l",col=4,lty=4, 
> xlim=c(.6,1), ylim=c(0,1), cex.lab=1.5
> )
> lines(mse.alpha1$lambda[mse.alpha1$nsample==250],mse.alpha1$mse.RegCal[mse.alpha1$nsample==250],col=2,lty=2)
> lines(mse.alpha1$lambda[mse.alpha1$nsample==250],mse.alpha1$mse.PL[mse.alpha1$nsample==250],col=3,lty=3)
> title ( expression (paste ("Mean squared error for ", alpha[1])), cex.main=1.5)
> title("\n\n sample size=250")
> legend(.7,1, legend= c("Naive", "Regression Calibration", "Pseudo Likelihood"), bty = "n",col=c(4,2,3),lty=c(4,2,3))
> 
> 
> plot(mse.alpha1$lambda[mse.alpha1$nsample==1000],
> mse.alpha1$mse.naive[mse.alpha1$nsample== 1000],
> xlab=expression(paste(lambda)),ylab="MSE",type="l",col=4,lty=4, 
> xlim=c(.6,1), ylim=c(0,1), cex.lab=1.5
> )
> lines(mse.alpha1$lambda[mse.alpha1$nsample== 1000],mse.alpha1$mse.RegCal[mse.alpha1$nsample== 1000],col=2,lty=2)
> lines(mse.alpha1$lambda[mse.alpha1$nsample== 1000],mse.alpha1$mse.PL[mse.alpha1$nsample== 1000],col=3,lty=3)
> title ( expression (paste ("Mean squared error for ", alpha[1])), cex.main=1.5)
> title("\n\n sample size=1000")
> legend(.7,1, legend= c("Naive", "Regression Calibration", "Pseudo Likelihood"), bty = "n",col=c(4,2,3),lty=c(4,2,3))
> 
> plot(mse.alpha2$lambda[mse.alpha2$nsample==50],
> mse.alpha2$mse.naive[mse.alpha2$nsample==50],
> xlab=expression(paste(lambda)),ylab="MSE",type="l",col=4,lty=4, 
> xlim=c(.6,1), ylim=c(0,.17), cex.lab=1.5
> )
> lines(mse.alpha2$lambda[mse.alpha2$nsample==50],mse.alpha2$mse.RegCal[mse.alpha2$nsample==50],col=2,lty=2)
> lines(mse.alpha2$lambda[mse.alpha2$nsample==50],mse.alpha2$mse.PL[mse.alpha2$nsample==50],col=3,lty=3)
> title ( expression (paste ("Mean squared error for ", alpha[2])), cex.main=1.5)
> title("\n\n sample size=50")
> legend(.7,.17, legend= c("Naive", "Regression Calibration", "Pseudo Likelihood"), bty = "n",col=c(4,2,3),lty=c(4,2,3))
> 
> 
> plot(mse.alpha2$lambda[mse.alpha2$nsample==250],
> mse.alpha2$mse.naive[mse.alpha2$nsample==250],
> xlab=expression(paste(lambda)),ylab="MSE",type="l",col=4,lty=4, 
> xlim=c(.6,1), ylim=c(0,.17), cex.lab=1.5
> )
> lines(mse.alpha2$lambda[mse.alpha2$nsample==250],mse.alpha2$mse.RegCal[mse.alpha2$nsample==250],col=2,lty=2)
> lines(mse.alpha2$lambda[mse.alpha2$nsample==250],mse.alpha2$mse.PL[mse.alpha2$nsample==250],col=3,lty=3)
> title ( expression (paste ("Mean squared error for ", alpha[2])), cex.main=1.5)
> title("\n\n sample size=250")
> legend(.7,.17, legend= c("Naive", "Regression Calibration", "Pseudo Likelihood"), bty = "n",col=c(4,2,3),lty=c(4,2,3))
> 
> plot(mse.alpha2$lambda[mse.alpha2$nsample==1000],
> mse.alpha2$mse.naive[mse.alpha2$nsample== 1000],
> xlab=expression(paste(lambda)),ylab="MSE",type="l",col=4,lty=4, 
> xlim=c(.6,1), ylim=c(0,.17), cex.lab=1.5
> )
> lines(mse.alpha2$lambda[mse.alpha2$nsample== 1000],mse.alpha2$mse.RegCal[mse.alpha2$nsample==250],col=2,lty=2)
> lines(mse.alpha2$lambda[mse.alpha2$nsample== 1000],mse.alpha2$mse.PL[mse.alpha2$nsample== 1000],col=3,lty=3)
> title ( expression (paste ("Mean squared error for ", alpha[2])), cex.main=1.5)
> title("\n\n sample size=1000")
> legend(.7,.17, legend= c("Naive", "Regression Calibration", "Pseudo Likelihood"), bty = "n",col=c(4,2,3),lty=c(4,2,3))
> 



David Winsemius
Alameda, CA, USA



More information about the R-help mailing list