首页 > 解决方案 > 如何在运行 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

帮助我解决错误并获得完美的图表

标签: rggplot2

解决方案


这里有一些错误。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")

在此处输入图像描述


推荐阅读