slabofguinness
slabofguinness

Reputation: 821

Issue changing marker color in R plotly scatter plot

The following code produces a plot with red markers:

trace_0 <- rnorm(100, mean = 5)
trace_1 <- rnorm(100, mean = 0)
x <- c(1:100)

data <- data.frame(x, trace_0, trace_1)

plot_ly(data, x = ~x, y = ~trace_0, type = 'scatter', mode = 'markers',
        marker = list(color='red'))

enter image description here

If I try to add a line I get unwanted red markers also:

plot_ly(data, x = ~x, y = ~trace_0, type = 'scatter', mode = 'markers',
        marker = list(color='red'))%>%
  add_trace(y = ~trace_1, mode = 'lines')

enter image description here

and a complaint from plot_ly:

A marker object has been specified, but markers is not in the mode
Adding markers to the mode...

Could somebody please explain where I'm going wrong here? Thanks.

Upvotes: 0

Views: 2396

Answers (1)

MLavoie
MLavoie

Reputation: 9836

This will give you what you want:

plot_ly(data, x = ~x) %>%
add_trace(y = ~trace_0, mode = 'markers', marker = list(color='red')) %>%
  add_trace(y = ~trace_1, mode = 'lines')

Upvotes: 1

Related Questions