Combining 3 scatterpolar Plots in Plotly R

Viewed 156

I'm trying to combine three different scatterpolars into a single plot using plotly in R, but it's not working. I ran the following code:

library(plotly)

m13 = runif(6, 0,1)
m15 = runif(6, 0,1)
m17 = runif(6, 0,1)

n =  c('Saneamento', 'Riqueza', 'Saúde', 'Longevidade', 'Educação', 'Crime')


plot_ly(type = 'scatterpolar',
        mode = 'lines') %>%
  add_trace(r = m13,
            theta = n,
            fill = "toself",
            mode = "markers",
            name='Índice 2013') %>%
  add_trace(r = m15,
            theta = n1,
            fill = "toself",
            mode = "markers",
            name='Índice 2015')%>%
  add_trace(r = m17,
            theta = n2,
            fill = "toself",
            mode = "markers",
            name='Índice 2017')%>%
  layout(
    polar = list(
      radialaxis = list(
        visible = T,
        range = c(0,1)
      )
    )
  ) 
%>%
  subplot(ncols = 3)
)

It gives me the following plot:enter image description here

The plot is superimposed. The subplots doesn't seem to work. I need three plots in the same row. How to do this ? I tried something like what is in plotly guide the but it didn't work.

I appreciate any help.

1 Answers

Slightly adjusted example from the documentation:

library(plotly)

fig <- plot_ly(
  type = 'scatterpolar',
  mode = 'lines'
) 
fig <- fig %>%
  add_trace(
    r = c(5, 4, 2, 4, 5),
    theta = c("a", "b", "c", "d", "a"),
    name = 'angular categories',
    fill = 'toself'
  ) 
fig <- fig %>%
  add_trace(
    r = c("a", "b", "c", "d", "b", "f", "a"),
    theta = c(1, 4, 2, 1.5, 1.5, 6, 5),
    thetaunit = 'radians',
    name = 'radial categories',
    fill = 'toself',
    subplot = 'polar2'
  ) 
fig <- fig %>%
  add_trace(
    r = c(5, 4, 2, 4, 5),
    theta = c("a", "b", "c", "d", "a"),
    name = 'angular categories (w/ categoryarray)',
    fill = 'toself',
    subplot = 'polar3'
  ) 

fig

Gives:

output

Related