插补中的ggplot比例x轴

时间:2019-09-27 15:36:44

标签: r ggplot2

我想在交点图中缩放x轴。但是,出现以下错误:

require(scales)
Reverse_TwoSD_Standardization_trans = function() trans_new("Reverse_TwoSD_Standardization", function(Tran) {Tran*2*sd(AAA)+mean(BBB)}, function() 1)

interplot(m=Model1, var1 = 'A', var2 = 'B') +
  xlab('AA') +
  ylab('BB') +
  theme_few() + scale_x_continuous(trans = Reverse_TwoSD_Standardization_trans)

paste0(x,“ _trans”)中的错误:   无法将类型'closure'强制转换为类型'character'的向量

谢谢你!

1 个答案:

答案 0 :(得分:1)

根据我过去在比例转换中的经验,您需要为转换定义一个函数,为逆定义一个函数。您上面的代码示例似乎不符合该条件。

我没有设置可用于测试的数据或内部样本的示例,这是我设置的方式:

#define the mean() and 2*sd() globally in order to perform the transformation and the inverse
meanx<-mean(#your x axis variable goes here)
twosdx<-2*sd(#your x axis variable goes here)

#custom transformn
custom<-function(x){
  (x-meanx)/twosdx
}

#inverse custom function
icustom<-function(x){
  y<-x*twosdx+meanx
  print(paste("icustom",x, y))  #debug statement
  return(y)
}


interplot(m=Model1, var1 = 'A', var2 = 'B') +
  xlab('AA') +
  ylab('BB') +
  theme_few() + scale_x_continuous(trans=scales::trans_new("custom", custom, icustom))

将x变量替换为xmean和twosdx定义,然后看看它如何进行。
代码正常工作后,请随时从生产代码中删除打印语句。