在 >4.x Plotly 中排列线图的路径顺序

mtc*_*lot 5 plot r linechart plotly r-plotly

我需要绘制一条路径,该路径并不严格从左到右,而是在 y 轴上交叉,但是自从我升级到plotly 4.7以来,我不能不这样做。fx 没问题。3.6

有谁知道如何以情节方式告诉路径如何排序?

library(dplyr)
library(plotly) # > 4.x

data.frame(x = c(1:5,5:1),y = c(1:10)) %>%
  arrange(y) %>%
  plot_ly(x = ~x,y = ~y) %>% add_lines()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

如果你查看 data.frame 它应该遵循红色路径:

data.frame(x = c(1:5,5:1),y = c(1:10)) %>% arrange(y)
   x  y
1  1  1
2  2  2
3  3  3
4  4  4
5  5  5
6  5  6
7  4  7
8  3  8
9  2  9
10 1 10
Run Code Online (Sandbox Code Playgroud)

M--*_*M-- 4

您可以在以下位置设置模式plotly

 data.frame(x = c(1:5,5:1),y = c(1:10)) %>%
    arrange(y) %>%
    plot_ly(x = ~x,y = ~y, mode = 'lines+markers') 
Run Code Online (Sandbox Code Playgroud)

图表将是:

在此输入图像描述

或者您可以使用以下 base-R 解决方案:

df <- data.frame(x = c(1:5,5:1),y = c(1:10))
      with(df, plot(x,y))
      with(df, lines(x,y))
Run Code Online (Sandbox Code Playgroud)

这将为您提供以下情节:在此输入图像描述

  • 非常感谢@Masoud,我认为`data.frame(x = c(1:5,5:1),y = c(1:10)) %&gt;%range(y)%&gt;%plot_ly(x = ~x,y = ~y, mode = 'lines')` 和 `data.frame(x = c(1:5,5:1),y = c(1:10)) %&gt;%排列(y) %&gt;%plot_ly(x = ~x,y = ~y) %&gt;% add_lines()` 会做同样的事情。显然不是。 (2认同)