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

标签 r function ggplot2 arithmetic-expressions

我想使用“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

谢谢。

最佳答案

您必须先定义函数及其反函数以进行标记,然后使用 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))

enter image description here

关于r - 如何使用函数通过ggplot2转换轴值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60901624/

相关文章:

r - 使用 ggplot2 在绘图底部绘制标题

r - 非标准评估、lapply 和 ggplot

r - 带有 ggplot2 for R 的多色标题

r - Bioconductor、R 版本和本地安装

javascript - 如何从javascript中的函数对象中调用子函数

Azure Functions 在 AppInsights 中缺少行

r - 使用效果包自定义绘图

r - 通过新函数传递图形参数

R - knitr - ShareLatex - install.packages(xyz)

javascript - 当行数未知时,使用 javascript 更改行的类