为ggplot定义新的比例轴变换

use*_*945 9 r ggplot2

我正在尝试squared使用y轴创建变换scales::trans_new但是遇到了错误.

MWE

data = data.frame(x = 1:10, y = runif(10), z=rnorm(10, 10))

library(ggplot2)
ggplot(data, aes(x, y, size=z)) +
geom_point() +
scale_y_continuous(trans=scales::trans_new("sq", function(x) x^2, function(x) x^(1/2)))
Run Code Online (Sandbox Code Playgroud)

给出了错误

if(zero_range(as.numeric(limits))){:
缺少值需要TRUE/FALSE时出错

我尝试添加limits,domain但得到了同样的错误.

scale_y_continuous(trans=scales::trans_new("sq", function(x) x^2, function(x) x^(1/2)), limits=c(0,1))
scale_y_continuous(trans=scales::trans_new("sq", function(x) x^2, function(x) x^(1/2), domain=c(0,1)), limits=c(0,1))
Run Code Online (Sandbox Code Playgroud)

它似乎inverse是导致错误的论据 - 但所有的价值观y都是积极的,所以我不明白.我该怎么办?

Dav*_*e2e 10

似乎ggplot使用反函数来设置y轴限制和轴标签.如果y接近零,则gglot填充下限并计算负值.这是美化极限的"负面"副作用.

这是一个解决方法,通过使用小于零的值修改反函数,它将阻止负数的平方根.
现在轴的下限总是大于或等于零.

library(ggplot2)

#square function
sq<-function(x){
  x^2
}
#inverse square function (square root)
isq<-function(x){
  print(paste("isq",x))  #debug statement
  x<-ifelse(x<0, 0, x)
  sqrt(x)
}

data =  data.frame(x = 1:10, y = runif(10), z=rnorm(10, 10))

print(data$y)
ggplot(data, aes(x, y, size=z)) +
  geom_point() +
  scale_y_continuous(trans=scales::trans_new("sq", sq, isq))
Run Code Online (Sandbox Code Playgroud)

注意:传输到生产代码时删除打印功能.

请参阅下面评论中的链接,了解Github上的错误报告.