Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating the appropriate inset value for legends automatically

Tags:

plot

r

legend

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"))

enter image description here

like image 929
d.b Avatar asked Feb 06 '17 19:02

d.b


People also ask

What is inset in legend in R?

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?

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.

What does legend do in matlab?

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' .

How do I add a legend to an outside plot in R?

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.


1 Answers

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"))

enter image description here


And just for fun, a more convoluted alternative.

After plotting, 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"))

enter image description here


*From ?par

usr A vector of the form c(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).

like image 100
Henrik Avatar answered Sep 28 '22 00:09

Henrik