我无法理解一位同事提供给我的 R 绘图脚本。我知道 ggplot 可用于在绘图例程中转换数据,但我正在寻找一种替代方法来使用 r 中的其他函数计算转换。对于数据 bwpeaks
structure(list(PROB = c(0.25, 0.116666666666667, 0.15, 0.366666666666667,
0.4, 0.133333333333333, 0.633333333333333, 0.5, 0.483333333333333,
0.516666666666667, 0.1, 0.3, 0.666666666666667, 0.9, 0.716666666666667,
0.466666666666667, 0.283333333333333, 0.583333333333333, 0.0833333333333333,
0.35, 0.416666666666667, 0.866666666666667, 0.6, 0.2, 0.75, 0.533333333333333,
0.933333333333333, 0.566666666666667, 0.816666666666667, 0.966666666666667,
0.433333333333333, 0.85, 0.95, 0.333333333333333, 0.8, 0.0666666666666667,
0.733333333333333, 0.883333333333333, 0.183333333333333, 0.0166666666666667,
0.7, 0.45, 0.266666666666667, 0.216666666666667, 0.616666666666667,
0.166666666666667, 0.916666666666667, 0.383333333333333, 0.683333333333333,
0.316666666666667, 0.05, 0.766666666666667, 0.233333333333333,
0.0333333333333333, 0.983333333333333, 0.783333333333333, 0.833333333333333,
0.65, 0.55), FLOW = c(2570, 1330, 1500, 3810, 4100, 1400, 10900,
5590, 5520, 6460, 1240, 2960, 11100, 33400, 13100, 4710, 2730,
9640, 1200, 3330, 4130, 24500, 9650, 2260, 13900, 6980, 38800,
8300, 17700, 49600, 4170, 22200, 47700, 3310, 15900, 1020, 13500,
25200, 2240, 135, 12500, 4650, 2580, 2300, 9680, 1660, 34600,
4010, 11800, 3070, 675, 14600, 2400, 406, 64300, 15100, 19800,
10900, 8040)), class = "data.frame", row.names = c(NA, -59L))
其中 bwpeaks$PROB
是向量 bwpeaks$FLOW
的威 bool 绘图位置。绘图例程如下所示:
library(ggplot2)
library(scales)
library(lmomco)
log10_minor_break = function (...){
function(x) {
minx = floor(min(log10(x), na.rm=T))-1;
maxx = ceiling(max(log10(x), na.rm=T))+1;
n_major = maxx-minx+1;
major_breaks = seq(minx, maxx, by=1)
minor_breaks =
rep(log10(seq(1, 9, by=1)), times = n_major)+
rep(major_breaks, each = 9)
return(10^(minor_breaks))
}
}
xbreaks <- c(0.002,0.01,0.10,0.25,0.5,0.8,0.9,0.95,0.975,0.99,0.995, 0.998)
log.range <- log10(range(bwpeaks$FLOW, na.rm = TRUE))
lower <- 10^floor(log.range[1])
upper <- 10^ceiling(log.range[2])
cap <- 100
ybreaks <- NULL
while(cap < upper) {
ybreaks <- c(ybreaks, seq(cap, cap*1, by = cap))
cap <- cap * 10
}
ggplot(bwpeaks) +
geom_point(aes(x=PROB, y=FLOW)) +
theme_bw() +
scale_y_continuous(trans="log10",
breaks=round(ybreaks, digits=2),
minor_breaks=log10_minor_break(),
name="Discharge (cfs)", labels=comma) +
scale_x_continuous(trans=probability_trans(distribution="norm"),
breaks=xbreaks,
labels=signif(prob2T(xbreaks), digits=3),
name="Return period (yrs)")
我对 scale_x_continuous(trans=probability_trans(distribution="norm")
感到困惑。有没有一种方法可以在不使用 ggplot 的情况下计算转换后的 x 变量 bwpeaks$PROB
?
最佳答案
probability_trans
函数来自 scales
包。它返回一个列表。
tmp <- scales::probability_trans(distribution="norm")
str(tmp)
#List of 7
# $ name : chr "prob-norm"
# $ transform :function (x)
# $ inverse :function (x)
# $ breaks :function (x, n = n_default)
# $ minor_breaks:function (b, limits, n)
# $ format :function (x)
# $ domain : num [1:2] -Inf Inf
# - attr(*, "class")= chr "trans"
所以你可以试试
tmp$transform(bwpeaks$PROB)
#[1] -0.67448975 -1.19181617 -1.03643339 -0.34069483 -0.25334710
#[6] -1.11077162 0.34069483 0.00000000 -0.04178930 0.04178930
# ...
关于r - 在没有 ggplot 的情况下计算 probability_trans(distribution ="norm"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60874039/