0

I need to display the differences in area using polarchart in R plotply. It seems that my code works fine, However I need to switch the traces, so that the "M0" layer (trace) will be the one on top and "M2" trace on the bottom. Could anyone help me with this, considering that I have tried multiple options and none have worked so far. My R code:

x1 <- c(
  0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36,
  36, 34, 32, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2, 0
)
y1 <- c(
  126.1413, 122.5064, 120.5929, 119.5620, 116.2632, 113.2340, 111.8527,
  112.5675, 116.6128, 112.6579, 104.5264, 103.5137, 101.0783, 104.8868,
  107.8864, 109.0920, 95.3229, 101.3696, 112.2920, 114.7041, 107.6714,
  107.5115, 100.6170, 107.9576, 108.9578, 113.2368, 114.9524, 119.4507,
  122.6751, 123.1861, 124.9997, 125.9652, 124.7454, 123.8936, 128.1220,
  129.7844, 129.7762, 117.2343
)
x2 <- c(
  0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36,
  36, 34, 32, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2, 0
)
y2 <- c(
  125.52385, 119.07241, 116.24269, 116.22503, 113.72144, 113.23399,
  108.94401, 108.83963, 109.37008, 107.46098, 104.52637, 102.99718,
  96.47646, 93.31721, 92.08402, 96.04877, 95.32290, 101.36958, 112.29199,
  114.70411, 107.67138, 107.51151, 108.39729, 107.95755, 108.95785,
  113.23678, 117.89176, 124.14630, 126.53308, 132.59236, 131.69146,
  132.94295, 131.67062, 128.36539, 129.79911, 130.99317, 131.97529, 125.52385
)
x3 <- c(
  0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 30, 28, 26,
  24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2, 0
)
y3 <- c(
  126.14132, 122.50643, 120.59295, 119.56199, 116.26324, 112.29014,
  111.85268, 112.56755, 116.61283, 112.65789, 104.34224, 96.50043,
  101.07827, 104.88684, 103.69290, 109.09203, 111.50719, 116.95312,
  115.86528, 119.68691, 114.95241, 119.45073, 124.83299, 131.63141,
  130.78950, 125.96519, 124.74543, 123.89356, 128.12202, 129.78436,
  129.77622, 126.14132
)

p <- plot_ly(
  type = "scatterpolar",
  mode = "lines"
) %>%
  add_trace(
    name = "M0",
    r = as.numeric(y1),
    theta = as.numeric(x1),
    thetaunit = "degrees",
    fill = "toself",
    line = list(
      color = "#1B98E0FF"
    ),
    fillcolor = "#1B98E0FF"
  ) %>%
  add_trace(
    name = "M1",
    r = as.numeric(y2),
    theta = as.numeric(x2),
    thetaunit = "degrees",
    fill = "toself",
    line = list(
      color = "#1B98E0BF"
    ),
    fillcolor = "#1B98E0BF"
  ) %>%
  add_trace(
    name = "M2",
    r = as.numeric(y3),
    theta = as.numeric(x3),
    thetaunit = "degrees",
    fill = "toself",
    line = list(
      color = "#1B98E08C"
    ),
    fillcolor = "1B98E08C"
  ) %>%
  layout(polar = list(
    domain = list(
      x = c(1, 1),
      y = c(1, 1)
    ),
    sector = c(0, 100),
    angularaxis = list(
      direction = "clockwise",
      thetaunit = "degrees",
      dtick = 10,
      rotation = 90
    ),
    radialaxis = list(
      range = c(0, 140), title = "µm"
    )
  ))

I tried to change the order of the traces as well by adding the "M2" trace initially and it did not work.

stefan
  • 90,330
  • 6
  • 25
  • 51
