add_histogram
, add_lines
, and other similar functions are designed for convenience, each with a preset trace type. Internally, these functions set the corresponding type and call add_trace_classed
. To inspect the function behind a specific type, you can use add_lines
in the console.
add_trace
is a versatile function that allows you to create traces of any available type.
An alternative method to create traces is by using the plot_ly()
function directly.
For additional information, please refer to the example section of ?add_trace
:
The plot_ly()
function initializes an object, setting a default plot if no trace type is specified. For instance: p <- plot_ly(economics, x = ~date, y = ~uempmed) p
Some add_*()
functions represent specific cases of trace types. For example, add_markers()
creates a scatter trace with markers mode. Use add_markers(p) to implement this.
If you do not specify a trace type in plot_ly
, it will be determined based on the supplied data:
library(plotly)
dat = data.frame(x = 1:100)
fig1 = plot_ly(data = dat, x = ~x)
# Trace type not specified:
# Based on the provided information, a 'histogram' trace is recommended.
# Learn more about this trace type -> https://plotly.com/r/reference/#histogram
fig1a = plot_ly(data = dat, x = ~x, type = "scatter", mode = "lines")
fig2 = fig1 %>% add_trace(y=~rnorm(100), mode = "lines")