- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create horizontal line for a range of values in a plot created by using ggplot2 in R?
To display a particular part of independent variable in a plot, we might want to use a horizontal line. This will make the plot look different and get the attention of the viewer. To create a horizontal line in a plot, we can use geom_line function but we need to pass the values in a data frame format for which we want to create the horizontal line.
Consider the below data frame −
Example
x<-rpois(10,6) y<-rpois(10,8) df<-data.frame(x,y) df
Output
x y 1 6 10 2 7 17 3 5 10 4 2 10 5 6 12 6 6 9 7 4 5 8 12 5 9 5 8 10 1 8
Loading ggplot2 package and creating point chart between x and y −
Example
library(ggplot2) ggplot(df,aes(x,y))+geom_point()
Output
Creating the point chart between x and y by displaying a line between 6 to 8 of x when y is 10 −
Example
ggplot(df,aes(x,y))+geom_point()+geom_line(data=data.frame(x=6:8,y=10))
Output
- Related Articles
- How to add a horizontal line to the plot created by ggplot2 in R?
- How to change legend values in a bar plot created by using ggplot2 in R?
- How to create regression model line in a scatterplot created by using ggplot2 in R?
- How to add a citation in a plot created by using ggplot2 in R?
- How to highlight text inside a plot created by ggplot2 using a box in R?
- How to create a dashed horizontal line in a ggplot2 graph in R?
- How to display regression slope using model in a plot created by ggplot2 in R?
- How to display regression intercept using model in a plot created by ggplot2 in R?
- How to extract data from a plot created by ggplot2 in R?
- How to create a plot using ggplot2 by including values greater than a certain value in R?
- How to align the text horizontally in a bar plot created by using ggplot2 in R?
- How to make all text size same in a plot created by using ggplot2 in R?
- How to create facetted plot with facets in horizontal direction using ggplot2 in R?
- How to create a horizontal bar graph using ggplot2 in R?
- How to create a dot plot using ggplot2 in R?

Advertisements