Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the color of a specific ring in a radar chart created in plotly

Tags:

r

plotly

I have a radar chart created w/ plotly as shown below. Is there a way to have only the ring at 100 colored red, while all other rings remain in their off-gray color?

enter image description here

library(plotly)

fig <- plot_ly(
    type = 'scatterpolar',
    r = c(135, 75, 110),
    theta = c('A','B','C'),
    fill = 'toself'
  ) 
fig %>%
  layout(
    polar = list(
      radialaxis = list(
        visible = T,
        range = c(0, 150)
      )
    )
  )
like image 285
DJC Avatar asked Jan 19 '26 21:01

DJC


1 Answers

To my knowledge, there is not an option to use custom formatting on any axis line other than zero, so there may not actually be a "canonical" answer here that you're hoping for.

That being said, adding another trace to your plot might be the best work-around, even if it's a little clunky. Because the added line trace overrides the default categorical axis, some more extensive customization of the angular axis and some math to manually calculate categorical values relative to 360 degree coordinate system (this is normally obscured from the user) is required.

Names <- c('A','B','C')
Values <- c(135, 75, 110)
ThetaValues <- seq(from = 0, to = 360, length.out = length(Values)+1L)[seq_len(length(Values - 1L))]

plot_ly() %>% 
  add_trace(type = 'scatterpolar',mode = "lines",
            r = rep(100,100),
            theta = seq(from = 0, to = 360, length.out = 100),
            line = list(smoothing = 1,
                        color = "red",
                        shape = "spline"),
            hoverinfo = "skip",
            showlegend = FALSE) %>%
  add_trace(type = 'scatterpolar',mode = "markers",
            r = Values,
            name = "",
            theta = ThetaValues,
            color = I("blue"),
            fill = 'toself') %>% 
  layout(polar = list(radialaxis = list(visible = T,
                                        range = c(0, 150)),
                      angularaxis = list(visible = T,
                                         type = "category",
                                         ticktext = Names,
                                         tickvals = ThetaValues)))

Polar Chart

like image 102
Matt Summersgill Avatar answered Jan 21 '26 12:01

Matt Summersgill