dat = c(1:100)
fig1 = plot_ly(x = ~x)
fig2 = fig1%>%add_trace(y=~rnorm(100), mode= "lines")
The outputs for "fig1" and "fig2" are: and respectively.
I could also get "fig2" using the following code:
fig3 = plot_ly(x= ~dat, y = ~rnorm(100))%>%add_lines()
Can someone explain what's the difference individual traces like (add_histogram, add_lines etc..) and the function add_trace with an appropriate mode?
add_histogram
, add_lines
etc. are convenience functions with a preset trace type (internally they are setting the according type and call add_trace_classed
- type eg add_lines
in the console to inspect the function).
add_trace
is a general function to creates traces of any available type .
Your third option to create traces is via the plot_ly()
function itself.
Please see the example section of ?add_trace
for additinal information:
the
plot_ly()
function initiates an object, and if no trace type is specified, it sets a sensible default p <- plot_ly(economics, x = ~date, y = ~uempmed) psome
add_*()
functions are a specific case of a trace type for example,add_markers()
is a scatter trace with mode of markers add_markers(p)
If you don't specify a trace type in plot_ly
it is set based on the data you provided:
library(plotly)
dat = data.frame(x = 1:100)
fig1 = plot_ly(data = dat, x = ~x)
# No trace type specified:
# Based on info supplied, a 'histogram' trace seems appropriate.
# Read 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")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.