在R&rs的尺度包中,为什么trans_new使用反向参数?

时间:2018-03-13 05:33:01

标签: r function methods analysis

我刚刚被建议在r&#s scale包中使用方法trans_new来使用立方根转换图的x轴。我使用trans_new来定义一个立方根函数,然后使用该立方根函数来转换x轴(可能这个练习更具学术性而非实际性。)

我通过trans_new的文档了解到该方法需要变换参数和反向参数。 transform参数说明了一切 - 通过它,我定义了我想要应用于我的数据的转换。

反过来的说法让我摸不着头脑。文档提到了论证的作用,但它没有说明为什么论证是必要的。

  

逆:   功能或功能名称,执行转换的反转

一般描述听起来有点像详细说明反向参数的功能,但我不确定是这样的:

  

并且预计标签功能将对这些中断执行某种逆转换,以便为它们提供在原始比例上有意义的标签。

标签功能? "某种"逆变换?

谷歌搜索没有结果,所以我非常感谢任何人帮助理解为什么trans_new需要反向论证。这个论点到底在做什么?

1 个答案:

答案 0 :(得分:1)

这意味着如果你的变换函数是base::log,那么你的反函数将是base::exp

my_new_transform <- trans_new(name = "test",
                              transform = base::log,
                              inverse = base::exp,
                              breaks = c(1, 10, 100))

正如文档中所述,显然需要标记断点。

然后,您可以继续使用coord_transggplot2一起使用此尺度。

实施例

library(scales)
library(ggplot2)

cube_root <- function(x) x ^ (1/3)
cube <- function(x) x ^ 3

trans_cube <- trans_new(name = "cube root",
                        transform = cube_root,
                        inverse = cube)

# dummy data
plot_data <- data.frame(x = 1:10,
                        y = cube(1:10))

# without applying a transform
ggplot(plot_data, aes(x = x, y = y)) +
  geom_point()

enter image description here

# applying a transform
ggplot(plot_data, aes(x = x, y = y)) +
  geom_point() +
  coord_trans(y = trans_cube)

enter image description here