在R中为hist2d定义中断

时间:2015-11-10 16:16:48

标签: r histogram2d

是否有一种简单的方法来为R中的2d直方图(hist2d)定义中断而不是nbins?

我想为2D直方图定义x轴和y轴的范围以及每个尺寸的bin数。

我的例子:

# example data
x <- sample(-1:100, 2000, replace=T)
y <- sample(0:89, 2000, replace=T)

# create 2d histogram 
h2 <- hist2d(x,y,nbins=c(23,19),xlim=c(-1,110), ylim=c(0,95),xlab='x',ylab='y',main='hist2d')

这导致这个2D直方图输出1

----------------------------
2-D Histogram Object
----------------------------

Call: hist2d(x = x, y = y, nbins = c(23, 19), xlab = "x", ylab = "y", 
   xlim = c(-1, 110), ylim = c(0, 95), main = "hist2d")

Number of data points:  2000 
Number of grid bins:  23 x 19 
X range: ( -1 , 100 ) 
Y range: ( 0 , 89 )

我需要

X range: ( -1 , 110 ) 
Y range: ( 0 , 95 ) 

代替。

我尝试定义xlim和ylim只会扩展绘图,但不会定义直方图的轴范围。我知道附加箱中没有数据。

有没有办法定义

xbreaks = seq(-1,110,5)
ybreaks = seq(0,95,5)

而不是使用nbins,它将范围从最小值到最大值分成给定的箱数?

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

按如下方式修改“hist2d”

hist2d_range<-function (x, y = NULL, nbins = 200, same.scale = TRUE, na.rm = TRUE,
show = TRUE, col = c("black", heat.colors(12)), FUN = base::length,
xlab, ylab,range=NULL, ...)
{
if (is.null(y)) {
if (ncol(x) != 2)
    stop("If y is ommitted, x must be a 2 column matirx")
y <- x[, 2]
x <- x[, 1]
}
if (length(nbins) == 1)
nbins <- rep(nbins, 2)
nas <- is.na(x) | is.na(y)
if (na.rm) {
x <- x[!nas]
y <- y[!nas]
}
else stop("missinig values not permitted if na.rm=FALSE")
if (same.scale) {
if(is.null(range))
   {
       x.cuts <- seq(from = min(x, y), to = max(x, y), length = nbins[1] +
             1)
       y.cuts <- seq(from = min(x, y), to = max(x, y), length = nbins[2] +
             1)
   }else{

       x.cuts <- seq(from = range[1], to = range[2], length = nbins[1] + 1)
           y.cuts <- seq(from = range[1], to = range[2], length = nbins[1] + 1)


   }

}
else {
x.cuts <- seq(from = min(x), to = max(x), length = nbins[1] +
    1)
y.cuts <- seq(from = min(y), to = max(y), length = nbins[2] +
    1)
}
index.x <- cut(x, x.cuts, include.lowest = TRUE)
index.y <- cut(y, y.cuts, include.lowest = TRUE)
m <- tapply(x, list(index.x, index.y), FUN)
if (identical(FUN, base::length))
m[is.na(m)] <- 0
if (missing(xlab))
xlab <- deparse(substitute(xlab))
if (missing(ylab))
ylab <- deparse(substitute(ylab))
if (show)
image(x.cuts, y.cuts, m, col = col, xlab = xlab, ylab = ylab,
    ...)
midpoints <- function(x) (x[-1] + x[-length(x)])/2
retval <- list()
retval$counts <- m
retval$x.breaks = x.cuts
retval$y.breaks = y.cuts
retval$x = midpoints(x.cuts)
retval$y = midpoints(y.cuts)
retval$nobs = length(x)
retval$call <- match.call()
class(retval) <- "hist2d"
retval
}

此函数有一个附加参数“range”。 修订点如下。

   if(is.null(range))
   {
       x.cuts <- seq(from = min(x, y), to = max(x, y), length = nbins[1] +
             1)
       y.cuts <- seq(from = min(x, y), to = max(x, y), length = nbins[2] +
             1)
   }else{

       x.cuts <- seq(from = range[1], to = range[2], length = nbins[1] + 1)
           y.cuts <- seq(from = range[1], to = range[2], length = nbins[1] + 1)


   }

答案 1 :(得分:0)

我稍微更改了代码,这个版本应该可以明确定义两个轴的中断。首先,您必须加载该功能。然后,您可以使用x.breaks提供y.breaksx.breaks=seq(0,10,0.1)选项。 如果same.scale为真,则只需x.breaks 返回值addionaly包含bin的数量和相对计数。 此外,如果需要,您可以通过设置legend=TRUE来添加图例。为此,您需要拥有包Fields

hist2d_breaks = function (x, y = NULL, nbins = 200,same.scale = FALSE, na.rm = TRUE, 
                    show = TRUE, col = c("black", heat.colors(12)), FUN = base::length, 
                    xlab, ylab,x.breaks,y.breaks, ...) 
{
  if (is.null(y)) {
  if (ncol(x) != 2) 
      stop("If y is ommitted, x must be a 2 column matirx")
    y <- x[, 2]
    x <- x[, 1]
  }
  if (length(nbins) == 1) 
    nbins <- rep(nbins, 2)
  nas <- is.na(x) | is.na(y)
  if (na.rm) {
    x <- x[!nas]
    y <- y[!nas]
  }
  else stop("missinig values not permitted if na.rm=FALSE")
  if(same.scale){
    x.cuts = x.breaks;
    y.cuts = x.breaks;
  }else{
    x.cuts <- x.breaks
    y.cuts <- y.breaks   
  }


  index.x <- cut(x, x.cuts, include.lowest = TRUE)
  index.y <- cut(y, y.cuts, include.lowest = TRUE)
  m <- tapply(x, list(index.x, index.y), FUN)
  if (identical(FUN, base::length)) 
    m[is.na(m)] <- 0
  if (missing(xlab)) 
    xlab <- deparse(substitute(xlab))
  if (missing(ylab)) 
    ylab <- deparse(substitute(ylab))
  if (show){
    if(legend){
      image.plot(x.cuts, y.cuts, m, col = col, xlab = xlab, ylab = ylab, 
                 ...)
    }else{
      image(x.cuts, y.cuts, m, col = col, xlab = xlab, ylab = ylab, 
                 ...)
    }
  } 
  midpoints <- function(x) (x[-1] + x[-length(x)])/2
  retval <- list()
  retval$counts <- m
  retval$counts_rel <- m/max(m)  
  retval$x.breaks = x.cuts
  retval$y.breaks = y.cuts
  retval$x = midpoints(x.cuts)
  retval$y = midpoints(y.cuts)
  retval$nobs = length(x)
  retval$bins = c(length(x.cuts),length(y.cuts))
  retval$call <- match.call()
  class(retval) <- "hist2d"
  retval
}

(我的数据)的调用然后带来以下内容: hist2d_breaks(df,x.breaks=seq(0,10,1),y.breaks=seq(-10,10,1),legend=TRUE) 提出以下情节 2D Histogram with breaks