【问题标题】:How to plot multiple lines in radar chart using split in plotly如何使用 split in plotly 在雷达图中绘制多条线
【发布时间】:2020-09-29 23:32:12
【问题描述】:

我尝试过使用带有 scatterpolar 的分割轨迹,它似乎部分有效,但无法绘制所有 10 个变量的值。所以我希望使用从 X1 到 X10 的值将每一行(由“ean”标识)绘制为自己的线。

library(tidyverse)
library(vroom)
library(plotly)

types <- rep(times = 10, list(
  col_integer(f = stats::runif,
              min = 1,
              max = 5)))

products = bind_cols(
  tibble(ean = sample.int(1e9, 25)),
  tibble(kategori = sample(c("kat1", "kat2", "kat3"), 25, replace = TRUE)),
  gen_tbl(25, 10, col_types = types)
  )

plot_ly(
    products,
    type = 'scatterpolar',
    mode = "lines+markers",
    r = ~X1,
    theta = ~"X1",
    split = ~ean
  )

如何在雷达图 (X1-X10) 中绘制所有变量?通常我会选择带有 X1:X10 的列,但我不能在这里这样做(我认为这与 ~ 用于在这里选择变量有关)。

所以我希望结果看起来像这样(但我只显示线条而不是填充多边形,我会有更多产品)。所以最后 25 个产品很多,但我将其连接起来以便用户可以选择它想要显示的图表。

【问题讨论】:

  • 请提供一个可重现的例子。 object 'types' not found
  • 感谢您让我知道它丢失了。现在添加了类型变量。
  • 太好了,感谢您的更新。如果我的回答符合您的期望,请告诉我。干杯

标签: r plotly


【解决方案1】:

在 plotly 中,使用长格式数据很方便 - 请参阅 ?gather

请检查以下内容:

library(dplyr)
library(tidyr)
library(vroom)
library(plotly)

types <- rep(times = 10, list(
  col_integer(f = stats::runif,
              min = 1,
              max = 5)))

products = bind_cols(
  tibble(ean = sample.int(1e9, 25)),
  tibble(kategori = sample(c("kat1", "kat2", "kat3"), 25, replace = TRUE)),
  gen_tbl(25, 10, col_types = types)
)

products_long <- gather(products, "key", "value", -ean, -kategori)

plot_ly(
  products_long,
  type = 'scatterpolar',
  mode = "lines+markers",
  r = ~value,
  theta = ~key,
  split = ~ean
)

【讨论】:

    猜你喜欢
    • 2020-05-25
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-11
    • 2019-05-19
    • 1970-01-01
    相关资源
    最近更新 更多