Is it possible to obtain the appropriate value of inset
automatically so that the left corner of legend will always be just outside of the top right corner of plot?
In the plot below I had to try several values for inset
manually. It would be nice to not have to do it manually since I have to make multiple plots.
graphics.off()
windows(width = 5, height = 5)
set.seed(42)
par(mar = c(5,5,1,10))
plot(rnorm(50,15,5), rnorm(50,15,3),
xlim = c(0,30), ylim = c(5,25),
pch = 19, col = c("red","blue"))
par(xpd = TRUE)
legend("topright", inset = c(-.80, 0),
pch = 19, col = c("red","blue"),
legend = c("LEGEND 1","Second Legend"))
The optional inset argument specifies how far the legend is inset from the plot margins. If a single value is given, it is used for both margins; if two values are given, the first is used for x - distance, the second for y -distance.
Which argument must be set with plotting function for Legend () to display the legends? Arguments. the x and y co-ordinates to be used to position the legend. They can be specified by keyword or in any way which is accepted by xy.
legend creates a legend with descriptive labels for each plotted data series. For the labels, the legend uses the text from the DisplayName properties of the data series. If the DisplayName property is empty, then the legend uses a label of the form 'dataN' .
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 Bottom. list("x" = 100, "y"= 1) Outside Right Top.
After the plot
call, before adding the legend
, use par("usr")
* to extract the coordinates of the plotting region.
Then, instead of positioning the legend using a 'keyword' and inset
, use x
and y
with the top-right coordinates of the plotting region obtained from par("usr")
. Adjust x
with a suitable coefficient.
coord <- par("usr")
legend(x = coord[2] * 1.05, y = coord[4],
pch = 19, col = c("red", "blue"),
legend = c("LEGEND 1", "Second Legend"))
And just for fun, a more convoluted alternative.
After plot
ting, call legend
with position topright
, but without plotting it to the device (plot = FALSE
), and assign it to an object.
Extract the left x
and the top y
coordinate of the legend box, and its width (see Value section in ?legend
), to be used in x
and y
in legend
:
leg <- legend("topright", pch = 19, col = c("red", "blue"),
legend = c("LEGEND 1", "Second Legend"),
plot = FALSE)
legend(x = (leg$rect$left + leg$rect$w) * 1.05, y = leg$rect$top,
pch = 19, col = c("red", "blue"),
legend = c("LEGEND 1", "Second Legend"))
*From ?par
usr
A vector of the formc(x1, x2, y1, y2)
giving the extremes of the user coordinates of the plotting region.
The position calculations that are made when inset
parameter(s) have been specified, are in fact based on par("usr")
(see line 188-199 in the legend
code).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With