简体   繁体   English

为x轴的指定间隔添加水平线到ggplot()

[英]Add horizontal line to ggplot() for specified interval of x axis

I would like to add horizontal lines to an existing plot, but I would only like to plot the line for certain intervals of the x-axis. 我想在现有的绘图中添加水平线,但我只想绘制x轴的某些间隔的线。

For example I would like to have a horizontal line at X=1:5 and y=50. 例如,我想在X = 1:5和y = 50时有一条水平线。

I would use existing_plot+geom_hline(yintercept = 50) 我会使用existing_plot+geom_hline(yintercept = 50)

Is it also possible to specify the x values somehow? 是否也可以以某种方式指定x值?

You can use geom_segment() to add line segment with your own defined starting and ending points (not only horizontal/vertical lines). 您可以使用geom_segment()添加具有您自己定义的起点和终点(不仅是水平/垂直线)的线段。

ggplot(mtcars,aes(mpg,qsec))+geom_point()+
  geom_segment(aes(x=15,xend=20,y=18,yend=18))

在此输入图像描述

You can use geom_line : 你可以使用geom_line

qplot(x=x,y=y,data=data.frame(x=1:10,y=100:1)) +
  geom_line(data=data.frame(x=1:5,y=50))

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM