Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何使用函数使用ggplot2变换轴值?_R_Function_Ggplot2_Arithmetic Expressions - Fatal编程技术网

R 如何使用函数使用ggplot2变换轴值?

R 如何使用函数使用ggplot2变换轴值?,r,function,ggplot2,arithmetic-expressions,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') 谢谢。您必须先定义函数,然后使用scales软件包中的trans\u new函数定义标签的逆函数: log2_1 <- function(x) log2

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

谢谢。

您必须先定义函数,然后使用scales软件包中的
trans\u 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))

log2\u 1这对我很有用。正在尝试重新启动R或重新运行
库(ggplot2)
,对我无效。你能展示你的结果吗?
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))