0

I'm trying to use plotly to draw facet maps of radar charts that have multiple traces. Here's a test data set. I'd like to have three radar charts in facets, one for each of the three scenarios. Within each radar chart I want three traces of food group codes and values, one for each of the three years.

test <- data.frame(scenario = c("SSP1_NoCC", "SSP1_NoCC", "SSP1_NoCC", "SSP1_NoCC", "SSP1_NoCC", "SSP1_NoCC", "SSP1_NoCC", "SSP1_NoCC", "SSP1_NoCC", "SSP1_NoCC", "SSP1_NoCC", "SSP1_NoCC", "SSP2_HGEM", "SSP2_HGEM", "SSP2_HGEM", "SSP2_HGEM", "SSP2_HGEM", "SSP2_HGEM", "SSP2_HGEM", "SSP2_HGEM", "SSP2_HGEM", "SSP2_HGEM", "SSP2_HGEM", "SSP2_HGEM"), 
                   year = c("X2010", "X2010", "X2010", "X2010", "X2030", "X2030", "X2030", "X2030", "X2050", "X2050", "X2050", "X2050", "X2010", "X2010", "X2010", "X2010", "X2030", "X2030", "X2030", "X2030", "X2050", "X2050", "X2050", "X2050"), 
                   foodGroupCode = c("cereals", "fish", "meats", "pulses", "cereals", "fish", "meats", "pulses", "cereals", "fish", "meats", "pulses", "cereals", "fish", "meats", "pulses", "cereals", "fish", "meats", "pulses", "cereals", "fish", "meats", "pulses"), 
                   value = c("445.0998", "0.2531572", "24.45475", "3.29157", "460.5723", "0.4612104", "35.059", "3.812299", "486.3048", "1.349964", "68.9127", "5.044619", "444.7544", "0.2531572", "24.45783", "3.284128", "441.8014", "0.4013985", "31.88695", "3.641812", "429.3817", "0.793566", "48.87213", "4.323462"))

One solution is to create the three radar plots separately and then use subplot as described in this QA. A second approach is to use group_map, as described in the last answer to this question. I tried this latter approach with the code below, which returns this error message.

Error in add_data(p, data) : argument "p" is missing, with no default

I haven't used dplyr much so I can't figure out where the argument p is missing.

 library(plotly)
    library(dplyr)
    maxVal <- max(test$value)
    test %>%
  group_by("scenario") %>% 
  group_map(~ plot_ly(data = ., type = 'scatterpolar', fill = 'toself', mode = "markers") %>%
              add_trace(data = ., type="scatterpolar", 
                        r = subset(test, year == "X2010", select = value),
                        theta = test$foodGroupCode,
                        fillcolor = '#B6FFB4',
                        name = "2010"
              ) %>%
              add_trace(data = ., type="scatterpolar", 
                        r = subset(test, year == "X2030", select = value),
                        theta = test$foodGroupCode,
                        fillcolor = '#B6FFB4',
                        name = "2030"
              ) %>%
              add_trace(data = ., type="scatterpolar", 
                        r = subset(test, year == "X2050", select = value),
                        theta = test$foodGroupCode,
                        fillcolor = '#B6FFB4',
                        name = "2050"
              ) %>%
              layout(
                polar = list(
                  radialaxis = list(
                    visible = T,
                    range = c(0,maxVal)
                  )
                )
              ) 
            %>%
              subplot(nrows = 2)
  )
JerryN
  • 2,356
  • 1
  • 15
  • 49
  • doesn't plot_ly requires x and y axis values? – monte Jan 03 '21 at 16:15
  • Not for radar plots - see https://plotly.com/r/radar-chart/. – JerryN Jan 03 '21 at 16:19
  • 1
    You code has `theta = test$foodGroupCode,` with a comma at the end, but no subsequent argument (such as `name = "2050"`)...that might explain your error? – Ben Jan 03 '21 at 17:33
  • I revised the code to fix this error. I have updated the Q to reflect this and also the new error which I have also included – JerryN Jan 03 '21 at 20:26

0 Answers0