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

Jim Lemon drjimlemon at gmail.com
Wed Jul 8 12:20:05 CEST 2015


Hi Rosa,
As you are using base graphics, here is an example that might be of
use. As we don't have access to your data, I have used something
similar to the toy data in the example for the "panes" function. This
could be done better using the "split.screen" function, so let me know
if you would like an example using that.

 library(plotrix)
 # start a wide plotting device
 x11(width=10,height=4)
 y<-runif(100)
 oldpar<-panes(matrix(1:6,nrow=2,byrow=TRUE),widths=c(1,1,1.7))
 par(mar=c(0,2,1.8,0))
 boxplot(y,axes=FALSE)
 axis(2)
 box()
 par(mar=c(0,0,1.8,0))
 tab.title("Boxplot of y",tab.col="#88dd88",cex=1)
 y_hist<-hist(y,axes=FALSE,breaks=seq(0,1,length.out=5))
 box()
 tab.title("Histogram of y",tab.col="#dd8800",cex=1)
 par(mar=c(0,0,1.8,12))
 pie(y_hist$counts,col=2:9)
 tab.title("Pie chart of y categories",tab.col="#8888dd",cex=1)
 box()
 par(mar=c(2,2,1.8,0))
 plot(y,xaxs="i",xlim=c(0,101),axes=FALSE,col=2:9)
 axis(2)
 box()
 tab.title("Scatterplot of y",tab.col="#aabbcc",cex=1)
 par(mar=c(2,0,1.8,0))
 plot(sort(y),xaxs="i",xlim=c(0,101),axes=FALSE,col=2:9)
 box()
 tab.title("Scatterplot of y sorted",tab.col="#ddbc44",cex=1)
 # center the title at the middle of the fifth plot
 mtext("Overall title of plot",side=1,line=0.8,cex=1.5)
 par(mar=c(2,0,1.8,12))
 plot(diff(y),xaxs="i",xlim=c(0,100),axes=FALSE,col=2:9)
 axis(4)
 box()
 tab.title("Scatterplot of diff(y)",tab.col="#ff33cc",cex=1)
 legend(115,1.8,
  c("Boxplot","Histogram","Pie chart","Scatterplot","Sort","Diff"),
  fill=c("#88dd88","#dd8800","#8888dd","#aabbcc","#ddbc44","#ff33cc"),
  xpd=NA)

Jim




On Wed, Jul 8, 2015 at 1:05 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list