颜色映射在R中使用Plotly不起作用。

huangapple go评论65阅读模式
英文:

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 &lt;- data.frame(&quot;time&quot; = 1:10, A1 = rnorm(10), B1 = rnorm(10), C1 = rnorm(10), D1 = rnorm(10))
df2 &lt;- data.frame(&quot;time&quot; = 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 &lt;- c(             ## add the standard plotly colors
  &#39;red&#39;,  
  &#39;blue&#39;,  
  &#39;green&#39;,  
  &#39;yellow&#39;
) 

Here is the plotly code I tried

longDF1 &lt;- melt(df1, id.vars = &quot;time&quot;)
longDF2 &lt;- melt(df2, id.vars = &quot;time&quot;)
  
  plot_ly() %&gt;%
  add_trace(data = longDF1, x = ~time, y = ~value, type = &#39;scatter&#39;, mode = &#39;lines+markers&#39;, color = ~variable,
            line = list(color = my_colors, dash = &#39;solid&#39;),
            marker = list(color = my_colors, symbol = &#39;circle&#39;)) %&gt;% 
  add_trace(data = longDF2, x = ~time, y = ~value, type = &#39;scatter&#39;, mode = &#39;lines+markers&#39;, color = ~variable,
            line = list(color = my_colors, dash = &#39;dot&#39;),
            marker = list(color = my_colors, symbol = &#39;circle-open&#39;))

The colors dont seem to be added to the plots. Instead, plotly generates its own colors. Any ideas how to solve this?

答案1

得分: 1

要获得所需的颜色,请从 linemarker 规范中删除 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")
  )

颜色映射在R中使用Plotly不起作用。

英文:

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)) %&gt;%
  add_trace(
    data = longDF1, x = ~time, y = ~value, type = &quot;scatter&quot;, mode = &quot;lines+markers&quot;, color = ~variable,
    line = list(dash = &quot;solid&quot;),
    marker = list(symbol = &quot;circle&quot;)
  ) %&gt;%
  add_trace(
    data = longDF2, x = ~time, y = ~value, type = &quot;scatter&quot;, mode = &quot;lines+markers&quot;, color = ~variable,
    line = list(dash = &quot;dot&quot;),
    marker = list(symbol = &quot;circle-open&quot;)
  )

颜色映射在R中使用Plotly不起作用。<!-- -->

huangapple
  • 本文由 发表于 2023年6月6日 08:14:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76410695.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定