shaii
  • 3
  • 2
  • Can you make your example [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? – jrcalabrese Feb 15 '23 at 18:34
  • Instead of commenting, can you edit/update your post so your original question is reproducible for other StackOverflow users? – jrcalabrese Feb 15 '23 at 19:12
  • I tried to add some example vectors that could be used to generate the plots by editing my post, however, it wouldn't be pasted on the "code section". Sorry for the inconvenience Im a new user of the stackoverflow. – shaii Feb 15 '23 at 19:20
  • Can you elaborate more about what you mean when you say that you want the switch the position of the traces? Have you tried switching the order of the `add_trace` arguments themselves? I'm looking at your code and moving around the order of the `add_trace` arguments adjusts the layers of the different colored objects and the order of the legend. – jrcalabrese Feb 15 '23 at 19:41
  • I have tried to reverse the order of the legend, however that does not produce any changes on the position of the graphical layers. By switching the "add_trace" order do you mean adding the M3 trace first and then the M1 trace last? If so, I tried that and it wouldn't work. What I exactly want to achieve is position the M1 layer (which is also the smallest area) on top of the other layers in the polarchart. And then as the layers' area expands I want to position them underneath the M1. – shaii Feb 15 '23 at 19:46

1 Answers1

0

I have included a reproducible example below and have edited the colors and the transparency so it's easier to see each layer. The first plot is a copy of OP's original plot and the second plot has M1 as the top most layer. In order to make M1 the top most layer, I had to move it to the very bottom. It also looks like the legend follows this pattern; so even though the legend shows M1 on bottom in the second plot, you can tell that M1 (in red) is on top because the M1 label appears whenever you hover over a section that contains M1 (red) and M2 (blue) traces.

library(plotly)

x1 <- c(0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 36, 34, 32, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2, 0) 
y1 <- c(126.1413,122.5064,120.5929,119.5620,116.2632,113.2340,111.8527,112.5675,116.6128,112.6579,104.5264,103.5137,101.0783,104.8868,107.8864,109.0920,95.3229,101.3696,112.2920,114.7041,107.6714,107.5115,100.6170,107.9576,108.9578,113.2368,114.9524,119.4507,122.6751,123.1861,124.9997,125.9652,124.7454,123.8936,128.1220,129.7844,129.7762,117.2343)
x2 <- c(0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,36,34,32,30,28,26,24,22,20,18,16,14,12,10,8,6,4,2,0)
x3 <- c(0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,30,28,26,24,22,20,18,16,14,12,10,8,6,4,2,0) 
y2 <- c(125.52385,119.07241,116.24269,116.22503,113.72144,113.23399,108.94401,108.83963,109.37008,107.46098,104.52637,102.99718,96.47646,93.31721,92.08402,96.04877,95.32290,101.36958,112.29199,114.70411,107.67138,107.51151,108.39729,107.95755,108.95785,113.23678,117.89176,124.14630,126.53308,132.59236,131.69146,132.94295,131.67062,128.36539,129.79911,130.99317,131.97529,125.52385) 
y3 <- c(126.14132,122.50643,120.59295,119.56199,116.26324,112.29014,111.85268,112.56755,116.61283,112.65789,104.34224,96.50043,101.07827,104.88684,103.69290,109.09203,111.50719,116.95312,115.86528,119.68691,114.95241,119.45073,124.83299,131.63141,130.78950,125.96519,124.74543,123.89356,128.12202,129.78436,129.77622,126.14132)

# original plot
plot_ly(type = 'scatterpolar', mode = 'lines') %>%
  add_trace(name = "M0", r = as.numeric(y1), theta = as.numeric(x1), 
            thetaunit = "degrees", fill = 'toself', 
            line = list(color = 'rgba(255,255,0,0.25)'),
            fillcolor = 'rgba(255,255,0,0.25)') %>%
  add_trace(name = "M1", r = as.numeric(y2), theta = as.numeric(x2),
            thetaunit = "degrees", fill = 'toself', 
            line = list(color = 'rgba(255,87,55,0.75)'),
            fillcolor = 'rgba(255,87,55,0.25)') %>%
  add_trace(name = "M2", r = as.numeric(y3), theta = as.numeric(x3),
            thetaunit = "degrees", fill = 'toself', 
            line = list(color = 'rgba(0,0,255,0.25)'),
            fillcolor = 'rgba(0,0,255,0.25)') %>%
  layout(polar = list(domain = list(x = c(1,1), y = c(1,1)), sector = c(0,100),
    angularaxis = list(direction = 'clockwise', thetaunit = 'degrees', dtick = 10, rotation = 90),
    radialaxis = list( range = c(0,140), title = "µm")))

# where m1 is on top
plot_ly(type = 'scatterpolar', mode = 'lines') %>%
  add_trace(name = "M0", r = as.numeric(y1), theta = as.numeric(x1), 
            thetaunit = "degrees", fill = 'toself', 
            line = list(color = 'rgba(255,255,0,0.25)'),
            fillcolor = 'rgba(255,255,0,0.25)') %>%
  add_trace(name = "M2", r = as.numeric(y3), theta = as.numeric(x3),
            thetaunit = "degrees", fill = 'toself', 
            line = list(color = 'rgba(0,0,255,0.25)'),
            fillcolor = 'rgba(0,0,255,0.25)') %>%
  add_trace(name = "M1", r = as.numeric(y2), theta = as.numeric(x2),
            thetaunit = "degrees", fill = 'toself', 
            line = list(color = 'rgba(255,87,55,0.75)'),
            fillcolor = 'rgba(255,87,55,0.25)') %>%
  layout(polar = list(domain = list(x = c(1,1), y = c(1,1)), sector = c(0,100),
                      angularaxis = list(direction = 'clockwise', thetaunit = 'degrees', dtick = 10, rotation = 90),
                      radialaxis = list( range = c(0,140), title = "µm")))
jrcalabrese
  • 2,184
  • 3
  • 10
  • 30