如何解决您可以在 ggplot2 图的轴上反转缩放和居中吗?
我想缩放回归模型的预测变量,但我想使用 ggplot2 在 x 轴上绘制原始值以提高清晰度。我尝试使用 scale_x_continuous() 来做到这一点。
library('tidyverse')
x <- rnorm(100,10,1.5)
Zx <- scale(x)
Zy <- .8*Zx + rnorm(100,sqrt(1 - (.8^2)))
df <- tibble(Zx = Zx,y = Zy)
SD_scale <- attr(df$Zx,"scaled:scale")
center <- attr(df$Zx,"scaled:center")
unscale_trans <- function(x){
scales::trans_new(
"unscale",function(x) center + x * SD_scale,function(x) scale(x) )
}
df %>%
ggplot(aes(x=Zx,y=y)) + geom_point() +
geom_smooth(method = "lm") +
scale_x_continuous(trans="unscale")
这会引发以下警告:
1: 在 c(-1,1) * (width * mul + add) 中: 不推荐使用向量数组算法中长度为 1 的循环数组。 使用 c() 或 as.vector() 代替。
2: 在 c(-1,1) * (width * mul + add) 中: 不推荐使用向量数组算法中长度为 1 的循环数组。 使用 c() 或 as.vector() 代替。
并导致奇怪的缩放标签。
是否有一种令人满意的“取消缩放”轴的方法?
预先感谢您的帮助,
约翰
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。