3

R で 2d ヒストグラム (hist2d) の nbins の代わりにブレークを定義する簡単な方法はありますか?

2D ヒストグラムの x​​ 軸と y 軸の範囲と、各次元のビンの数を定義したいと考えています。

私の例:

# 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 を使用する代わりに?

ご協力ありがとうございました

4

2 に答える 2

1

コードを少し変更しました。このバージョンは、両方の軸のブレークを明示的に定義することで動作するはずです。まず、関数をロードする必要があります。次に、x.breaksy.breaksオプションを で指定できますx.breaks=seq(0,10,0.1)。が true の場合same.scale、必要なx.breaks のは、戻り値に加えて、ビンの数と相対カウントが含まれていることだけです。また、 を設定することで、必要に応じて凡例を含めることができます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
}

(my data) を呼び出すと、次のようになります: hist2d_breaks(df,x.breaks=seq(0,10,1),y.breaks=seq(-10,10,1),legend=TRUE) 次のプロットが表示されます 2D ヒストグラムとブレーク

于 2016-02-02T08:01:51.040 に答える