gpt4 book ai didi

r - 如何重新缩放 R 中直方图的 Y 轴(频率)?

转载 作者:行者123 更新时间:2023-12-02 02:06:42 26 4
gpt4 key购买 nike

我有这个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

enter image description here

最佳答案

一种解决方案是保存直方图 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)

关于r - 如何重新缩放 R 中直方图的 Y 轴(频率)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14647727/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com