Open In App

Add Vertical and Horizontal Lines to ggplot2 Plot in R

Last Updated : 20 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to add Vertical and Horizontal lines to the plot using ggplot2 in R Programming Language.

Adding Vertical Line To R Plot using geom_vline()

For adding the Vertical line to the R plot, geom_vline() draws a vertical line at a specified position.

Syntax: geom_vline(xintercept)

Parameter: here xintercept is used control the X position of line.

Return: Vertical line on R plot.

Example: To add the vertical line on the plot, we simply add geom_vline() function to ggplot2() function and pass the xintercept, which basically has a location on X-axis, where we actually want to create a vertical line. Here we set 2011 to the xintercept.

R




# load ggplot2 package
library(ggplot2)
 
# Create a dataframe for Plot data
data <- data.frame(year = c(2011, 2012, 2013, 2014, 2015),
                   point = c(10, 20, 30, 40, 50))
 
# Plot the scatter plot with vertical line
# at X=2011
ggplot(data, aes(year, point)) +   
  ggtitle("Point Chart")+
  geom_point(aes(size = 1.0), col = "green")+
  geom_vline(xintercept = 2011)


Output:

with verticle line

Scatter plot with vertical line at X=2011

Adding Horizontal Line To R Plot using geom_hline()

And for adding Horizontal lines to the R plot, we will use geom_hline() function:

Syntax: geom_hline(yintercept)

Parameter: here yintercept is used control the y position of line.

Return: Horizontal line on R plot.

Example: To add the horizontal line on the plot, we simply add geom_hline() function to ggplot2() function and pass the yintercept, which basically has a location on the Y axis, where we actually want to create a vertical line. Here we set 20 to the xintercept.

R




# load ggplot2 package
library(ggplot2)
 
# Create a dataframe for Plot data
data <- data.frame(year = c(2011, 2012, 2013, 2014, 2015),
                   point = c(10, 20, 30, 40, 50))
 
# Plot the scatter plot with horizontal
# line at Y=20
ggplot(data, aes(year, point)) +   
  ggtitle("Point Chart")+
  geom_point(aes(size = 1.0), col = "green")+
  geom_hline(yintercept = 20)


Output:

with horizontal line

Scatter plot with Horizontal line at Y=20

Adding both vertical and horizontal lines to R plot

To add both lines, we add both geom_vline() and geom_hline() function to ggplot() function and set the values of xintercept and yintercept. 

Example: Here we will draw the vertical line at X=2012 and the vertical line at Y=20.

R




# load ggplot2 package
library(ggplot2)
 
# Create a dataframe for Plot data
data <- data.frame(year = c(2011, 2012, 2013, 2014, 2015),
                   point = c(10, 20, 30, 40, 50))
 
# Plot the scatter plot with both lines
# at X=2012 and Y=20
ggplot(data, aes(year, point)) +   
  ggtitle("Point Chart")+
  geom_point(aes(size = 1.0), col = "green")+
  geom_vline(xintercept = 2012)+
  geom_hline(yintercept = 20)


Output:

with both lines

Scatter plot with both lines at X=2012 and Y=20

 

Adding multiple vertical and horizontal lines to R plot using seq() function

We will use seq() function to represent the xintercept and yintercept and all other settings will remain the same as example 3.

Syntax: seq(from, to, by)

Parameters:

  • from : it is used to represent starting length.
  • to:  it is used to represent ending length.
  • by: it represents increment of the system.

Return: well-formed sequence.

Example: Here we’re drawing vertical lines from 2011 to 2015 with a difference of 0.5 and horizontal lines from 10 to 50 with a difference of 5.

R




# load ggplot2 package
library(ggplot2)
 
# Create a dataframe for Plot data
data <- data.frame(year = c(2011, 2012, 2013, 2014, 2015),
                   point = c(10, 20, 30, 40, 50))
 
# Plot the scatter plot with multiple
# vertical and horizontal lines
ggplot(data, aes(year, point)) +   
  ggtitle("Point Chart")+
  geom_point(aes(size = 1.0), col = "green")+
  geom_vline(xintercept = seq(from=2011, to=2015, by = 0.5))+
  geom_hline(yintercept = seq(from=10, to=50, by = 5))


Output:

Scatter plot with multiple verticle and horizontal lines

Scatter plot with multiple vertical and horizontal lines



Previous Article
Next Article

Similar Reads

Remove Vertical or Horizontal Gridlines in ggplot2 Plot in R
In this article, we are going to learn how to remove Vertical or Horizontal Gridlines in the background of a ggplot2 Plot in R programming language. To understand how to remove Vertical or Horizontal Gridlines, first import all the required libraries to the working space and create a dataframe. Let us first create a regular plot so that the differe
2 min read
How to Add Vertical Lines By a Variable in Multiple Density Plots with ggplot2 in R
In this article, we will discuss how to add vertical lines by a variable in multiple density plots with ggplot2 package in the R Programming language. To do so first we will create multiple density plots colored by group and then add the line as a separate element. Basic Multiple Density Plot: To make multiple density plots with coloring by variabl
3 min read
Set Axis Limits of ggplot2 Facet Plot in R - ggplot2
In this article, we will discuss how to set the axis limits of the ggplot2 facet plot in the R programming language. Method 1: Set axis limits of ggplot2 facet plot with Free Scales Here the role of the ggplot2 package is to plot the facet plot and provide some functionalities to the user, further the user needs to set the argument of the scales fu
5 min read
Change Spacing Between Horizontal Legend Items of ggplot2 Plot in R
In this article, we are going to see how to change the space between horizontal legend items using the ggplot2 bar plot in R programming language. Here we are using a bar graph the same can be done to any other plot. For creating a simple bar plot we will use the function geom_bar( ). Syntax: geom_bar(stat, fill, color, width) Parameters : stat : S
2 min read
Draw Vertical Line to X-Axis of Class Date in ggplot2 Plot in R
In this article, we will see how to draw Vertical Line to X-Axis of Class Date in ggplot2 Plot in R programming language. Here we are using Scatter Plot, you can draw any graph as per your requirement. First, load the ggplot2 package by using the library() function. Now we will create a DataFrame with two vectors for X and Y-Axis respectively. Here
4 min read
How to put text on different lines to ggplot2 plot in R?
ggplot2 is a plotting package in R programming language that is used to create complex plots from data specified in a data frame. It provides a more programmatic interface for specifying which variables to plot onto the graphical device, how they are displayed, and general visual properties. In this article, we will discuss how to put text on diffe
3 min read
How to create a plot using ggplot2 with Multiple Lines in R ?
In this article, we will discuss how to create a plot using ggplot2 with multiple lines in the R programming language. Method 1: Using geom_line() function In this approach to create a ggplot with multiple lines, the user need to first install and import the ggplot2 package in the R console and then call the ggplot() and the geom_line() functions i
3 min read
Annotate Multiple Lines of Text to ggplot2 Plot in R
In this article, we will see how to annotate Multiple Lines of text to ggplot2 Plot in R programming language. Let us first create a regular plot so that the difference is apparent, Example: C/C++ Code # Load Package library(&quot;ggplot2&quot;) # Create a DataFrame DF &lt;- data.frame(X = runif(100, min=0, max=100), Y = runif(100, min=0, max=100))
2 min read
Plot lines from a list of dataframes using ggplot2 in R
In this article, we will discuss how to plotlines in ggplot2 from a list of data frames in the R programming language. The ggplot2 package is used to visualize and analyze the data. The package can be downloaded and installed using the following command in R : install.packages("ggplot2")The ggplot2 method in R is used to do graph visualizations usi
5 min read
Plot Shaded Area between vertical lines in R
Plotting a shaded area between vertical lines means to put a vertical rectangle on a different color plot as compared to the rest of the plot area. We create this rectangle depending on the conditional values of the x-axis and represent the pivot area on the basis of characteristics of the data. This can be obtained using R and is described how in
1 min read
Article Tags :