As the title says: How can I plot a legend outside the plotting area when using base graphics?
I thought about fiddling around with layout
and produce an empty plot to only contain the legend, but I would be interested in a way using just the base graph facilities and e.g., par(mar = )
to get some space on the right of the plot for the legend.
Here an example:
plot(1:3, rnorm(3), pch = 1, lty = 1, type = "o", ylim=c(-2,2))
lines(1:3, rnorm(3), pch = 2, lty = 2, type="o")
legend(1,-1,c("group A", "group B"), pch = c(1,2), lty = c(1,2))
produces:
But as said, I would like the legend to be outside the plotting area (e.g., to the right of the graph/plot.
Sorry for resurrecting an old thread, but I was with the same problem today. The simplest way that I have found is the following:
Found here: http://www.harding.edu/fmccown/R/
You could do this with the Plotly R API, with either code, or from the GUI by dragging the legend where you want it.
Here is an example. The graph and code are also here.
You can position the legend outside of the graph by assigning one of the x and y values to either 100 or -100.
Here are the other options:
list("x" = 100, "y" = 0)
for Outside Right Bottomlist("x" = 100, "y"= 1)
Outside Right Toplist("x" = 100, "y" = .5)
Outside Right Middlelist("x" = 0, "y" = -100)
Under Leftlist("x" = 0.5, "y" = -100)
Under Centerlist("x" = 1, "y" = -100)
Under RightThen the response.
response = p$plotly(x,y,x2,y2, kwargs=list(layout=layoutstyle));
Plotly returns a URL with your graph when you make a call. You can access that more quickly by calling
browseURL(response$url)
so it will open your graph in your browser for you.That gives us this graph. You can also move the legend from within the GUI and then the graph will scale accordingly. Full disclosure: I'm on the Plotly team.
Recently I found very easy and interesting function to print legend outside of the plot area where you want.
Make the outer margin at the right side of the plot.
Create a plot
Add legend and just use locator(1) function as like below. Then you have to just click where you want after load following script.
Try it
Another solution, besides the ondes already mentioned (using
layout
orpar(xpd=TRUE)
) is to overlay your plot with a transparent plot over the entire device and then add the legend to that.The trick is to overlay a (empty) graph over the complete plotting area and adding the legend to that. We can use the
par(fig=...)
option. First we instruct R to create a new plot over the entire plotting device:Setting
oma
andmar
is needed since we want to have the interior of the plot cover the entire device.new=TRUE
is needed to prevent R from starting a new device. We can then add the empty plot:And we are ready to add the legend:
will add a legend to the bottom right of the device. Likewise, we can add the legend to the top or right margin. The only thing we need to ensure is that the margin of the original plot is large enough to accomodate the legend.
Putting all this into a function;
And an example. First create the plot making sure we have enough space at the bottom to add the legend:
Then add the legend
Resulting in:
Adding another simple alternative that is quite elegant in my opinion.
Your plot:
Legend:
Result:
Here only the second line of the legend was added to your example. In turn:
inset=c(0,1)
- moves the legend by fraction of plot region in (x,y) directions. In this case the legend is at"bottomright"
position. It is moved by 0 plotting regions in x direction (so stays at "right") and by 1 plotting region in y direction (from bottom to top). And it so happens that it appears right above the plot.xpd=TRUE
- let's the legend appear outside of plotting region.horiz=TRUE
- instructs to produce a horizontal legend.bty="n"
- a style detail to get rid of legend bounding box.Same applies when adding legend to the side:
Here we simply adjusted legend positions and added additional margin space to the right side of the plot. Result:
I like to do it like this:
The only tweaking required is in setting the right margin to be wide enough to accommodate the legend.
However, this can also be automated: