You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

R语言Plotly雷达图图例异常:如何隐藏RGB色值仅显示分组名?

Fix: Remove RGB Values from Plotly Radar Chart Legend in R

I see the issue here—your legend is showing both the team name and RGB color indicators (from the marker symbols) because Plotly defaults to displaying both the filled area and marker in each legend entry. Let's tweak this so only the team names appear alongside their corresponding fill colors.

Root Cause

When using scatterpolar with fill='toself' and setting a distinct marker color from the fillcolor, Plotly automatically includes both the filled square and marker circle in each legend item. This can create the appearance that RGB values are attached to the team name, even though it's just the marker symbol being displayed.

Solution 1: Align Marker Color with Fill Color

If you want to keep the markers on the radar chart but clean up the legend, set the marker color to match the fill color (use the same base RGB values, just remove transparency for markers). Here's the adjusted code:

attrib1 <- c(2, 3, 5)
attrib2 <- c(1, 2, 5)
attrib3 <- c(5, 2, 4)
attrib4 <- c(4, 3, 1)
attrib5 <- c(2, 2, 1)
teams_final <- data.frame(attrib1, attrib2, attrib3, attrib4, attrib5, attrib1)
row.names(teams_final) <- c("A Team", "B Team", "C Team")
labels <- c("attrib1", "attrib2", "attrib3", "attrib4", "attrib5", "attrib1")

library(plotly)
p <- plot_ly(type = 'scatterpolar', fill = 'toself') %>%
  add_trace(
    r = unname(teams_final[1, ]), 
    theta = labels, 
    name = "A Team", 
    fillcolor = 'rgba(64, 64, 64, 0.5)', 
    marker = list(color = 'rgba(64, 64, 64, 1)'),  # Match fill color (no transparency)
    hoverinfo = "text", 
    text = ~paste(labels, '<br> Importance: ', unname(teams_final[1, ]))
  ) %>%
  add_trace(
    r = unname(teams_final[2, ]), 
    theta = labels, 
    name = "B Team", 
    fillcolor = 'rgba(171, 35, 35, 0.5)', 
    marker = list(color = 'rgba(171, 35, 35, 1)'),  # Match fill color
    hoverinfo = "text", 
    text = ~paste(labels, '<br> Importance: ', unname(teams_final[2, ]))  # Fixed: use row 2 instead of 1
  ) %>%
  add_trace(
    r = unname(teams_final[3, ]), 
    theta = labels, 
    name = "C Team", 
    fillcolor = 'rgba(241, 103, 69, 0.5)', 
    marker = list(color = 'rgba(241, 103, 69, 1)'),  # Match fill color
    hoverinfo = "text", 
    text = ~paste(labels, '<br> Importance: ', unname(teams_final[3, ]))  # Fixed: use row 3 instead of 1
  ) %>%
  layout(
    polar = list(radialaxis = list(visible = TRUE, range = c(0,6))), 
    font = list(size = 11, color = "#202020"),
    legend = list(
      traceorder = "normal",
      itemclick = "toggleitem",
      title = list(text = "Teams")  # Optional: add a clear legend title
    )
  )
p

Solution 2: Remove Markers Entirely

If you don't need the individual markers on the radar chart, set mode='lines' to hide them. This will make the legend only display the filled square and team name:

# Same data setup as above
p <- plot_ly(type = 'scatterpolar', fill = 'toself', mode='lines') %>%  # Add mode='lines' to hide markers
  add_trace(
    r = unname(teams_final[1, ]), 
    theta = labels, 
    name = "A Team", 
    fillcolor = 'rgba(64, 64, 64, 0.5)', 
    marker = list(color = '#404040'),
    hoverinfo = "text", 
    text = ~paste(labels, '<br> Importance: ', unname(teams_final[1, ]))
  ) %>%
  add_trace(
    r = unname(teams_final[2, ]), 
    theta = labels, 
    name = "B Team", 
    fillcolor = 'rgba(171, 35, 35, 0.5)', 
    marker = list(color = '#AB2323'),
    hoverinfo = "text", 
    text = ~paste(labels, '<br> Importance: ', unname(teams_final[2, ]))
  ) %>%
  add_trace(
    r = unname(teams_final[3, ]), 
    theta = labels, 
    name = "C Team", 
    fillcolor = 'rgba(241, 103, 69, 0.5)', 
    marker = list(color = '#F16745'),
    hoverinfo = "text", 
    text = ~paste(labels, '<br> Importance: ', unname(teams_final[3, ]))
  ) %>%
  layout(
    polar = list(radialaxis = list(visible = TRUE, range = c(0,6))), 
    font = list(size = 11, color = "#202020")
  )
p

Bonus Fix

I also spotted a small bug in your original code: the text parameter for the second and third traces was incorrectly using teams_final[1, ] instead of their respective rows. I fixed that in the adjusted code so the hover text shows the correct values for each team.

内容的提问来源于stack exchange,提问作者brunosm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:03:20