[R] Legend for two plots

Bert Gunter gunter.berton at gene.com
Fri Dec 18 19:07:03 CET 2009


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.




More information about the R-help mailing list