R图:如何连接极坐标/雷达图上的线?

时间:2020-02-01 16:32:31

标签: r charts plotly radar-chart

我正在用R绘制在极坐标图中的极坐标图,但是我不希望线间的值被颜色填充。我找到了python库的line_close属性,但是找不到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

1 个答案:

答案 0 :(得分:2)

我对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