如何反转轴顺序并在ggplot中使用预定义的比例?

问题描述 投票:5回答:2

我读过past post,询问有关同时使用scale_reversescale_log10的问题。我有一个类似的问题,除了我的规模,我试图“反向”是“鳞片”包中的预定义比例。这是我的代码:

    ##Defining y-breaks for probability scale
    ybreaks <- c(1,2,5,10,20,30,40,50,60,70,80,90,95,98,99)/100

    #Random numbers, and their corresponding weibull probability valeus (which I'm trying to plot)
    x <- c(.3637, .1145, .8387, .9521, .330, .375, .139, .662, .824, .899)
    p <- c(.647, .941, .255, .059, .745, .549, .853, .451, .352, .157)
    df <- data.frame(x, p)

    require(scales)
    require(ggplot2)

    ggplot(df)+
        geom_point(aes(x=x, y=p, size=2))+
        stat_smooth(method="lm", se=FALSE, linetype="dashed", aes(x=x, y=p))+
        scale_x_continuous(trans='probit',
                           breaks=ybreaks,
                           minor_breaks=qnorm(ybreaks))+
        scale_y_log10()

结果图:有关更多信息,我想要达到的尺度是概率绘图比例,它在比例的任一端(0和1)具有更精细的分辨率以显示极端事件,对中位数的分辨率不断降低值(0.5)。

我希望能够与我的scale_x_reverse概率量表同时使用scale_x_continuous,但我不知道如何以任何自定义比例构建它。对此有何指导?

r ggplot2
2个回答
0
投票

而不是尝试组合两个转换,为什么不转换现有数据然后绘制它?以下看起来应该是正确的。

#http://r.789695.n4.nabble.com/Inverse-Error-Function-td802691.html
erf.inv <- function(x) qnorm((x + 1)/2)/sqrt(2)
#http://en.wikipedia.org/wiki/Probit#Computation 
probit <- function(x) sqrt(2)*erf.inv((2*x)-1) 
# probit(0.3637)
df$z <- probit(df$x)
ggplot(df)+
  geom_point(aes(x=z, y=p), size=2)+
  stat_smooth(method="lm", se=FALSE, linetype="dashed", aes(x=z, y=p))+
  scale_x_reverse(breaks = ybreaks,
                  minor_breaks=qnorm(ybreaks))+
  scale_y_log10()

0
投票

scale_(x|y)_reverse()中的参数传递给scale_(x|y)_continuous()所以你应该简单地做:

scale_x_reverse(trans='probit', breaks = ybreaks, minor_breaks=qnorm(ybreaks))
© www.soinside.com 2019 - 2024. All rights reserved.