在R散点图中发布更改标记颜色的问题

时间:2018-08-22 12:28:19

标签: r plotly

以下代码生成带有红色标记的图:

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

如果我尝试添加一行,也会出现不必要的红色标记:

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

和plot_ly的投诉:

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

有人可以解释一下我在哪里出问题了吗?谢谢。

1 个答案:

答案 0 :(得分:1)

这将为您提供想要的东西:

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