Issue changing marker color in R plotly scatter plot

自作多情 提交于 2020-02-29 07:10:31

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/51966920/issue-changing-marker-color-in-r-plotly-scatter-plot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!