R 自动确定绘图图例的位置

R 自动确定绘图图例的位置,r,automation,plot,ggplot2,gnuplot,R,Automation,Plot,Ggplot2,Gnuplot,可以在大多数打印程序中手动定位图例。例如,在gnuplot中,使用set key top right完成。在ggplot2中,它完成了 是否有绘图库、脚本或简单算法自动定位图例,使其与绘图中的数据重叠最小 我的意思是:假设我画一条线y=x。在这种情况下,图例的最佳位置是左上角或右下角。试试这个 require(Hmisc) ?largest.empty R-help存档中还提出了其他讨论和功能 require(plotrix) ?emptyspace # Find the large

可以在大多数打印程序中手动定位图例。例如,在gnuplot中,使用
set key top right
完成。在ggplot2中,它完成了

是否有绘图库、脚本或简单算法自动定位图例,使其与绘图中的数据重叠最小

我的意思是:假设我画一条线
y=x
。在这种情况下,图例的最佳位置是左上角或右下角。

试试这个

require(Hmisc)
?largest.empty
R-help存档中还提出了其他讨论和功能

require(plotrix)

?emptyspace     # Find the largest empty space on a plot
以下是帮助页面中的示例:

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

x一个快速技巧,可以获得
“topleft”
“topright”
“bottomleft”
“bottomright”

编辑

par(“usr”)
after
plot
返回打印区域的用户坐标极值:请参见
?par
。因此,我们可以将函数更改为:

auto.legend.pos <- function(x,y,xlim=NULL,ylim=NULL) {
  if (dev.cur() > 1) {
    p <- par('usr')
    if (is.null(xlim)) xlim <- p[1:2]
    if (is.null(ylim)) ylim <- p[3:4]
  } else {
    if (is.null(xlim)) xlim <- range(x, na.rm = TRUE)
    if (is.null(ylim)) ylim <- range(y, na.rm = TRUE)
  }
  countIt <- function(a) {
    tl <- sum(x <= xlim[1]*(1-a)+xlim[2]*a & y >= ylim[1]*a+ylim[2]*(1-a))
    tr <- sum(x >= xlim[1]*a+xlim[2]*(1-a) & y >= ylim[1]*a+ylim[2]*(1-a))
    bl <- sum(x <= xlim[1]*(1-a)+xlim[2]*a & y <= ylim[1]*(1-a)+ylim[2]*a)
    br <- sum(x >= xlim[1]*a+xlim[2]*(1-a) & y <= ylim[1]*(1-a)+ylim[2]*a)
    c(topleft=tl,topright=tr,bottomleft=bl,bottomright=br)
  }
  for (k in seq(0.5,0.1,by=-0.05)) {
    a <- countIt(k)
    if (sum(a==0)>0) break
  }
  names(a)[which(a==0)][1]   # may delete "[1]"
}

plot(Sepal.Length~Petal.Length, data=iris, xlim=c(1,5), ylim=c(3.5,6))
auto.legend.pos(iris$Petal.Length, iris$Sepal.Length)
# [1] "bottomright"
auto.legend.pos 1){

p在R中,我怀疑答案是否定的。但是你更有可能用[R]标签吸引有知识的R用户的眼球。实际上,我更感兴趣的是一个独立的解决方案(我将实现的脚本或算法),它将为任何绘图程序实现这一点(我选择的武器是
gnuplot
plot(Sepal.Length~Petal.Length, data=iris)
auto.legend.pos(iris$Petal.Length, iris$Sepal.Length)
# [1] "topleft"
auto.legend.pos <- function(x,y,xlim=NULL,ylim=NULL) {
  if (dev.cur() > 1) {
    p <- par('usr')
    if (is.null(xlim)) xlim <- p[1:2]
    if (is.null(ylim)) ylim <- p[3:4]
  } else {
    if (is.null(xlim)) xlim <- range(x, na.rm = TRUE)
    if (is.null(ylim)) ylim <- range(y, na.rm = TRUE)
  }
  countIt <- function(a) {
    tl <- sum(x <= xlim[1]*(1-a)+xlim[2]*a & y >= ylim[1]*a+ylim[2]*(1-a))
    tr <- sum(x >= xlim[1]*a+xlim[2]*(1-a) & y >= ylim[1]*a+ylim[2]*(1-a))
    bl <- sum(x <= xlim[1]*(1-a)+xlim[2]*a & y <= ylim[1]*(1-a)+ylim[2]*a)
    br <- sum(x >= xlim[1]*a+xlim[2]*(1-a) & y <= ylim[1]*(1-a)+ylim[2]*a)
    c(topleft=tl,topright=tr,bottomleft=bl,bottomright=br)
  }
  for (k in seq(0.5,0.1,by=-0.05)) {
    a <- countIt(k)
    if (sum(a==0)>0) break
  }
  names(a)[which(a==0)][1]   # may delete "[1]"
}

plot(Sepal.Length~Petal.Length, data=iris, xlim=c(1,5), ylim=c(3.5,6))
auto.legend.pos(iris$Petal.Length, iris$Sepal.Length)
# [1] "bottomright"