3
votes

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
Check out the plotly tutorial plot.ly/r/line-and-scatteremilliman5

1 Answers

1
votes

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/