On Fri, Dec 18, 2009 at 12:03, Marc Giombetti <giombe...@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]]

______________________________________________
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.

Reply via email to