将水平线添加到ggplot()以指定x轴的间隔 [英] Add horizontal line to ggplot() for specified interval of x axis

查看:521
本文介绍了将水平线添加到ggplot()以指定x轴的间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为现有的图表添加水平线条,但我只想绘制x轴某些间隔的线条。

例如,

例如我想在X = 1:5和y = 50处有一条水平线。



我会用 existing_plot + geom_hline(yintercept = 50 )



是否可以以某种方式指定x值?

解决方案您可以使用 geom_segment()添加具有您自己定义的起点和终点的线段(不仅是水平/垂直线)。

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


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.

For example I would like to have a horizontal line at X=1:5 and y=50.

I would use existing_plot+geom_hline(yintercept = 50)

Is it also possible to specify the x values somehow?

解决方案

You can use geom_segment() to add line segment with your own defined starting and ending points (not only horizontal/vertical lines).

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

这篇关于将水平线添加到ggplot()以指定x轴的间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