r - 如何在运行 ggplot2 代码时解决美学错误?
问题描述
多元线性回归模型是 Load=-33.124+1.470VO2max-4.909Gradient 我在 ggplot2 中工作,并尝试通过改变“梯度 = 0、5、10 和 15”来绘制四个不同的回归线,并绘制具有在 VO2max=50,60 和 75 并找到交点以获得负载的值。
library("ggplot2")
df<- data.frame(load=rep(c(0,4.4,10.7,17,21.4), each=4),
Gradient=c(0,5,10,15),
VO2max=c(28.0,41.0,56.3,71.3,28.2,41.1,57.0,75.0,31.0,45.4,63.6,82.1,
32.0,48.8,66.8,85.5,34.6,50.5,69.9,89.3))
df$Gradient <- as.factor(df$Gradient)
x5 <- -57.669+7.35
x10 <- -82.214+14.7
x15 <- -106.759+22.05
ggplot(df, aes(vo2max,load, group = Gradient)) +
geom_point(aes(shape = Gradient), size = 3) +
geom_abline(aes(slope = 1.47, intercept = -33.124)) +
geom_abline(aes(slope = 1.47, intercept = -57.669)) +
geom_abline(aes(slope = 1.47, intercept = -82.214)) +
geom_abline(aes(slope = 1.47, intercept = -106.759)) +
geom_segment(data = data.frame(x = c(50, 60, 75),
y = c(x5, x10, x15),
Gradient = factor(c(50, 60, 75))),
aes(x, y, xend = 0, yend = y, colour = Gradient),
linetype = 2) +
geom_point(data = data.frame(VO2max = c(50, 60, 75),
load = c(x5, x10, x15),
Gradient = 1)) +
coord_cartesian(xlim = c(0, 105), ylim = c(0, 25),
expand = 0) +
geom_hline(data = data.frame(x = c(50, 60, 75),
Gradient = factor(c(50, 60, 75))),
aes(yintercept = y, colour = Gradient), linetype = 2) +
theme_minimal() +
theme(axis.line = element_line()) +
guides(colour = "none")
data.frame(x = c(50, 60, 75),y = c(x5, x10, x15))
通过运行代码,我收到错误消息:不知道如何为 data.frame 类型的对象自动选择比例。默认为连续。错误:美学长度必须为 1 或与数据 (20) 相同:x
帮助我解决错误并获得完美的图表
解决方案
这里有一些错误。geom_abline
首先,您还没有根据调用所隐含的回归线公式计算截取点。
其次,在您最初的美学调用中,您已经完成了aes(vo2max
,但变量被调用VO2max
(区分大小写)。
第三,在您对geom_hline
创建的数据框的调用中有一个名为的变量,x
但您正在将您的变量设置为一个不存在yintercept
的名为的变量。y
正如 Ian Campbell 在评论中指出的那样,你想要一个geom_hline
这里。
通过从上一个问题中手动反转图中的轴,您似乎给自己带来了困难。+ coord_flip()
您可以通过添加到先前的情节更简单地完成此操作。
以下修改后的代码有效:
x5 <- -57.669+ 1.47 * 50
x10 <- -82.214+ 1.47 * 60
x15 <- -106.759+ 1.47 * 75
ggplot(df, aes(VO2max,load, group = Gradient)) +
geom_point(aes(shape = Gradient), size = 3) +
geom_abline(aes(slope = 1.47, intercept = -33.124)) +
geom_abline(aes(slope = 1.47, intercept = -57.669)) +
geom_abline(aes(slope = 1.47, intercept = -82.214)) +
geom_abline(aes(slope = 1.47, intercept = -106.759)) +
geom_point(data = data.frame(VO2max = c(50, 60, 75),
load = c(x5, x10, x15),
Gradient = 1)) +
geom_segment(data = data.frame(x = c(50, 60, 75),
y = c(x5, x10, x15),
Gradient = factor(c(50, 60, 75))),
aes(x, y, xend = 0, yend = y, colour = Gradient),
linetype = 2) +
coord_cartesian(xlim = c(0, 105), ylim = c(0, 25),
expand = 0) +
geom_vline(data = data.frame(x = c(50, 60, 75),
Gradient = factor(c(50, 60, 75))),
aes(xintercept = x, colour = Gradient), linetype = 2) +
theme_minimal() +
theme(axis.line = element_line()) +
guides(colour = "none")
推荐阅读
- flutter - 如何在 Flutter 的 dropdownButton 中添加按钮
- python - 如何选择具有多个条件的行并计算原始数据框中的新列
- json - VBA post 方法请求正文(“MSXML2.XMLHTTP”):解析 JSON 时出错:^ 期望 '{' 或 '['
- r - 日期时间对象到分钟:我需要 3 个包
- for-loop - 表单中的 Google 应用程序脚本。每周逐步隐藏行
- android - 如何将 Kindle 帐户中的笔记和亮点同步到 iOS / Android 应用程序 - 类似于 ReadWise 所做的
- java - 在 recyclerview 中单击上述单选按钮后,将启用多个单选按钮
- javascript - 我怎样才能让这个视频流畅地滚动?
- java-16 - 我安装了 NetBeans 12.4。我有 JDK 16.0.2。但是在安装netbeans之后,它并没有启动。我在 nb 文件夹 hs_err_pid10436 中找到了这个文件
- python - Doc2Vec 模型中 tqdm 函数的问题