Automatically determine position of plot legend

2019-07-04 11:11发布

You can position the key legend manually in most plotting programs. For example, in gnuplot it's done using set key top right. In ggplot2, it's done like this.

Is there a plotting library, script, or simple algorithm that automatically positions the legend such that it overlaps the data in the plot minimally?

What I mean is: Suppose I plot the line y=x. A good place for the legend in that case would be top left or bottom right.

2条回答
Anthone
2楼-- · 2019-07-04 11:44
require(plotrix)

?emptyspace     # Find the largest empty space on a plot

This is the example from the help page:

x<-rnorm(100)
 y<-rnorm(100)
 plot(x,y,main="Find the empty space",xlab="X",ylab="Y")
 es<-plotrix::emptyspace(x,y)
 # use a transparent background so that any overplotted points are shown
 plotrix::boxed.labels(es,labels="Here is the\nempty space",bg="transparent")
查看更多
孤傲高冷的网名
3楼-- · 2019-07-04 11:49

Try this,

require(Hmisc)
?largest.empty

there are other discussions and functions proposed in R-help archives

查看更多
登录 后发表回答