Jonas,

A relatively simple way to get the legend to plot beside a graph is to use
the "layout" function to create two plot areas, create the graph in the
first area and the legend in the second area.  You can fool around with the
location of the legend by changing the parameters to "layout":

layout(matrix(c(1,2,1,3),nrow=2,ncol=2,byrow=TRUE),widths=c(8,4),heights=c(5,10));
plot(1:10,1:10,lty=1); #main graph
plot(c(0,1),c(0,1),type='n',bty='n',ann=FALSE,xaxt='n',yaxt='n');
legend('topleft',c('txt 1','txt 2'),pch=c(21,22));
Note that you can use 'layout.show(n=3)' (no quotes, of course) after
calling "layout" above to let you see the 3 plot areas (you only use the
1st two in the example above).  You can spcify the plot widths/heights in
absolute units in "layout" using lcm().

Hope this helps.

Buck Stockhausen
***************************************************
* Dr. William T. Stockhausen                      *
***************************************************
* Resource Ecology and Fisheries Management       *
* Alaska Fisheries Science Center                 *
* National Marine Fisheries Service               *
* National Oceanic and Atmospheric Administration *
* 7600 Sand Point Way N.E.                        *
* Seattle, Washington 98115-6349                  *
***************************************************
* email: william.stockhau...@noaa.gov             *
* voice: 206-526-4241 fax: 206-526-6723           *
* web  : http://www.afsc.noaa.gov                 *
***************************************************
All models are wrong, some are useful.--G.E.P. Box
Beware of geeks bearing equations.    --W. Buffett
***************************************************
Disclaimer: The opinions expressed above are personal
and do not necessarily reflect official NOAA policy.




On Sun, Feb 12, 2012 at 4:07 PM, Duncan Murdoch <murdoch.dun...@gmail.com>wrote:

> On 12-02-11 9:07 PM, Jonas Stein wrote:
>
>> Wrong? Nothing. You told R to put the legend at c(1,3)
>>> so it did. If you want it elsewhere you need to specify that.
>>> legend(-1,3, legend=c("one", "two"), inset=-1, xpd=NA)
>>> maybe, or some other location?
>>>
>>
>> ok that works fine. Now i understand how to use it.
>> If i create several plots it would be nice if all legends would
>> have the same distance to plots with different scaling.
>>
>> Can the legend be placed vertically centered, 1cm right to the plot aera?
>>
>
> I'm not sure what position you mean, but legend("top", ...) puts it on the
> top edge of the plot, and the inset argument gives detailed positioning.
>  The units aren't cm, but the grDevices package (or is it grid?) has
> functions to convert between units.
>
> Duncan Murdoch
>
>
>
>>  Can i include a legend like this in a standard plot like
>>>> plot(1:10) too?
>>>> http://www.r-bloggers.com/wp-**content/uploads/2011/03/**heatmap.png<http://www.r-bloggers.com/wp-content/uploads/2011/03/heatmap.png>
>>>>
>>>
>>> Yes.
>>>
>>> What part of that do you want to duplicate?
>>>
>>
>> The coloured squares.
>> for the reader who got to this article and had the same question:
>> I have just found another nice solution for a colour legend a minute ago
>> http://www.r-bloggers.com/**rethinking-loess-for-binomial-**
>> response-pitch-fx-strike-zone-**maps/<http://www.r-bloggers.com/rethinking-loess-for-binomial-response-pitch-fx-strike-zone-maps/>
>>
>> You can specify colors, symbols, labels, etc. in legend().
>>>
>>
>> can i even invent my own symbols?
>>
>> Also, please link to the original blog post, not just the figure, so that
>>> the author gets some credit and we can see the code used.
>>>
>>
>> sure
>> http://www.r-bloggers.com/**ggheat-a-ggplot2-style-**heatmap-function/<http://www.r-bloggers.com/ggheat-a-ggplot2-style-heatmap-function/>
>>
>> kind regards,
>>
>>
>  ______________________________**________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
> PLEASE do read the posting guide http://www.R-project.org/**
> posting-guide.html <http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>

        [[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