r - 如何使用任何变换来缩放/变换 graphics::plot() 轴,而不仅仅是对数(对于威 bool 图)?

标签 r plot ggplot2 weibull

我正在构建一个 R 包来在 R 中显示威 bool 图(使用 graphics::plot )。该图有一个对数变换的 x 轴和一个威 bool 变换的 y 轴(因为缺乏更好的描述)。因此,双参数威 bool 分布可以表示为该图上的一条直线。

x 轴的对数变换就像添加 log="x" 一样简单。 plot() 的参数或 curve() .如何以优雅的方式提供 y 轴变换,以便所有与图形相关的绘图都能在我的轴变换绘图上工作?为了演示我需要什么,请运行以下示例代码:

## initialisation ##
beta     <- 2;eta <- 1000
ticks    <- c(seq(0.01,0.09,0.01),(1:9)/10,seq(0.91,0.99,0.01))
F0inv    <- function (p) log(qweibull(p, 1, 1))
    # this is the transformation function
F0       <- function (q) exp(-exp(q))
    # this is the inverse of the transformation function
weibull  <- function(x)pweibull(x,beta,eta)
    # the curve of this function represents the weibull distribution 
    # as a straight line on weibull paper
weibull2 <- function(x)F0inv(weibull(x))

首先是带有 beta=2 的威 bool 分布示例和 eta=1000在常规的、未转换的情节上:
## untransformed axes ##
curve(weibull ,xlim=c(100,1e4),ylim=c(0.01,0.99))
abline(h=ticks,col="lightgray")

plot1



此图对于威 bool 分析无用。这是我目前实现的解决方案,它使用函数 F0inv() 转换数据并修改绘图的 y 轴。请注意,我必须使用 F0inv()在所有 y 轴相关数据上。
## transformed axis with F0inv() ##
curve(weibull2,xlim=c(100,1e4),ylim=F0inv(c(0.01,0.99)),log="x",axes=F)
axis(1);axis(2,at=F0inv(ticks),labels=ticks)
abline(h=F0inv(ticks),col="lightgray")

plot2



这是可行的,但这不是很人性化:当用户想要添加注释时,必须始终使用 F0inv() :
text(300,F0inv(0.4),"at 40%")

我发现您可以使用 ggplot2 解决我的问题和比例,但除非绝对必要,否则我不想更改为图形包,因为需要重写许多其他代码。
## with ggplot2 and scales ##
library(ggplot2)
library(scales)
weibull_trans <- function()trans_new("weibull", F0inv, F0)
qplot(c(100,1e4),xlim=c(100,1e4),ylim=c(0.01,0.99),
    stat="function",geom="line",fun=weibull) + 
    coord_trans(x="log10",y = "weibull") 

plot3



我想如果我能动态地用我自己的代码替换应用对数变换的代码,我的问题就解决了。

我试图通过谷歌搜索“R 轴变换”、“R 用户坐标”、“R 轴缩放”来查找更多信息,但没有有用的结果。我发现的几乎所有内容都涉及对数标度。

我试图查看 plot()在如何log="x"参数有效,但 plot.window 的相关代码是用 C 写的——根本不是我的强项。

最佳答案

虽然在基本图形中似乎不可能,但您可以使此函数执行您想要的操作,以便您可以更简单地调用它:

F0inv    <- function (p) log(qweibull(p, 1, 1))
## this is the transformation function
F0       <- function (q) exp(-exp(q))

weibullplot <- function(eta, beta,
                        ticks=c(seq(0.01,0.09,0.01),(1:9)/10,seq(0.91,0.99,0.01)),
                        ...) {
  ## the curve of this function represents the weibull distribution 
  ## as a straight line on weibull paper
  weibull2 <- function(x)
    F0inv(pweibull(x, beta, eta))
  curve(weibull2, xlim=c(100, 1e4), ylim=F0inv(c(0.01, 0.99)), log="x", axes=FALSE)
  axis(1);
  axis(2, at=F0inv(ticks), labels=ticks)
  abline(h=F0inv(ticks),col="lightgray")
}

weibullplot(eta=1000, beta=2)

关于r - 如何使用任何变换来缩放/变换 graphics::plot() 轴,而不仅仅是对数(对于威 bool 图)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15879050/

相关文章:

r - 获取范围对于 min.n 错误来说太小

r - 将多个文本注释添加到多面 ggplot geom_histogram

r - ggplots 条件色标

r - 将低于阈值的值转换为 1

r - 如何列出 R 文件中的所有函数签名?

R:使用新的点()或线()添加更新图 [xy]lims?

r - 更新 shiny r 中的绘图输出

r - 在R中如何将两个具有不同行和列的矩阵相加?

r - R中的vi输入模式?

matlab - 在 MATLAB 中对条形图进行排序