r - 在 R 中为 hist2d 定义中断

标签 r histogram2d

有没有一种简单的方法可以为 R 中的二维直方图 (hist2d) 定义中断而不是 nbins?

我想为二维直方图定义 x 轴和 ya 轴的范围以及每个维度的 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')

这导致此二维直方图输出 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 将范围从最小到最大划分为给定数量的 bins?

谢谢你的帮助

最佳答案

我稍微更改了代码,这个版本应该可以明确定义两个轴的中断。首先你必须加载函数。然后你可以用 x.breaks=seq(0,10,0.1)x.breaksy.breaks 选项。 如果same.scale为真,你只需要x.breaks 返回值还包含 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
}

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

关于r - 在 R 中为 hist2d 定义中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33634593/

相关文章:

r - data.table:按小时增加 IDateTime 以滞后每小时数据

r - polar ggplot 的两个图例(其中一个自定义)

python - 3D 直方图和等高线图 Python

python - 同一图上的多个二维直方图

python - 使 plt.colorbar 扩展到 vmin/vmax 前后的步骤

r - 如何在R中的同一个折线图上绘制多个柱形图?

R根据data.frame中的列创建邻接矩阵

r - mutate 中的嵌套 ifelse 会产生错误的输出

python - 改变matplotlib histogram2d的高度范围

python - matplotlib 和 numpy - 直方图条形颜色和标准化