r - 您可以在 ggplot2 绘图的轴上反向缩放和居中吗?
问题描述
我想缩放回归模型的预测变量,但我想使用 ggplot2 在 x 轴上绘制原始值以获得可理解性。我尝试使用 scale_x_continuous() 来做到这一点。
library('tidyverse')
x <- rnorm(100, 10, 1.5)
Zx <- scale(x)
Zy <- .8*Zx + rnorm(100, 0, 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() 。
并导致奇怪的缩放标签。
是否有令人满意的“缩放”轴的方法?
预先感谢您的帮助,
约翰
解决方案
推荐阅读
- c++ - 如何按照 Google C++ 样式指南连接字符串文字?
- graph - n 个节点的最小共同祖先的 AQL
- asynchronous - Flutter:从 Future 中获取价值的正确方法
- r - 饼图以及 infoBox() shinyApp 数据框错误
- django - 在 Django 中创建自定义页面查看计数器
- winapi - Windows - 从代码打开文件并等待它关闭
- database-connection - SQL 查询执行是 TTFB 的一部分吗?
- mysql - 删除重复的表单选择结果
- javascript - 将输入转换为文本而不影响表格列
- django - 图片上传不适用于 DRF 和 React