R编程其他情节替代品

时间:2018-08-04 12:12:55

标签: r

我想知道如何简化这两个:

plot (payroll,wins)
id = identify(payroll, wins,labels = code, n = 5)
plot (payroll,wins)
with(data, text(payroll, wins, labels = code, pos = 1, cex=0.5))

使用其他替代方法-pch()dan as.numeric()吗?

1 个答案:

答案 0 :(得分:0)

不确定是否更容易,但是您可以在识别过程中按以下方式更改pch(摘自R-help)。每次单击空白点都会更改为实心点。

# data simulation
data <- data.frame(payroll = rnorm(10), wins = rnorm(10), code = letters[1:10])

identifyPch <- function(x, y = NULL, n = length(x), plot = FALSE, pch = 19, ...)
{
  xy <- xy.coords(x, y)
  x <- xy$x
  y <- xy$y
  sel <- rep(FALSE, length(x))
  while (sum(sel) < n) {
    ans <- identify(x[!sel], y[!sel], labels = which(!sel), n = 1, plot = plot, ...)
    if(!length(ans)) {
      break
    }
    ans <- which(!sel)[ans]
    points(x[ans], y[ans], pch = pch)
    sel[ans] <- TRUE
  }
  ## return indices of selected points
  which(sel)
}

if(dev.interactive()) { ## use it
  with(data, plot(payroll,wins))

  id = with(data, identifyPch(payroll, wins))
}