【问题标题】:how to rescale the Y axis (frequency) of a histogram in R?如何重新调整R中直方图的Y轴(频率)?
【发布时间】:2013-02-01 13:48:44
【问题描述】:

我有这个raster file 我想将 y 轴(频率)重新调整为 [0,1](通过将频率除以所有频率的总和)。

conne <- file("C:\\fined.bin","rb")
sd<- readBin(conne, numeric(), size=4,  n=1440*720, signed=TRUE)
y<-t(matrix((data=sd), ncol=1440, nrow=720))
r = raster(y)
hist(r, breaks=30, main="SMD_2010",
        xlab="Pearson correlation", ylab="Frequency", xlim=c(-1,1))

example:


        values  frequency        (rescaled by dividing each frequency by the sum(85600))
          -1    0                       0
        -0.5    100               0.001168224
           0    38000                 0.443925234
         0.5    7500                  0.087616822
        0.75    40000                 0.46728972

【问题讨论】:

  • 也许在你的数据周围使用density( )
  • 使用:hist(...,probability = TRUE)
  • 好的,然后可能类似于: hist_temp

标签: r plot histogram


【解决方案1】:

一种解决方案是保存直方图 os 对象。如果您查看此对象的结构,您可以看到直方图条的高度存储在元素 counts 中。

r<-sample(1:25000,1000)
hist.ob <- hist(r)
str(hist.ob)
List of 7
 $ breaks     : num [1:14] 0 2000 4000 6000 8000 10000 12000 14000 16000 18000 ...
 $ counts     : int [1:13] 75 46 72 91 71 91 74 87 86 82 ...
 $ intensities: num [1:13] 3.75e-05 2.30e-05 3.60e-05 4.55e-05 3.55e-05 4.55e-05 3.70e-05 4.35e-05 4.30e-05 4.10e-05 ...
 $ density    : num [1:13] 3.75e-05 2.30e-05 3.60e-05 4.55e-05 3.55e-05 4.55e-05 3.70e-05 4.35e-05 4.30e-05 4.10e-05 ...
 $ mids       : num [1:13] 1000 3000 5000 7000 9000 11000 13000 15000 17000 19000 ...
 $ xname      : chr "r"
 $ equidist   : logi TRUE
 - attr(*, "class")= chr "histogram"

要转换您的数据以使所有条形高度的总和为 1,您必须将每个数字除以 counts 数字的总和。然后使用plot() 函数获取新的绘图。

hist.ob$counts<-hist.ob$counts/sum(hist.ob$counts)
plot(hist.ob)

【讨论】:

  • @Barry 除以 sum() 将改变范围。所以除以最大值将保持范围0-1。如果这不是您需要的解决方案,请更新您的问题以进一步解释您所说的缩放 y 轴的含义。
  • @Barry - 检查你的样本数据 - 如果你使用 sum(hist.ob$counts),那么高度的总和将为 1。在我的样本数据中,所有值都非常相似,所以你改造后看到这么小的值。
猜你喜欢
  • 2014-05-09
  • 2021-03-03
  • 1970-01-01
  • 2015-07-18
  • 1970-01-01
  • 2013-02-05
  • 2015-10-13
  • 2011-10-28
  • 2020-05-10
相关资源
最近更新 更多