- 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 a horizontal line in ggplot2 graph with larger width in R?
To create a horizontal line in ggplot2 graph with larger width in R, we can follow the below steps −
- First of all, create a data frame.
- Then, create a plot using ggplot2.
- After that, create the same plot with geom_hline function having horizontal line defined with yintercept and its width defined with size argument
Create the data frame
Let's create a data frame as shown below −
> x<-sample(1:50,20) > y<-sample(1:100,20) > df<-data.frame(x,y) > df
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
x y 1 1 43 2 21 17 3 36 9 4 43 40 5 8 19 6 34 75 7 23 29 8 44 84 9 33 8 10 24 87 11 45 20 12 17 86 13 9 60 14 50 35 15 46 3 16 49 14 17 47 18 18 19 1 19 25 16 20 32 5
Create the plot using ggplot2
Let’s create a scatterplot between x and y −
> x<-sample(1:50,20) > y<-sample(1:100,20) > df<-data.frame(x,y) > library(ggplot2) > ggplot(df,aes(x,y))+geom_point()
Output
Create the plot with wider horizontal line
Using geom_hline to create the horizontal line in the above plot with yintercept = 60 and size = 2 −
> x<-sample(1:50,20) > y<-sample(1:100,20) > df<-data.frame(x,y) > library(ggplot2) > ggplot(df,aes(x,y))+geom_point()+geom_hline(yintercept=60,size=2)
Output
- Related Articles
- How to create a line chart using ggplot2 with larger width in R?
- How to create a horizontal line in ggplot2 graph with different color in R?
- How to create a dashed horizontal line in a ggplot2 graph in R?
- How to create a horizontal bar graph using ggplot2 in R?
- How to create a line chart in R using plot function with larger width?
- How to create quantile regression plot with larger width of lines using ggplot2 in R?
- How to create horizontal legend using ggplot2 in R?
- How to create a line chart using ggplot2 with a vertical line in R?
- How to create a bar graph using ggplot2 without horizontal gridlines and Y-axes labels in R?
- How to create horizontal line in xyplot in R?
- How to create facetted plot with facets in horizontal direction using ggplot2 in R?
- How to create a ggplot2 graph in R without showing values?
- How to create horizontal line for a range of values in a plot created by using ggplot2 in R?
- How to create a graph in R using ggplot2 with all the four quadrants?
- How to add a horizontal line to the plot created by ggplot2 in R?

Advertisements