R plotly : How to connect lines on polar/radar chart?

标签 r charts plotly radar-chart

我正在用 plotly 在 R 中创建一个极坐标图,但我不希望线条之间的值用颜色填充。我找到了 line_close python 库的属性,但我在 R 中找不到等效项。

图表代码:

library(plotly)

p <- plot_ly(
  type = 'scatterpolar',
  mode = 'lines',
) %>%
  add_trace(
    mode = 'lines',
    r = c(3, 0, 1),
    theta = c('A','B','C'),
    name = '1'
  ) %>%
  add_trace(
    mode = 'lines',
    r = c(1, 2, 3),
    theta = c('A','B','C'),
    name = '2'
  ) %>%
  layout(
    polar = list(
      radialaxis = list(
        angle = 90,
        visible = T,
        range = c(0,3),
        showline = F,
        color = '#bfbfbf',
        nticks = 4,
        tickangle = 90
      )
    )
  )

p

图表图像:

enter image description here

最佳答案

我已经仔细浏览了 plotly::schema ,并且似乎没有选项可以内置于 plotly 的 R 端口中。 .

然而 , 定义自己的 add_closed_trace 很简单像这样的功能:

add_closed_trace <- function(p, r, theta, ...) 
{
  plotly::add_trace(p, r = c(r, r[1]), theta = c(theta, theta[1]), ...)
}

您可以将其用作 add_trace 的插件。 , 像这样:

library(plotly)

p <- plot_ly(
  type = 'scatterpolar',
  mode = 'lines',
) %>%
  add_closed_trace(
    mode = 'lines',
    r = c(3, 0, 1),
    theta = c('A','B','C'),
    name = '1'
  ) %>%
  add_closed_trace(
    mode = 'lines',
    r = c(1, 2, 3),
    theta = c('A','B','C'),
    name = '2'
  ) %>%
  layout(
    polar = list(
      radialaxis = list(
        angle = 90,
        visible = T,
        range = c(0,3),
        showline = F,
        color = '#bfbfbf',
        nticks = 4,
        tickangle = 90
      )
    )
  )

p

enter image description here

关于R plotly : How to connect lines on polar/radar chart?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60018891/

相关文章:

python - 悬停时仅显示单行,隐藏所有其他行

r - colnames 命令产生错误 : attribute [13] must be the same length as the vector [12]

r - 尝试使用 ldply 将列表转换为数据帧时出错((function (..., row.names = NULL, :arguments imply differing number of rows: )

javascript - 加载 CSV 的连续轴 Google 图表?

excel - 如何在同一张图表上绘制两个散点图?

java - Android MPAndroid图表

python - Plotly:如何自定义 3D 散点图的符号?

python - 阴谋失踪的逆戟鲸

删除 R 中数据框中所有列名的前两个字符

r - R 中变量的单独值的单独表/矩阵