r - 如何在ggplot中绘制多条线段

标签 r ggplot2

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

如何在同一个图中绘制三个线段? 我一次只能绘制一条线段。

最佳答案

将数据放入 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
  )

关于r - 如何在ggplot中绘制多条线段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62536499/

相关文章:

r - 如何仅获取 ID 的日期早于具有相同 ID 的另一行的行?

R:如何存储向量的向量

r - dmvnorm 中 x 参数的目的是什么?

r - 使用 for 循环生成 ggplots 网格

r - 如何在ggplot2中显示obs的方向(标题)

python - Pandas /matplotlib : faceting bar plots

r - 如何使用ggplot2使抖动点居中?

将多列中的行 id 的值替换为 dplyr case_when

r - 使用 ggplot2 绘制 xts 对象

R: grid.arrange 边缘图到 ggplot2 "heatmap"(geom_tile)