r - 在 R 的 scales 包中,为什么 trans_new 使用反参数?

标签 r function methods analysis

我刚刚被建议使用 r 的 scales 包中的方法 trans_new 来使用立方根转换绘图的 x 轴。我正在使用 trans_new 定义一个立方根函数,然后使用该立方根函数来转换 x 轴(大概这个练习更学术而不是实际)。

我通过 trans_new 的文档了解到该方法需要一个转换参数和一个反向参数。 transform 参数不言自明——通过它,我定义了我想要应用于我的数据的转换。

不过,反论证让我摸不着头脑。文档提到了参数的作用,但没有说明为什么需要参数。

inverse: function, or name of function, that performs the inverse of the transformation

一般的描述听起来有点像是在详细说明反参数的功能,但我不确定情况是否如此:

and it's expected that the labels function will perform some kind of inverse tranformation on these breaks to give them labels that are meaningful on the original scale.

标签功能? “某种”逆变换?

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

最佳答案

这意味着如果你的转换函数是 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

关于r - 在 R 的 scales 包中,为什么 trans_new 使用反参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49248937/

相关文章:

python - 将毫秒转换为小时、分钟和秒 python

java - 为什么要重写父类(super class)的方法,而不是给它一个不同的名称?

java - 如何找到在给定类中实现其方法的 Java 接口(interface)?

r - 如何使用 dplyr 更新值

php - 使用 URL 将参数传递给函数

r - ggplot geom_errorbarh 错误(输入为连续时需要离散,但输入为离散时需要连续)

c++ - 参数列表中的类型定义

Java-BankAccount.class

r - Dplyr:同时汇总组和整个数据

r - 有效地删除列表和所有子列表中的所有NULL值