【问题标题】:How to use a function to transform axis values with ggplot2?如何使用函数通过 ggplot2 转换轴值?
【发布时间】:2020-07-09 02:44:50
【问题描述】:

我想使用“log2/(log2-1)”而不是“log2”

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

谢谢。

【问题讨论】:

  • 它对我有用。尝试重新启动 R 或重新运行 library(ggplot2),
  • 不适合我。你能展示你的结果吗?

标签: r function ggplot2 arithmetic-expressions


【解决方案1】:

您必须先定义函数,然后定义它的逆函数,然后使用 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))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 2019-06-17
    相关资源
    最近更新 更多