R plotly scatterpolar图表气泡丢失

R plotly scatterpolar chart bubble missing

我知道这一定是基本问题,但很难找到问题所在。 这是我简单的 可重现的 R 代码:

library(plotly)

risk_status <- data.frame(
  id <- c(1:6),
  r <- c(1, 2, 3, 4, 3, 2),
  theta <- c(30, 90, 180, 220, 270, 330),
  bubbleSize <- c(10, 20, 30, 40, 50, 60),
  name <- c("Rick","Dan","Michelle","Ryan","Gary", "Pedro"),
  status <- c("Low", "Medium", "Low", "Medium", "Low", "Low")
)

risk_status$status <- factor(risk_status$status, levels = c("Low", "Medium", "High"))
bubble_colors_vector <- c("red", "green", "blue")


p <- plot_ly( data = risk_register_data) %>%
  add_trace(
    type = 'scatterpolar',
    r = ~r,
    theta = ~theta,
    size = as.numeric(risk_status$status),
    sizes = c(100,500)#,

    #color = risk_status$status,
    #colors = bubble_colors_vector
  ) %>%
  add_trace(
    type = "scatterpolar",
    mode = "text",
    r = risk_status$r,
    theta = risk_status$theta,
    text = risk_status$id

  )
p

图表输出:

到目前为止,还不错。 但是现在可以说,如果我想在其中引入颜色,只需取消注释 colorcolors 参数即可。 我得到这张图表:

假设我使用 color = as.numeric(risk_status$status) 在那种情况下,我得到这样的图表

在这里,我不要这个比例和图例。

我知道,有一些问题,因为虽然 factoring 数据框的 status 列,但我提供了 "Low", "Medium", "High" 作为级别,而在列中只有 "Low", "Medium" 是当前的。 但是,这是正常情况,同样,没有颜色,气泡的大小也能正确表示,那么为什么在添加颜色的同时也缺少尺寸?

提前致谢!

最后, 我通过在 for loop.

中使用 add_trace 的解决方法解决了这个问题

代码如下:

library(plotly)

risk_status <- data.frame(
  id <- c(1:6),
  r <- c(1, 2, 3, 4, 3, 2),
  theta <- c(30, 90, 180, 220, 270, 330),
  bubbleSize <- c(10, 20, 30, 40, 50, 60),
  name <- c("Rick","Dan","Michelle","Ryan","Gary", "Pedro"),
  status <- c("Low", "Medium", "Low", "Medium", "Low", "Low")
)

risk_status$status <- factor(risk_status$status, levels = c("Low", "Medium", "High"))
bubble_colors_vector <- c("red", "green", "blue")


p <- plot_ly( data = risk_status) %>%

  add_trace(
    type = 'scatterpolar',
    r = risk_status$r[risk_status$status == 'Low'],
    theta = risk_status$theta[risk_status$status == 'Low'],
    #size = as.numeric(risk_status$status[risk_status$status == 'Low']),
    #sizes = c(100,500),

    #color = 'red', #risk_status$status[risk_status$status == 'Low'], #as.numeric(risk_status$status), #risk_status$status,
    #colors = bubble_colors_vector[risk_status$status == 'Low'],

    mode = 'markers',
    marker = list(symbol = 'circle', line = list(width = 1, color = 'black'), mode="scatterpolar", color = "red", size=20)
  ) %>%
  add_trace(
    type = 'scatterpolar',
    r = risk_status$r[risk_status$status == 'Medium'],
    theta = risk_status$theta[risk_status$status == 'Medium'],
    #size = as.numeric(risk_status$status[risk_status$status == 'Medium']),
    #sizes = c(100,500),

    #color = 'red', #risk_status$status[risk_status$status == 'Medium'], #as.numeric(risk_status$status), #risk_status$status,
    #colors = bubble_colors_vector[risk_status$status == 'Medium'],

    mode = 'markers',
    marker = list(symbol = 'circle', line = list(width = 1, color = 'black'), mode="scatterpolar", color = "yellow", size=20)
  ) %>%

  add_trace(
    type = 'scatterpolar',
    r = risk_status$r[risk_status$status == 'High'],
    theta = risk_status$theta[risk_status$status == 'High'],
    #size = as.numeric(risk_status$status[risk_status$status == 'High']),
    #sizes = c(100,500),

    #color = 'red', #risk_status$status[risk_status$status == 'High'], #as.numeric(risk_status$status), #risk_status$status,
    #colors = bubble_colors_vector[risk_status$status == 'High'],

    mode = 'markers',
    marker = list(symbol = 'circle', line = list(width = 1, color = 'black'), mode="scatterpolar", color = "green", size=20)
  ) %>%


  add_trace(
    type = "scatterpolar",
    mode = "text",
    r = risk_status$r,
    theta = risk_status$theta,
    text = risk_status$id

  )
p

输出图表: