4
#Input data
df1<- data.frame(Gradient=rep(c(0,5,10,15), each=5),
                 load=c(0,4.4,10.7,17,21.4),
                vo2max=c(28.0,28.2,31.0,32.0,34.6,41.0,41.1,45.4,48.8,50.5,56.3,57.0,
                         63.6,66.8,69.9,71.3,75.0,82.1,85.5,89.3))
head(df1)
sp<-ggplot(data=df1, aes(x=load, y=VO2max, group=Gradient)) +
  geom_line()+
  geom_point()
#Horizontal line segment
sp+geom_segment(aes(x=0,y=50,xend=25,yend=50))
sp+geom_segment(aes(x=0,y=60,xend=25,yend=60))``
sp+geom_segment(aes(x=0,y=75,xend=25,yend=75))

How to draw the three line segments in the same graph? I'm able to draw only one line segment at a time.

1
  • 1
    (1) VO2max or vo2max, please give us usable code. (2) sp+geom_segment(...)+geom_segment(...)+geom_segment(...)? Commented Jun 23, 2020 at 14:03

3 Answers 3

5

Put the data into a data.frame:

line_df <- data.frame(
  x = 0,
  y = c(50, 60, 75),
  xend = 25,
  yend = c(50, 60, 75)
)

sp +
  geom_segment(
    data = line_df, 
    mapping = aes(x=x, y=y, xend=xend, yend=yend), 
    inherit.aes = FALSE
  )
Sign up to request clarification or add additional context in comments.

Comments

2

Just add them all together.

sp +
  geom_segment(aes(x=0,y=50,xend=25,yend=50)) +
  geom_segment(aes(x=0,y=60,xend=25,yend=60)) +
  geom_segment(aes(x=0,y=75,xend=25,yend=75))

enter image description here

Comments

1
ggplot(data=df1, aes(x=load, y=vo2max, group=Gradient)) +
  geom_line()+
  geom_point() + 
  geom_segment(
    data=tibble(), 
    aes(
      x=c(0, 0, 0),
      y=c(50, 60, 75),
      xend=c(25, 25, 25),
      yend=c(50, 60, 75)
    ), 
    inherit.aes=FALSE
  )

Also consider geom_hline().

Comments

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.