简体   繁体   中英

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'))

在此处输入图片说明

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')

在此处输入图片说明

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.

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')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM