Add Horizontal or Vertical Line in Plotly Using R
Last Updated :
23 Jul, 2025
Plotly is a powerful and versatile plotting library in R that enables the creation of interactive and publication-quality visualizations. One common requirement in data visualization is to add reference lines, such as horizontal or vertical lines, to a plot to highlight specific values or thresholds. This article will cover how to add horizontal and vertical lines to a Plotly plot using R, providing both the theory behind the functionality and practical examples.
Add Horizontal or Vertical Line in Plotly
In Plotly, adding reference lines involves using R Programming Language and the layout
function to modify the plot’s layout by adding shapes
. These shapes can include lines, rectangles, and other geometric forms. For horizontal and vertical lines, the type
of the shape is set to "line"
, and the coordinates for the start and end points of the line are specified. Key Parameters for Shapes are:
- type: Specifies the type of shape (
"line"
for lines). - x0: The starting x-coordinate of the line.
- x1: The ending x-coordinate of the line.
- y0: The starting y-coordinate of the line.
- y1: The ending y-coordinate of the line.
- line: A list defining the line’s properties, such as color, width, and dash style.
Example 1: Adding a Horizontal Line
A horizontal line spans across the x-axis at a specific y-coordinate. Thus, y0
and y1
are set to the desired y-coordinate, while x0
and x1
are set to cover the range of the x-axis. Here, we will create a simple scatter plot and add a horizontal line at y = 5
.
R
# Load necessary libraries
library(plotly)
# Create sample data
x <- 1:10
y <- c(3, 5, 2, 8, 7, 6, 4, 5, 9, 1)
# Create the scatter plot
p <- plot_ly(x = ~x, y = ~y, type = 'scatter', mode = 'markers') %>%
layout(
shapes = list(
list(
type = "line",
x0 = 0,
x1 = 10,
y0 = 5,
y1 = 5,
line = list(color = "red", width = 2, dash = "dash")
)
)
)
# Display the plot
p
Output:
Add Horizontal or Vertical Line in Plotly Using RExample 2: Adding a Vertical Line
A vertical line spans across the y-axis at a specific x-coordinate. Thus, x0
and x1
are set to the desired x-coordinate, while y0
and y1
are set to cover the range of the y-axis Next, we will create a similar scatter plot and add a vertical line at x = 5
.
R
# Create the scatter plot
p <- plot_ly(x = ~x, y = ~y, type = 'scatter', mode = 'markers') %>%
layout(
shapes = list(
list(
type = "line",
x0 = 5,
x1 = 5,
y0 = 0,
y1 = 10,
line = list(color = "blue", width = 2, dash = "dot")
)
)
)
# Display the plot
p
Output:
Add Horizontal or Vertical Line in Plotly Using RExample 3: Adding Both Horizontal and Vertical Lines
In this example, we will add both a horizontal line at y = 5
and a vertical line at x = 5
.
R
# Create the scatter plot
p <- plot_ly(x = ~x, y = ~y, type = 'scatter', mode = 'markers') %>%
layout(
shapes = list(
list(
type = "line",
x0 = 0,
x1 = 10,
y0 = 5,
y1 = 5,
line = list(color = "red", width = 2, dash = "dash")
),
list(
type = "line",
x0 = 5,
x1 = 5,
y0 = 0,
y1 = 10,
line = list(color = "blue", width = 2, dash = "dot")
)
)
)
# Display the plot
p
Output:
Add Horizontal or Vertical Line in Plotly Using RConclusion
Adding horizontal or vertical lines to a Plotly plot in R is a straightforward process that enhances the interpretability of the visualization. By utilizing the layout
function and defining shapes
, you can easily add reference lines to highlight important values or thresholds. This capability is invaluable for creating more informative and effective visualizations.
In this article, we have demonstrated the theory behind adding shapes in Plotly and provided practical examples to add horizontal and vertical lines to scatter plots. These examples can be adapted and extended to suit various types of plots and analysis needs.