Show Sidebar Hide Sidebar

Inset Plots in R

How to make an inset plot in R with Plotly

New to Plotly?

Plotly's R library is free and open source!
Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode.
We also have a quick-reference cheatsheet (new!) to help you get started!

Version Check

Version 4 of Plotly's R package is now available!
Check out this post for more information on breaking changes and new features available in this version.

## [1] '4.8.0'

Basic Inset

See also the chapter on subplots in the plotly book

p <- plotly::plot_ly()
p <- plotly::add_trace(p, x = c(1, 2, 3), y = c(4, 3, 2), mode='lines')
p <- plotly::add_trace(p, x = c(20, 30, 40), y = c(30, 40, 50), xaxis='x2', yaxis='y2', mode='lines')
p <- plotly::layout(p, xaxis2 = list(domain = c(0.6, 0.95), anchor='y2'),
         yaxis2 = list(domain = c(0.6, 0.95), anchor='x2'))

# Create a shareable link to your chart
# Set up API credentials:
chart_link = api_create(p, filename="subplots-1")