首页 > 解决方案 > ggplot在interplot中缩放x轴

问题描述

我想在 interplot 中缩放 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”)中的错误:无法将类型“闭包”强制转换为“字符”类型的向量

提前谢谢你!

标签: rggplot2

解决方案


根据我过去的比例变换经验,您需要为变换定义一个函数,为逆定义另一个函数。您上面的代码示例似乎不符合该标准。

如果没有可用于测试的数据样本或 interplot,这就是我设置它的方式:

#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 定义,看看它是如何进行的。
一旦代码正常工作,请随时从生产代码中删除打印语句。


推荐阅读