对 ggplot2 - R 中的连续变换感到困惑

问题描述 投票:0回答:1

我一定错过了一些微不足道的东西,但已经在这上面花了太长时间了,并且认为有人可以提供帮助。

假设我想绘制随机变量的 CDF:

x = rnorm(100)
df <- data.frame(x = x, y = pnorm(x))
ggplot(df, aes(x=x, y=y)) + 
  geom_point()

如果我现在想将轴转换为概率空间,并且可能添加显示 z 分数的辅助轴,那么我会这样做:

ggplot(df, aes(x=x, y=y)) + 
  geom_point() + 
  scale_y_continuous(trans = "probit", sec.axis = sec_axis(trans = stats::qnorm))

现在假设我只想对 y 轴上的 z 分数进行转换。这个:

ggplot(df, aes(x=x, y=y)) + 
  geom_point() + 
  scale_y_continuous(trans = stats::qnorm)

返回:

as.trans()
中的错误: !
trans
必须是字符向量或转换器对象

我也尝试过:

ggplot(df, aes(x=x, y=y)) + 
  geom_point() + 
  scale_y_continuous(trans = transform_probability(distribution = "norm"))

这给了我上面的概率变换。

如何指定 stats::qnorm 对主轴进行的变换?

r ggplot2 transformation cdf
1个回答
0
投票

当然,一旦我制作了可重现的示例,我就会得到答案🤦u200d♀️

这是唯一/最好的方法吗?

x = rnorm(100)
df <- data.frame(x = x, y = pnorm(x))

z_breaks = c(-2, -1, 0, 1, 2)

ggplot(df, aes(x=x, y=y)) + 
  geom_point() + 
  scale_y_continuous(
    trans = "probit", 
    breaks = pnorm(z_breaks), 
    labels = z_breaks
  )

© www.soinside.com 2019 - 2024. All rights reserved.