我正在使用Plotly可视化一些数据(如下):
y1 <- rnorm(100, mean = 5)
y2 <- rnorm(100, mean = -5)
x <- c(1:100)
data <- data.frame(x, y1, y2)
我想要两个图,一个用于y1的散点图(仅用于标记),一个用于y2的线形图(仅用于线,没有标记)。
我有这个工作...
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))
但是,我想更改y1中标记的颜色和符号。当我这样做时,它会将标记添加到y2中,这是我不想要的。我该如何解决?
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))
答案 0 :(得分:3)
您可以先创建一个空的Plotly对象/仅使用x值的Plotly对象
plot_ly(data, x = ~x) %>%
,然后分别添加每个跟踪。
symbol
中的 add_trace
用于指定符号source,而symbols
存储实际符号。在您的情况下,您需要在symbol
中传递marker
类型,即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))