2

I'm just starting to use plotly and I haven't been able to do something as simple as adding objects to an existing plot. How can I add line segments to an existing empty plot?

I've tried this, but every call of add_segment creates a new plot with only one segment:

p<-plot_ly()
add_segment(p,x=0,y=1,x=5,y=1)
add_segment(p,x=0,y=4,x=5,y=4)

What am I doing wrong?

1 Answer 1

2

The answer: use the %>% operator like this:

p <- plot_ly(data, x = ~x)
p <- p %>% add_trace(y = ~trace_0, name = 'trace 0',mode = 'lines')
p

The details:

One approach is to use the piping %>% operator from this example where the following plot is produced by the code below:

Plot:

enter image description here

Code:

library(plotly)

set.seed(1)
trace_0 <- rnorm(100, mean = 5)
trace_1 <- rnorm(100, mean = 0)
trace_2 <- rnorm(100, mean = -5)
x <- c(1:100)

data <- data.frame(x, trace_0, trace_1, trace_2)

p <- plot_ly(data, x = ~x) %>%
  add_trace(y = ~trace_0, name = 'trace 0',mode = 'lines') %>%
  add_trace(y = ~trace_1, name = 'trace 1', mode = 'lines+markers') %>%
  add_trace(y = ~trace_2, name = 'trace 2', mode = 'markers')
p

Step by step using %>%:

Code Step 1:

# step 1
p <- plot_ly(data, x = ~x)

# plot
p

Plot step 1:

enter image description here

Code step 2:

# step 1
p <- plot_ly(data, x = ~x)

# step 2
p <- p %>% add_trace(y = ~trace_0, name = 'trace 0',mode = 'lines')

# plot
p

Plot step 2:

enter image description here

Code all steps:

# step 1
p <- plot_ly(data, x = ~x)

# step 2
p <- p %>% add_trace(y = ~trace_0, name = 'trace 0',mode = 'lines')

# step 3
p <- p %>% add_trace(y = ~trace_1, name = 'trace 1', mode = 'lines+markers')

# step 4
p <- p %>% add_trace(y = ~trace_2, name = 'trace 2', mode = 'markers')

# plot
p

Plot all steps:

enter image description here

Edit: Response to comments

Is there any way to add elements without using the pipe?

Not to my knowledge, but I'm hoping someone proves me wrong. Since this is an [R] question, I'm guessing you're more familiar with building ggplot plots stepwise like this:

library(ggplot2)
g <- ggplot(diamonds, aes(x=carat, y=price, color=cut)) + geom_point()
g <- g + geom_smooth()
g

The + approach will not work for plotly though:

Code:

# step 1
p <- plot_ly(data, x = ~x)

# step 2
p <- p + add_trace(y = ~trace_0, name = 'trace 0',mode = 'lines')

Error:

Error in add_data(p, data) : argument "p" is missing, with no default

And there's absolutely no reason to fear the %>% operator. a %>% b simply means something along the lines of Take a, and do b with it. To my knowledge, this was first seen in the magrittr package, and is widely used in the fantastic dplyr package. I'm mostly messing around with python at the time, but this really makes me miss R

Sign up to request clarification or add additional context in comments.

7 Comments

I don't understand very well what's the difference, technically the pipe operator shouldn't make function(x) the same as x %>% function()? I'm gonna try with add_trace instead to check, thanks!
Only on phone right now, but I'll check back in tomorrow for some more details.
try: p <- plot_ly(data, x = ~x)
and then: p %>% add_trace(y = ~trace_0, name = 'trace 0',mode = 'lines')
Is there any way to add elements without using the pipe?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.