Re: [R] multiple graphs with a single legend and trellis graph
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=#dd,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,#dd,#aabbcc,#ddbc44,#ff33cc), xpd=NA) Jim On Wed, Jul 8, 2015 at 1:05 PM, David Winsemius dwinsem...@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],
Re: [R] multiple graphs with a single legend and trellis graph
Dear Jim, first of all, thank you very much :) can you please explain me how to use split.screen? I’m felling so silly, I could not run your example because of x11(width=10,height=4). I already installed package XQuartz because X11 library was missing , nonetheless, after I have installed it, “ Error in .External2(C_X11, d$display, d$width, d$height, d$pointsize, : unable to start device X11 In addition: Warning message: In x11(width = 10, height = 4) : unable to open connection to X11 display ‘' I didn’t started the x11 server on my mac ;) Now I got the graphs ;) I attach my previous graphs and my data, so you can see :) I’m very naive and new in R :( Thanks again for your help ;) Atenciosamente, Rosa Oliveira Atenciosamente, Rosa Oliveira -- Rosa Celeste dos Santos Oliveira, E-mail: rosit...@gmail.com Tlm: +351 939355143 Linkedin: https://pt.linkedin.com/in/rosacsoliveira Many admire, few know Hippocrates On 08 Jul 2015, at 11:20, Jim Lemon drjimle...@gmail.com wrote: 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=#dd,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,#dd,#aabbcc,#ddbc44,#ff33cc), xpd=NA) Jim On Wed, Jul 8, 2015 at 1:05 PM, David Winsemius dwinsem...@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],
Re: [R] multiple graphs with a single legend and trellis graph
Dear Jim, first of all, thank you very much :) can you please explain me how to use split.screen? I attach my previous graphs and my data, so you can see :) I’m very naive and new in R :( I really tried: 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)) mse - 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 #,main=yaxs default ) 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) tab.title(alpha 1 N sample=50,tab.col=#88dd88,cex=1) # tab.title(\n\n sample size=50) # problem: when I run: tab.title(Mean squared error for , paste(alpha[1])),tab.col=#88dd88,cex=1) # I get an error message: unexpected string constant in tab.title(Mean squared error for , paste(alpha[1])),tab.col= # I searched but wasn't able to fix this one, neither the other subtitle: # tab.title(\n\n sample size=50) box() par(mar=c(0,0,1.8,0)) 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 #,main=yaxs default ) 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) tab.title ( alpha 1 N sample=250,tab.col=#dd8800,cex=1) box() 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 #,main=yaxs default ) 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) tab.title(alpha 1 N sample=1000,tab.col=#dd,cex=1) box() par(mar=c(2,2,1.8,0)) 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 #,main=yaxs default ) 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) box() tab.title(alpha 2 N sample=50,tab.col=#aabbcc,cex=1) par(mar=c(2,0,1.8,0)) 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 #,main=yaxs default ) 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) box() tab.title(alpha 2 N sample=250,tab.col=#ddbc44,cex=1) # center the title at the middle of the fifth plot mtext(Mean Squared Error,side=1,line=0.8,cex=1.5) par(mar=c(2,0,1.8,12)) 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 #,main=yaxs default ) 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) box() tab.title(alpha 2 N sample=1000,tab.col=#ff33cc,cex=1) legend(115,1.8, c(alpha 1 N sample=50,alpha 1 N sample=250,alpha 1 N sample=1000, alpha 2 N sample=50,alpha 2 N sample=250,alpha 2 N sample=1000), fill=c(#88dd88,#dd8800,#dd,#aabbcc,#ddbc44,#ff33cc), xpd=NA) legend(115,1.8, c(Naive, Regression Calibration, Pseudo Likelihood), bty = n,col=c(4,2,3),lty=c(4,2,3), xpd=NA) and I got: Thanks again for your help ;) Atenciosamente, Rosa Oliveira -- Rosa Celeste dos Santos Oliveira, E-mail: rosit...@gmail.com Tlm: +351 939355143 Linkedin: https://pt.linkedin.com/in/rosacsoliveira Many admire, few know Hippocrates On 08 Jul 2015, at 11:35, Dennis Murphy djmu...@gmail.com wrote: Hi: The general process for doing this kind of thing in either ggplot2 or lattice
Re: [R] multiple graphs with a single legend and trellis graph
Dear Jim, first of all, thank you very much :) when I run the code: myDF - rbind(mse.alpha1, mse.alpha2) # assumes both data frames have the same variables in the same order myDF$ID - factor(rep(c(alpha1, alpha2), times = c(nrow(mse.alpha1), nrow(mse.alpha2))) ) library(reshape2) myDF.melt - melt(myDF, id = c(ID, nsample, lambda), measure = c(mse.naive, mse.RegCal, mse.PL)) #Melt the three columns to be plotted, something like library(ggplot2) ggplot(myDF.melt, aes(x = lambda, y = value, color = variable)) + geom_point() + geom_line() + facet_grid(ID ~ nsample) + labs(x = expression(paste(lambda)), y = MSE, color = Type”) I get the attached graph. I also attach my data, so you can see :) I’m I able to resize the graphs differently? I.e. for alpha1: ylim=ylim=c(0,.6) and for alpha2: ylim=c(0,.17)? I reshaped, a new variable in sample was created, NA, Was it me that made something wrong? I’m very naive and new in R :( Thanks again ;) Atenciosamente, Rosa Oliveira -- Rosa Celeste dos Santos Oliveira, E-mail: rosit...@gmail.com Tlm: +351 939355143 Linkedin: https://pt.linkedin.com/in/rosacsoliveira Many admire, few know Hippocrates __ R-help@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.
Re: [R] multiple graphs with a single legend and trellis graph
Hi Rosa, Neither the graph nor the data arrived. R-help can be very fussy about what attached files it will accept. Usually pdf, txt and png, I think, will come through. In any case it is much better to supply data using the dput() function See ?dput or have a look at http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example and http://adv-r.had.co.nz/Reproducibility.html for how to do this. It is best to use dput() rather than supplying a data file because dput() ensures that we get an exact copy of the data as it exists on your computer. There is no real guarantee that I will read in a data file the same way as you have. John Kane Kingston ON Canada -Original Message- From: rosit...@gmail.com Sent: Wed, 8 Jul 2015 13:11:34 +0100 To: djmu...@gmail.com, drjimle...@gmail.com, r-help@r-project.org Subject: Re: [R] multiple graphs with a single legend and trellis graph Dear Jim, first of all, thank you very much :) when I run the code: myDF - rbind(mse.alpha1, mse.alpha2) # assumes both data frames have the same variables in the same order myDF$ID - factor(rep(c(alpha1, alpha2), times = c(nrow(mse.alpha1), nrow(mse.alpha2))) ) library(reshape2) myDF.melt - melt(myDF, id = c(ID, nsample, lambda), measure = c(mse.naive, mse.RegCal, mse.PL)) #Melt the three columns to be plotted, something like library(ggplot2) ggplot(myDF.melt, aes(x = lambda, y = value, color = variable)) + geom_point() + geom_line() + facet_grid(ID ~ nsample) + labs(x = expression(paste(lambda)), y = MSE, color = Type”) I get the attached graph. I also attach my data, so you can see :) I’m I able to resize the graphs differently? I.e. for alpha1: ylim=ylim=c(0,.6) and for alpha2: ylim=c(0,.17)? I reshaped, a new variable in sample was created, NA, Was it me that made something wrong? I’m very naive and new in R :( Thanks again ;) Atenciosamente, Rosa Oliveira -- Rosa Celeste dos Santos Oliveira, E-mail: rosit...@gmail.com Tlm: +351 939355143 Linkedin: https://pt.linkedin.com/in/rosacsoliveira Many admire, few know Hippocrates __ R-help@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. FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop! __ R-help@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.
[R] multiple graphs with a single legend and trellis graph
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: rosit...@gmail.com Tlm: +351 939355143 Linkedin: https://pt.linkedin.com/in/rosacsoliveira Many admire, few know Hippocrates
Re: [R] multiple graphs with a single legend and trellis graph
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==