3

Suppose I have the following data frames :

a <- c(1,2,2,3,7,9,8,3,7,9)
b <- c(1:10)
df1 <- data.frame(a,b)
c <- c(2,5,4)
d <- c(3,6,5)
df2 <- data.frame(c,d)

And I generate a scatter plot of df1 with plotly :

p <- plot_ly(data=df1, x=a, y=b)

Then how can I add points from df2 to p with, for instance, a different color?

1

1 Answer 1

2

You can try:

colnames(df2) <- c("a", "b")
df1$gr <- 1
df2$gr <- 2
df <- rbind(df1, df2)
plot_ly(data = df, x = ~a, y = ~b, color = ~factor(gr), type = "scatter")

Create one data.frame including all data and a grouping factor, then plot.

There are other ways. You will find all of them here:https://plot.ly/r/line-and-scatter/

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

2 Comments

I don't see other ways of adding points to previous plot on the webpage. Actually I can't find things relevant anywhere in the official documentation of Plotly.
You can add points to a previous plot by using plot_ly() %>% add_trace(). It's under the segment "Plotting Markers and Lines".

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.