简体   繁体   English

R 中的 Plotly:3D 折线图 - 防止一条连续线

[英]Plotly in R: 3D Line Graphs - Preventing One Continuous Line

I'm new to using Plotly in R.我是在 R 中使用 Plotly 的新手。

I am trying to construct a 3d line graph.我正在尝试构建一个 3d 折线图。

I am trying to achieve a graph that looks like this:我正在尝试实现如下所示的图表:

在此处输入图像描述

The above is my objective and I can't seem to make it work.以上是我的目标,我似乎无法实现。

When I impliment what I think the right code is, I get one continuous line (so the last value of the first series line above is connected to the first value of the second series, etc).当我暗示我认为正确的代码是什么时,我得到一条连续的线(所以上面第一个系列的最后一个值连接到第二个系列的第一个值,等等)。

Here is my result (you are viewing from an angle that shows the problem of the "looping"):这是我的结果(您从显示“循环”问题的角度查看):

在此处输入图像描述

My code is here:我的代码在这里:

fig <- plot_ly(df_cohort_master, y = ~ord_month, x = ~cohort, z = ~conversions, 
                  type = 'scatter3d', mode = 'lines',color=~conversions) %>%
                  layout(
                    scene= list(
                      xaxis = list(autorange = "reversed"),
                      yaxis = list(autorange = "reversed")))

suppressWarnings(print(fig))

And here is what my data looks like:这是我的数据的样子:

在此处输入图像描述

What am I doing wrong?我究竟做错了什么?

Thanks in advance.提前致谢。

Perhaps you could try using split to separate into multiple traces based on your cohort .也许您可以尝试使用split根据您的cohort分成多个跟踪。

Here's an example with made up data based on your post.这是一个根据您的帖子组成的数据的示例。

library(plotly)

set.seed(123)

df_cohort_master <- data.frame(
  cohort = rep(1:4, times = 5),
  ord_month = rep(1:4, each = 5),
  conversions = sample(20, 20)
)

plot_ly(df_cohort_master,
        x = ~cohort, 
        y = ~ord_month, 
        z = ~conversions, 
        type = 'scatter3d', 
        mode = 'lines',
        split = ~cohort) %>%
  layout(
    title = "3D Scatter plot", 
    scene = list(
      camera = list(
        eye = list(x = 1, y = 2, z = 2)
      )
    )
  )

Plot Plot

具有多条轨迹的 3d 绘图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM