1

I am wanting to rotate a legend to be placed outside the graph on the right axis. I can place the legend outside the graph, but I cannot figure out how to rotate it. I tried using las=2, but that clearly didn't work. Note: I am using all other axes, or I would place the legend above the graph with two columns to achieve the same look.

A general example of what I have already:

par(mar=c(6,4,5,4)+0.1)
plot(0,type="n")
legend(x="right",
       legend=c("Control Chemical","Test Chemcials"),
       bty="n",
       fill=c("Darkgreen","Dodgerblue4"),
       title="Legend Title",
       inset=c(-0.2,0),
       xpd=TRUE,
       horiz=TRUE)

The margins may need some adjustment, but I can easily make those once I figure out how to rotate the legend.

For additional clarification, what I am hoping to achieve is similar to:

legend(x="top",
       legend=c("Control Chemical","Test Chemcials"),
       bty="n",
       fill=c("Darkgreen","Dodgerblue4"),
       title="Legend Title",inset=c(0,-0.3),
       xpd=TRUE,
       horiz=TRUE)

But on the right axis rather than the top axis.

dayne
  • 7,504
  • 6
  • 38
  • 56
  • 1
    I don't think it's possible, and even gridBase won't help as it cannot use rotated viewports. Is there any reason not to use lattice/ggplot2/grid? – baptiste May 28 '13 at 21:09
  • 1
    @baptiste The simplest answer is I haven't learned ggplot yet, because I have been able to do everything I need in base graphics or lattice. If it's not possible I can just make the right margin bigger than I'd like and place a normal legend outside the figure. I already have a lot of code drawing a pretty complex plot - hence why I cannot place the legend in the plotting region. For now it would not be worth work to learn how to implement all of the graph features in ggplot. – dayne May 28 '13 at 21:11
  • if you don't mind a somewhat ugly workaround, you _could potentially_ set up a layout with gridBase and in the right viewport plot [a rasterised version](http://stackoverflow.com/a/10097858/471093) of the legend. – baptiste May 28 '13 at 21:20

1 Answers1

3

The most sensible option I could think of is to set up two drawing regions with the help of gridBase, and use a grid-based function for the legend (because grobs can always be rotated). Lattice has one (simpleKey I think), but vcd probably has the closest match to the base version.

Note that if you don't need to resize the window, you probably can get away without using gridBase at all: just give enough margin on the right, and tweak the x and y coordinates to push the grid legend at the right location.

Here's a workable example,

library(vcd)
par(mar=c(6,4,5,4)+0.1)
plot(0,type="n")

g = grid_legend(0.5, 0.5, pch=c(22,22), col=c("red", "blue"),
                gp=gpar(fill = c("Darkgreen","Dodgerblue4")),
                labels=c("Control Chemical","Test Chemicals"), 
                title = "Legend Title", draw=FALSE)

grid.draw(grobTree(g, vp=viewport(x=0.93,angle=-90)))

enter image description here

baptiste
  • 75,767
  • 19
  • 198
  • 294
  • I am not sure if this should be an additional question, but is there a way to mimic the `ncol=2` or `horiz=TRUE` calls to `graphics::legend` in `vdc::grid_legend`? And just a quick note: the fill portion of your code does not work like I think you intended. – dayne May 29 '13 at 15:05
  • grid_legend does seem to be fairly limited, looking at its code. The fill parameter is a bit useless here, indeed, and for this purpose one would be better off using a different square shape with only colour. If you want more flexibility, you'll probably have to write your own function. – baptiste May 29 '13 at 15:16
  • I was afraid that would be the case. Thanks for the follow-up! – dayne May 29 '13 at 15:19
  • you could put a second legend with an offset y position, to mimic a second column (not title, and no box, probably) – baptiste May 29 '13 at 16:08