Dear R users,
I am new to R and I couldn't figure out how to solve the following
problem:
I am trying to put a legend below two plots using the code below. The
legend appears in the second plot,
but I want the legend to appear below the two plots in the center of
the total chart. At the moment the
graphic looks like this: http://i48.tinypic.com/2h2fvhf.jpg
[code]
layout(matrix(1:2, nrow=1))
#c(down,left,top,right)
par(mar=c(4,4,3,1))
plot(totalExp , totalDiffs,
main="Years of experience",
cex.main = 0.9,
cex.lab=0.8,
xlab="Years of experience",
ylab="COCOMO II - expert estimate",
pch=totalPch,
col = totalColors
)
abline(0,0)
plot(totalSoftwareEXP , totalDiffs,
main="Years of prof. software experience",
cex.main = 0.9,
cex.lab=0.8,
xlab="Years of experience",
ylab="COCOMO II - expert estimate",
pch=totalPch,
col = totalColors
)
abline(0,0)
legend("topleft", c("Security","Usability")
,col=c('red','blue'),
pch=c(8,9), cex=0.8,bg='#e0dcdc')
[/code]
Thanks a lot for your help! I really appreciate it!
Marc
Marc:
As you have not received a (public) reply yet, let me try. You may need to
take what I say with a spoonful of salt, though.
Basically, your request makes no sense. The nature of a legend is that it
provides information about a specific plot (e.g. the group labels associated
with point colors). So the legend code apparently does not contemplate
association and positioning with respect to several similar plots, which is
what you'd like to do, although maybe there is something I've overlooked
and
this can be straightforwardly done. In which case, please post the solution
to the list.
So I think what you need to do is draw the legend manually using, e.g.,
?text or ?mtext, ?segments, etc.(You may particularly want to note the
"outer" argument in mtext and par). One way to do this might be to
use
layout to set up your regions, including the legend area, and then
"draw"
the legend as a plot where you want it.
This does seem rather laborious, so I would hope that there is a more
elegant solution. Alternatively, you may wish to examine the ggplot or
lattice plotting systems to see whether they can do this simply (lattice
certainly can using it's "key" argument; but I'm not sure
whether your plots
fit easily into the lattice framework). Of course, there's an additional
learning curve there to contend with.
HTH.
Cheers,
Bert Gunter
Genentech Nonclinical Biostatistics
467-7374
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On
Behalf Of Marc Giombetti
Sent: Friday, December 18, 2009 9:04 AM
To: r-help at r-project.org
Subject: [R] Legend for two plots
Dear R users,
I am new to R and I couldn't figure out how to solve the following
problem:
I am trying to put a legend below two plots using the code below. The
legend appears in the second plot,
but I want the legend to appear below the two plots in the center of
the total chart. At the moment the
graphic looks like this: http://i48.tinypic.com/2h2fvhf.jpg
[code]
layout(matrix(1:2, nrow=1))
#c(down,left,top,right)
par(mar=c(4,4,3,1))
plot(totalExp , totalDiffs,
main="Years of experience",
cex.main = 0.9,
cex.lab=0.8,
xlab="Years of experience",
ylab="COCOMO II - expert estimate",
pch=totalPch,
col = totalColors
)
abline(0,0)
plot(totalSoftwareEXP , totalDiffs,
main="Years of prof. software experience",
cex.main = 0.9,
cex.lab=0.8,
xlab="Years of experience",
ylab="COCOMO II - expert estimate",
pch=totalPch,
col = totalColors
)
abline(0,0)
legend("topleft", c("Security","Usability")
,col=c('red','blue'),
pch=c(8,9), cex=0.8,bg='#e0dcdc')
[/code]
Thanks a lot for your help! I really appreciate it!
Marc
______________________________________________
R-help at r-project.org mailing list
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.
On Fri, Dec 18, 2009 at 12:03, Marc Giombetti <giombetti@gmail.com> wrote:> Dear R users, > > I am new to R and I couldn't figure out how to solve the following > problem: > > I am trying to put a legend below two plots using the code below. The > legend appears in the second plot, > but I want the legend to appear below the two plots in the center of > the total chart. At the moment the > graphic looks like this: http://i48.tinypic.com/2h2fvhf.jpg > > [code] > layout(matrix(1:2, nrow=1)) > #c(down,left,top,right) > par(mar=c(4,4,3,1)) > plot(totalExp , totalDiffs, > main="Years of experience", > cex.main = 0.9, > cex.lab=0.8, > xlab="Years of experience", > ylab="COCOMO II - expert estimate", > pch=totalPch, > col = totalColors > ) > abline(0,0) > > plot(totalSoftwareEXP , totalDiffs, > main="Years of prof. software experience", > cex.main = 0.9, > cex.lab=0.8, > xlab="Years of experience", > ylab="COCOMO II - expert estimate", > pch=totalPch, > col = totalColors > ) > abline(0,0) > > legend("topleft", c("Security","Usability") ,col=c('red','blue'), > pch=c(8,9), cex=0.8,bg='#e0dcdc') > [/code] > > Thanks a lot for your help! I really appreciate it! > > Marc > > ______________________________________________ > R-help@r-project.org mailing list > 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. >Marc, There a few issues with your legend call. Using "topleft" causes the legend to be drawn in the top left corner _inside_ the current plot. So you need to specify x and y coordinates that are outside the plotting region. Second, you need to use the xpd=TRUE parameter to have the legend appear in the margins. This line will get you close: legend(par()$usr[1],par()$usr[3], c("Security","Usability") ,col=c('red','blue'),pch=c(8,9), cex=0.8,bg="#e0dcdc",xpd=TRUE,xjust=1) but there are still problems. The legend box is not centered between the plots, it could be clipped by the first plot depending on the size of your plotting window, etc. Using locator() may be the easiest way (if your graphics device supports it): run this line then click on the plot where you want the legend to appear: legend(locator(1), c("Security","Usability") ,col=c('red','blue'),pch=c(8,9), cex=0.8,bg="#e0dcdc",xpd=TRUE,xjust=0) If your legend is getting clipped, try decreasing the right margin of the first plot. Hope that helps. -Chris [[alternative HTML version deleted]]