[R] how to plot a nice legend?

Duncan Murdoch murdoch.duncan at gmail.com
Mon Feb 13 01:07:52 CET 2012


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
>>
>> 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/
>
>> 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/
>
> kind regards,
>



More information about the R-help mailing list