英文:
Color mapping not working with plotly in R
问题
I want to plot df1 with "time" on the x-axis and all other variables on the y-axis. Similarly, I want to plot df2 with "time" on the x-axis and all other variables on the y-axis. In this case, I want the lines to be "dot" and markers to be hollow circles. I specified the following colors to be used:
my_colors <- c(
'red',
'blue',
'green',
'yellow'
)
Here is the plotly code I tried:
longDF1 <- melt(df1, id.vars = "time")
longDF2 <- melt(df2, id.vars = "time")
plot_ly() %>%
add_trace(data = longDF1, x = ~time, y = ~value, type = 'scatter', mode = 'lines+markers', color = ~variable,
line = list(color = my_colors, dash = 'solid'),
marker = list(color = my_colors, symbol = 'circle')) %>%
add_trace(data = longDF2, x = ~time, y = ~value, type = 'scatter', mode = 'lines+markers', color = ~variable,
line = list(color = my_colors, dash = 'dot'),
marker = list(color = my_colors, symbol = 'circle-open'))
问题中提到的颜色似乎没有正确应用到图表中。有没有解决这个问题的方法?
英文:
I have a data frame like this
df1 <- data.frame("time" = 1:10, A1 = rnorm(10), B1 = rnorm(10), C1 = rnorm(10), D1 = rnorm(10))
df2 <- data.frame("time" = 1:5, A2 = rnorm(5), B2 = rnorm(5), C2 = rnorm(5), D2 = rnorm(5))
I want to plot df1 with "time" on x axis and all other variables on the y axis. Similarly, I want to plot df2 with "time" on x axis and all other variables on the y axis. In this case,
I want the lines to be "dot" and markers to be hollow circles.
I specified the following colors to be used
my_colors <- c( ## add the standard plotly colors
'red',
'blue',
'green',
'yellow'
)
Here is the plotly code I tried
longDF1 <- melt(df1, id.vars = "time")
longDF2 <- melt(df2, id.vars = "time")
plot_ly() %>%
add_trace(data = longDF1, x = ~time, y = ~value, type = 'scatter', mode = 'lines+markers', color = ~variable,
line = list(color = my_colors, dash = 'solid'),
marker = list(color = my_colors, symbol = 'circle')) %>%
add_trace(data = longDF2, x = ~time, y = ~value, type = 'scatter', mode = 'lines+markers', color = ~variable,
line = list(color = my_colors, dash = 'dot'),
marker = list(color = my_colors, symbol = 'circle-open'))
The colors dont seem to be added to the plots. Instead, plotly generates its own colors. Any ideas how to solve this?
答案1
得分: 1
要获得所需的颜色,请从 line
和 marker
规范中删除 color
属性,而是将颜色向量传递给 colors
属性,可以仅传递给第一个跟踪,或者直接在 plot_ly()
中传递,如下所示。此外,由于您有八个不同的类别,请复制您的颜色向量,即使用 colors = c(my_colors, my_colors)
。
library(plotly)
set.seed(123)
plot_ly(colors = c(my_colors, my_colors)) %>%
add_trace(
data = longDF1, x = ~time, y = ~value, type = "scatter", mode = "lines+markers", color = ~variable,
line = list(dash = "solid"),
marker = list(symbol = "circle")
) %>%
add_trace(
data = longDF2, x = ~time, y = ~value, type = "scatter", mode = "lines+markers", color = ~variable,
line = list(dash = "dot"),
marker = list(symbol = "circle-open")
)
英文:
To get your desired colors drop the color
attribute from the line
and marker
specs and instead pass your color vector to the colors
attribute, where it is sufficient to pass it to the first trace only or directly inside plot_ly()
as I do below. Additionally, as you you have eight different categories duplicate your color vector, i.e. use colors = c(my_colors, my_colors)
.
library(plotly)
set.seed(123)
plot_ly(colors = c(my_colors, my_colors)) %>%
add_trace(
data = longDF1, x = ~time, y = ~value, type = "scatter", mode = "lines+markers", color = ~variable,
line = list(dash = "solid"),
marker = list(symbol = "circle")
) %>%
add_trace(
data = longDF2, x = ~time, y = ~value, type = "scatter", mode = "lines+markers", color = ~variable,
line = list(dash = "dot"),
marker = list(symbol = "circle-open")
)
<!-- -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论