Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding points to an existing scatter plot with plotly

Tags:

plot

r

plotly

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?

like image 745
J2Letters Avatar asked Dec 27 '25 20:12

J2Letters


1 Answers

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/

like image 181
Roman Avatar answered Dec 30 '25 15:12

Roman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!