3
votes

I am using Plotly to visualize some data (below):

y1 <- rnorm(100, mean = 5)
y2 <- rnorm(100, mean = -5)
x <- c(1:100)

data <- data.frame(x, y1, y2)

I want to have two plots, a scatterplot for y1 (just markers), and a line plot for y2 (just line, no markers).

I have this working...

plot_ly(data, x = ~x, y = ~y1, name = 'symbol only', type = 'scatter', mode = 'markers') %>%
  add_trace(y = ~y2, name = 'line only', mode = 'lines',
             line = list(shape = 'spline', color = 'rgb(200, 12, 46)', width = 2))

However, I want to change the color and symbol of the markers in y1. When I do that, it adds markers to y2, which I do not want. How can I fix this?

plot_ly(data, x = ~x, y = ~y1, name = 'symbol only', type = 'scatter', mode = 'markers',
             symbol = 8) %>%
  add_trace(y = ~y2, name = 'line only (why markers added?)', mode = 'lines',
             line = list(shape = 'spline', color = 'rgb(200, 12, 46)', width = 2))
1

1 Answers

3
votes

You could first create an empty Plotly object/a Plotly object with just your x-values

plot_ly(data, x = ~x) %>%

and then add each trace separately.

symbol in add_trace is used to specify the symbol source while symbols stores the actual symbols. In your case you would need to pass the symbol type in marker, i.e. marker=list(symbol = 8).

library(plotly)

y1 <- rnorm(100, mean = 5)
y2 <- rnorm(100, mean = -5)
x <- c(1:100)

data <- data.frame(x, y1, y2)


plot_ly(data, x = ~x) %>%
  add_trace(y = ~y1, name = 'symbol only', type = 'scatter', mode = 'markers', marker=list(symbol = 8)) %>%
  add_trace(y = ~y2, name = 'line only', type='scatter', mode = 'lines',
            line = list(shape = 'spline', color = 'rgb(200, 12, 46)', width = 2))

enter image description here