如何使用函数通过ggplot2转换轴值?

How to use a function to transform axis values with ggplot2?

而不是 'log2',我想使用 'log2/(log2-1)'

sp <- ggplot(cars, aes(x = speed, y = dist)) + geom_point()
sp
sp + scale_x_continuous(trans='log2') +
  scale_y_continuous(trans='log2')

当我尝试时,我得到:

object 'log2/(log2-1)_trans' of mode 'function' was not found

谢谢。

您必须先定义函数及其反函数用于标记,然后使用 scales 包中的 trans_new 函数:

log2_1 <- function(x) log2(x)/(log2(x)-1)
antilog2_1 <- function(x) 2^(x/(x-1))

sp + scale_x_continuous(trans = trans_new("log2_1", 
                                          transform=log2_1,
                                          inverse=antilog2_1)) +
  scale_y_continuous(trans = trans_new("log2_1", 
                                       transform=log2_1,
                                       inverse=antilog2_1))