Black Lives Matter. Please consider donating to Black Girls Code today.

Customising colours for a scatter plot with different modes

Hello,

I’ve tried to generate a scatter plot with markers and lines as mode arguments:

st <- c(1,2,3,4,5);nd <- c(1,2,3,4,5);rd <- c(-1,-2,-3,-4,-5);th <- c(1,2,3,4,5)
plot_ly(x = st, y = nd, type = “scatter”, mode = “lines”, name = “line”,
line = list(color = “green”))%>%
add_trace(x = rd, y = th, type = “scatter”, mode = “markers”, name = “markers”,
marker = list(color = “red”))
Default colours work fine, adjustment results in this:

A line object has been specified, but lines is not in the mode Adding lines to the mode…<<

Thanks for any tips.

Re-freshing the topic - I found the same problem for histograms, when the kernel density is added.

Hey @JW52

There are two ways you can do this. (1) add inherit = F to the 2nd trace:

st <- c(1,2,3,4,5)
nd <- c(1,2,3,4,5)
rd <- c(-1,-2,-3,-4,-5)
th <- c(1,2,3,4,5)

plot_ly(x = st, y = nd, type = "scatter", mode = "lines", name = "line",
        line = list(color = "green"))%>%
  add_trace(x = rd, y = th, type = "scatter", mode = "markers", name = "markers",
            marker = list(color = "red"), inherit = F)

or (2):

plot_ly() %>%
  add_trace(x = st, y = nd, type = "scatter", mode = "lines", name = "line",
        line = list(color = "green"))%>%
  add_trace(x = rd, y = th, type = "scatter", mode = "markers", name = "markers",
            marker = list(color = "red"))

Thank you very much!