0

The multiple linear regression model is Load=-33.124+1.470VO2max-4.909Gradient I'm working in ggplot2 and tried to draw four different regression line by varying "Gradient=0,5,10 and 15" and also drawing the line segment having at VO2max=50,60 and 75 and finding the intersection point to get the value of the Load.

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)) 

By running the code I'm getting error as: Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous. Error: Aesthetics must be either length 1 or the same as the data (20): x

Help me in solving the error and getting perfect graph

1 Answer 1

1

There were a few errors here. Firstly, you have not calculated the intercept points based on the regression line formulas implied by your geom_abline calls.

Secondly, in your initial aesthetics call, you have done aes(vo2max, but the variable is called VO2max (case sensitive).

Thirdly, in your call to geom_hline the data frame you created has a variable called x but you are setting your yintercept to a variable called y that doesn't exist. As Ian Campbell pointed out in the comments, you want a geom_hline here.

You seem to have made things difficult for yourself by manually reversing the axes in the plot from your previous question. You could have done this much more simply by adding + coord_flip() to the previous plot.

The following revised code works:

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")

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

The values of x in the geom_hline call are too high to be for the y axis, I think it is intended to be geom_vline.
@IanCampbell the formulae for the segments was also wrong.
Ah, that makes much more sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.