R 如何解决运行ggplot2代码时出现的美观错误?

R 如何解决运行ggplot2代码时出现的美观错误?,r,ggplot2,R,Ggplot2,多元线性回归模型为 负荷=-33.124+1.470VO2max-4.909梯度 我在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),

多元线性回归模型为 负荷=-33.124+1.470VO2max-4.909梯度 我在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)) 
库(“ggplot2”)

df这里有一些错误。首先,您没有根据
geom_abline
调用隐含的回归线公式计算截距点

其次,在最初的美学调用中,您已经执行了
aes(vo2max
),但变量名为
vo2max
(区分大小写)

第三,在您对
geom_hline
的调用中,您创建的数据框有一个名为
x
的变量,但您正在将
yintercept
设置为一个名为
y
的变量,该变量不存在。正如伊恩·坎贝尔在评论中指出的那样,您需要一个
geom_hline

通过手动反转绘图中的轴,您似乎给自己带来了困难。您可以通过在上一个绘图中添加
+coord\u 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")

x5在
geom\u hline
调用中
x
的值对于
y
轴来说太高了,我认为它的目的是
geom\u vline
@acampbell段的公式也是错误的。啊,这更有意义。