简体   繁体   English

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

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

I am creating a polar chart in R with plotly, but I don't want the values between lines to be filled with color.我正在用 plotly 在 R 中创建一个极坐标图,但我不希望线条之间的值用颜色填充。 I found the line_close property for python library, but I can't find the equivalent in R.我找到了 python 库的line_close属性,但在 R 中找不到等效项。

Chart code:图表代码:

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

Chart image:图表图像:

在此处输入图片说明

I've had a good look through plotly::schema , and there doesn't seem to be an option to do this built in to the R port of plotly .我已经很好地浏览了plotly::schema ,似乎没有一个选项可以在plotly的 R 端口中执行此plotly

However , it is trivial to define your own add_closed_trace function like this:但是,像这样定义自己的add_closed_trace函数很简单:

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

You can use this as a drop-in for add_trace , like this:您可以将其用作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

在此处输入图片说明

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

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