# Adding Horizontal & Vertical Lines to plotly Graph in R

This tutorial provides several examples to add custom lines and shapes to plotly graphs in the R programming language.

Note: This article was created in collaboration with Kirby White. Kirby is a Statistics Globe author, innovation consultant, and data science instructor. His Ph.D. is in Industrial-Organizational Psychology. You can read more about Kirby here!

## Packages and Example Data

If you have not already done so, install and load these packages:

Weâ€™ll use the mtcars dataset for this example, which is already included in R.

df <- datasets::mtcars

Letâ€™s build a basic scatterplot that weâ€™ll store in an object called fig, which weâ€™ll modify throughout the tutorial to demonstrate the features:

fig <-
df %>%
plot_ly(
x = ~disp,
y = ~mpg,
color = ~factor(cyl),
type = "scatter",
mode = "markers"
) %>%
layout(title=list(text='<b>Cylinders</b>'))

## Single Lines

Letâ€™s start by adding a horizontal line to show the mean mpg for all cylinders and displacement values.

fig %>%
y = mean(df\$mpg),
x = range(df\$disp),
line = list(
color = "grey"
),
inherit = FALSE,
showlegend = FALSE
)

The x and y arguments can accept a single value or a vector of two numeric values. In this case, we passed a single value to the y argument by calculating the mean mpg. The range function returned a two-element vector with the min and max disp to have the line stretch across all points in the graph. Note that you may need to add na.rm = TRUE to your mean function to ignore missing values.

Creating a vertical line is the same, but flipping the inputs to the x and y arguments:

fig %>%
y = range(df\$mpg),
x = mean(df\$disp),
line = list(
color = "grey"
),
inherit = FALSE,
showlegend = FALSE
)

## Multiple Lines

You can add multiple add_lines() elements to a graph to mix-and-match multiple lines across groups. This example shows how to create a vertical line within each group of vehicles.

fig %>%
y = range(df[df\$cyl == 4,"mpg"]),
x = mean(df[df\$cyl == 4,"disp"]),
inherit = FALSE,
showlegend = FALSE,
line = list(color = "gray")
) %>%
y = range(df[df\$cyl == 6,"mpg"]),
x = mean(df[df\$cyl == 6,"disp"]),
inherit = FALSE,
showlegend = FALSE,
line = list(color = "gray")
) %>%
y = range(df[df\$cyl == 8,"mpg"]),
x = mean(df[df\$cyl == 8,"disp"]),
inherit = FALSE,
showlegend = FALSE,
line = list(color = "gray")
)

This is an explicit way of adding several lines, but it can be cumbersome if you have much more than this! Letâ€™s look at a more complicated â€” but scalable â€” way of adding several lines. Weâ€™ll create a function to add a vertical line to each group, regardless of how many groups there are.

vertical_lines <- function(fig, groups, x_variable, y_variable, data){

return_fig <- fig

for (i in unique(data[,`groups`])){
temp_dat <-
data[data[,`groups`] == i,c(`x_variable`, `y_variable`)]

# print(dim(temp_dat))
return_fig <-
return_fig %>%
y = range(temp_dat[,2], na.rm = TRUE),
x = mean(temp_dat[,1], na.rm = TRUE),
inherit = FALSE,
showlegend = FALSE
)
}

return(return_fig)
}

We can now pass our baseline figure (fig) into the function and it will add a vertical line to each group.

vertical_lines(fig = fig,
groups = 'cyl',
data = df,
x_variable = 'disp',
y_variable = 'mpg')

## Line Formatting

There are many ways to format lines in plotly. Letâ€™s use an example from above, but change several characteristics of each line. This example should give you ideas about ways to change your graphs!

fig %>%
y = range(df[df\$cyl == 4, 'mpg']),
x = mean(df[df\$cyl == 4, 'disp']),
inherit = FALSE,
showlegend = FALSE,
line = list(color = "gray",
width = 15,
dash = 'dot')
) %>%
y = range(df[df\$cyl == 6, 'mpg']),
x = mean(df[df\$cyl == 6, 'disp']),
inherit = FALSE,
showlegend = FALSE,
line = list(color = "red",
width = 5,
dash = 'dash')
) %>%
y = range(df[df\$cyl == 8, 'mpg']),
x = mean(df[df\$cyl == 8, 'disp']),
inherit = FALSE,
showlegend = FALSE,
line = list(color = "rgb(115,112,110)",
dash = 'solid'))
)

## Further Resources

You may find a video introduction to the plotly package below:

Furthermore, you can check out these other articles for more detailed examples and videos of these popular charts in plotly:

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Top