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

Rosa Oliveira rosita21 at gmail.com
Tue Jul 7 23:45:27 CEST 2015


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)

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










Atenciosamente,
Rosa Oliveira

-- 
____________________________________________________________________________


Rosa Celeste dos Santos Oliveira, 

E-mail: rosita21 at gmail.com
Tlm: +351 939355143 
Linkedin: https://pt.linkedin.com/in/rosacsoliveira
____________________________________________________________________________
"Many admire, few know"
Hippocrates



More information about the R-help mailing list