R 按值范围绘制线条颜色

发布于 2025-01-16 09:15:17 字数 1816 浏览 5 评论 0原文

我想制作这种图表(来自 我们的数据世界),其中线条颜色随值范围而变化。

编辑:添加屏幕截图以使其更清晰:

在此处输入图像描述

通过plotly,我发现了这个示例 但使用 type = scatter 和 mode = 标记图而不是线条:

    x <- seq(from = -2,
         to = 2,
         b = 0.1)
y <- sin(x)

p11 <- plot_ly() %>% 
  add_trace(type = "scatter",
            x = ~x,
            y = ~y,
            mode = "markers",
            marker = list(size = 10,
                          color = colorRampPalette(brewer.pal(10,"Spectral"))(41))) %>% 
  layout(title = "Multicolored sine curve",
         xaxis = list(title = "x-axis"),
         yaxis = list(title = "y-axis"))
p11

是否有任何方法可以使用 colorRampPalette 或值范围,但使用线条(实际上这是一个时间序列)

    x <- seq(from = -2,
         to = 2,
         b = 0.1)
y <- sin(x)

p11 <- plot_ly() %>% 
  add_trace(type = "scatter",
            x = ~x,
            y = ~y,
            mode = "lines",
            line = list(width = 1,
                          color = colorRampPalette(brewer.pal(10,"Spectral"))(41))) %>% 
  layout(title = "Multicolored sine curve",
         xaxis = list(title = "x-axis"),
         yaxis = list(title = "y-axis"))
p11

谢谢

I would like to make this kind of graph (here from Our World In data ) where the line color varies by value range.

edit : adding a screenshot to make it clearer :

enter image description here

With plotly, I found this example but working with type = scatter and mode = markers plot and not with lines:

    x <- seq(from = -2,
         to = 2,
         b = 0.1)
y <- sin(x)

p11 <- plot_ly() %>% 
  add_trace(type = "scatter",
            x = ~x,
            y = ~y,
            mode = "markers",
            marker = list(size = 10,
                          color = colorRampPalette(brewer.pal(10,"Spectral"))(41))) %>% 
  layout(title = "Multicolored sine curve",
         xaxis = list(title = "x-axis"),
         yaxis = list(title = "y-axis"))
p11

is there any ways to use the colorRampPalette or values range but with line (actually it's a time series)

    x <- seq(from = -2,
         to = 2,
         b = 0.1)
y <- sin(x)

p11 <- plot_ly() %>% 
  add_trace(type = "scatter",
            x = ~x,
            y = ~y,
            mode = "lines",
            line = list(width = 1,
                          color = colorRampPalette(brewer.pal(10,"Spectral"))(41))) %>% 
  layout(title = "Multicolored sine curve",
         xaxis = list(title = "x-axis"),
         yaxis = list(title = "y-axis"))
p11

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦幻的味道 2025-01-23 09:15:17

你可以,但是你拥有的点数越多,它看起来就越好。请注意,我将 x 中的 .1 更改为 .001。

library(plotly)
library(RColorBrewer)

x <- seq(from = -2,
         to = 2,
         b = 0.001)
y <- sin(x)

z = cut(x, breaks = 5, include.lowest = T)

p11 <- plot_ly() %>% 
  add_lines(x = ~x,
            y = ~y,
            color = ~z,
            colors = colorRampPalette(brewer.pal(10,"Spectral"))(length(x))) %>% 
  layout(title = "Multicolored sine curve",
         xaxis = list(title = "x-axis"),
         yaxis = list(title = "y-axis"))
p11

输入图片此处描述

如果我将 .001 改回 .1,那就有点难看了!你可以看到差距。

输入图片此处描述

You can, but the more points you have the better it will look. Note that I change the .1 in x, to .001.

library(plotly)
library(RColorBrewer)

x <- seq(from = -2,
         to = 2,
         b = 0.001)
y <- sin(x)

z = cut(x, breaks = 5, include.lowest = T)

p11 <- plot_ly() %>% 
  add_lines(x = ~x,
            y = ~y,
            color = ~z,
            colors = colorRampPalette(brewer.pal(10,"Spectral"))(length(x))) %>% 
  layout(title = "Multicolored sine curve",
         xaxis = list(title = "x-axis"),
         yaxis = list(title = "y-axis"))
p11

enter image description here

If I change that .001 back to .1, it's a bit ugly! You can see the gaps.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文