Polar Charts in R

How to create Polar Charts in R with Plotly.


New to Plotly?

Plotly is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.

Polar Charts 1.0

Looking for the old polar chart docs? See legacy polar charts

Basic Polar Charts

library(plotly)

fig <- plot_ly(
  type = 'scatterpolar',
  r = c(0,1,2,2),
  theta = c(0,45,90,0),
  mode = 'markers'
)

fig

Line Polar Charts

library(plotly)

df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/polar_dataset.csv")

fig <- plot_ly(
  df,
  type = 'scatterpolar',
  mode = 'lines'
  ) 
fig <- fig %>%
  add_trace(
    r = ~x1,
    theta = ~y,
    name = 'Figure8',
    line = list(
      color = 'peru'
    )
  ) 
fig <- fig %>%
  add_trace(
    r = ~x2,
    theta = ~y,
    name = 'Cardioid',
    line = list(
      color = 'darkviolet'
    )
  ) 
fig <- fig %>%
  add_trace(
    r = ~x3,
    theta = ~y,
    name = 'Hypercardioid',
    line = list(
      color = 'deepskyblue'
    )
  ) 
fig <- fig %>%
  add_trace(
    r = ~x4,
    theta = ~y,
    name = 'Subcardioid',
    line = list(
      color = 'orangered'
    )
  ) 
fig <- fig %>%
  add_trace(
    r = ~x5,
    theta = ~y,
    name = 'Supercardioid',
    line = list(
      color = 'green'
    )
  ) 

fig <- fig %>%
  layout(
    title = 'Mic Patterns',
    font = list(
      family = 'Arial',
      size = 12,
      color = '#000'
    ),
    showlegend = F
  )

fig

Area Polar Charts

library(plotly)

fig <- plot_ly(
    type = 'scatterpolar',
    mode = 'lines'
  ) 
fig <- fig %>%
  add_trace(
    r = c(0, 1.5, 1.5, 0, 2.5, 2.5, 0),
    theta = c(0, 10, 25, 0, 205, 215, 0),
    fill = 'toself',
    fillcolor = '#709Bff',
    line = list(
      color = 'black'
    )
  ) 
fig <- fig %>%
  add_trace(
    r = c(0, 3.5, 3.5, 0),
    theta = c(0, 55, 75, 0),
    fill = 'toself',
    fillcolor = '#E4FF87',
    line = list(
      color = 'black'
    )
  ) 
fig <- fig %>%
  add_trace(
    r = c(0, 4.5, 4.5, 0, 4.5, 4.5, 0),
    theta = c(0, 100, 120, 0, 305, 320, 0),
    fill = 'toself',
    fillcolor = '#FFAA70',
    line = list(
      color = 'black'
    )
  ) 
fig <- fig %>%
  add_trace(
    r = c(0, 4, 4, 0),
    theta = c(0, 165, 195, 0),
    fill = 'toself',
    fillcolor = '#FFDF70',
    line = list(
      color = 'black'
    )
  ) 
fig <- fig %>%
  add_trace(
    r = c(0, 3, 3, 0),
    theta = c(0, 262.5, 277.5, 0),
    fill = 'toself',
    fillcolor = '#B6FFB4',
    line = list(
      color = 'black'
    )
  ) 
fig <- fig %>%
  layout(
    polar = list(
      radialaxis = list(
        visible = T,
        range = c(0,5)
      )
    ),
    showlegend = F
  )

fig

Categorical Polar Charts

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 <- fig %>%
  add_trace(
    r = c("a", "b", "c", "d", "b", "f", "a", "a"),
    theta = c(45, 90, 180, 200, 300, 15, 20, 45),
    name = 'radial categories (w/ category descending)',
    fill = 'toself',
    subplot = 'polar4'
  ) 
fig <- fig %>%
  layout(
    polar = list(
      domain = list(
        x = c(0,0.46),
        y = c(0.56,1)
      ),
      radialaxis = list(
        angle = 45
      ),
      angularaxis = list(
        direction = 'clockwise',
        period = 6
      )
    ),
    polar2 = list(
      domain = list(
        x = c(0,0.46),
        y = c(0,0.44)
      ),
      radialaxis = list(
        angle = 180,
        tickangle = -180
      )
    ),
    polar3 = list(
      domain = list(
        x = c(0.54,1),
        y = c(0.56,1)
      ),
      sector = c(150,400),
      radialaxis = list(
        angle = -45
      ),
      angularaxis = list(
        categoryarray = c("d", "a", "c", "b")
      )
    ),
    polar4 = list(
      domain = list(
        x = c(0.54,1),
        y = c(0,0.44)
      ),
      radialaxis = list(
        categoryorder = "category descending"
      ),
      angularaxis = list(
        thetaunit= "radians",
        dtick = 0.3141592653589793
      )
    )
  )

fig

Polar Charts Directions

library(plotly)

fig <- plot_ly(
    type = 'scatterpolar',
    mode = "lines+markers"
  ) 
fig <- fig %>%
  add_trace(
    r = c(1,2,3,4,5),
    theta = c(0,90,180,360,0),
    line = list(
      color = "#ff66ab"
    ),
    marker = list(
      color = "#8090c7",
      symbol = 'square',
      size = 8
    ),
    text = "sector: 135->225<br>rotation: 90<br>direction: counterclockwise"
  ) 
fig <- fig %>%
  add_trace(
    r = c(1,2,3,4,5),
    theta = c(0,90,180,360,0),
    line = list(
      color = "#ff66ab"
    ),
    marker = list(
      color = "#8090c7",
      symbol = 'square',
      size = 8
    ),
    text = "sector: 135->225<br>rotation: 90<br>direction: counterclockwise",
    subplot = 'polar2'
  ) 
fig <- fig %>%
  layout(
    polar = list(
      domain = list(
        x = c(0,0.4),
        y = c(0,1)
      ),
      radialaxis = list(
        tickfont = list(
          size = 8
        )
      ),
      angularaxis = list(
        tickfont = list(
          size = 8
        ),
        rotation = 90,
        direction = 'counterclockwise'
      )
    ),
    polar2 = list(
      domain = list(
        x = c(0.6,1),
        y = c(0,1)
      ),
      radialaxis = list(
        tickfont = list(
          size = 8
        )
      ),
      angularaxis = list(
        tickfont = list(
          size = 8
        ),
        rotation = 90,
        direction = 'clockwise'
      )
    ),
    showlegend = F
  )

fig

Polar Charts Sector

library(plotly)

fig <- plot_ly(
  type = 'scatterpolar',
  mode = "lines+markers"
) 
fig <- fig %>%
  add_trace(
    r = c(1,2,3,4,5),
    theta = c(0,90,180,360,0),
    line = list(
      color = "#ff66ab"
    ),
    marker = list(
      color = "#8090c7",
      symbol = 'square',
      size = 8
    )
  ) 
fig <- fig %>%
  add_trace(
    r = c(1,2,3,4,5),
    theta = c(0,90,180,360,0),
    line = list(
      color = "#ff66ab"
    ),
    marker = list(
      color = "#8090c7",
      symbol = 'square',
      size = 8
    ),
    subplot = 'polar2'
  ) 
fig <- fig %>%
  layout(
    polar = list(
      domain = list(
        x = c(0,0.4),
        y = c(0,1)
      ),
      sector = c(150,210),
      radialaxis = list(
        tickfont = list(
          size = 8
        )
      ),
      angularaxis = list(
        tickfont = list(
          size = 8
        )
      )
    ),
    polar2 = list(
      domain = list(
        x = c(0.6,1),
        y = c(0,1)
      ),
      radialaxis = list(
        tickfont = list(
          size = 8
        )
      ),
      angularaxis = list(
        tickfont = list(
          size = 8
        )
      )
    ),
    showlegend = F
  )

fig

Polar Charts Subplot

library(plotly)

fig <- plot_ly(
    type = 'scatterpolar',
    mode = 'lines'
) 
fig <- fig %>%
  add_trace(
    r = c(1,2,3),
    theta = c(50,100,200),
    marker = list(
      symbol = 'square'
    )
  ) 
fig <- fig %>%
  add_trace(
    r = c(1,2,3),
    theta = c(1,2,3),
    thetaunit = 'radians'
  ) 
fig <- fig %>%
  add_trace(
    r = c("a", "b", "c", "d"),
    theta = c("D","C","B","A"),
    subplot = 'polar2'
  ) 
fig <- fig %>%
  add_trace(
    r = c(50,300,900),
    theta = c(0,90,180),
    subplot = 'polar3'
  ) 
fig <- fig %>%
  add_trace(
    r = c(3,3,4,3),
    theta = c(0,45,90,270),
    fill = 'toself',
    subplot = 'polar4'
  ) 
fig <- fig %>%
  layout(
    polar = list(
      domain = list(
        x = c(0,0.46),
        y = c(0.56,1)
      ),
      radialaxis = list(
        range = c(1,4)
      ),
      angularaxis = list(
        thetaunit = 'radians'
      )
    ),
    polar2 = list(
      domain = list(
        x = c(0,0.46),
        y = c(0,0.42)
      )
    ),
    polar3 = list(
      domain = list(
        x = c(0.54,1),
        y = c(0.56,1)
      ),
      sector = c(0,180),
      radialaxis = list(
        type = 'log',
        angle = 45
      )
    ),
    polar4 = list(
      domain = list(
        x = c(0.54,1),
        y = c(0,0.44)
      ),
      radialaxis = list(
        visible = F,
        range = c(0,6)
      )
    ),
    showlegend = F
  )

fig

Webgl Polar Charts

library(plotly)

df <- read.csv("https://raw.githubusercontent.com/plotly/datasets/master/hobbs-pearson-trials.csv")

fig <- plot_ly(
    type = 'scatterpolargl',
    mode = 'markers'
  )

j = 1
k = 2
for (i in 1:(length(df)/2)){
 fig <- add_trace(
    fig,
    r = df[,j],
    theta = df[,k],
    name = paste('Trial ', i),
    marker = list(
      size = 15,
      line = list(
        color = '#FFF'
      ),
      opacity = 0.7
    )
  )
  j <- j + 2
  k <- k + 2
}

fig <- layout(
  fig,
  title = "Hobbs-Pearson Trials",
  showlegend = F,
  paper_bgcolor = "rgb(223, 223, 223)",
  polar = list(
    bgcolor = "rgb(223, 223, 223)",
    angularaxis = list(
      tickwidth = 2,
      linewidth = 3,
      layer = 'below traces'
    ),
    radialaxis = list(
      side = 'counterclockwise',
      showline = T,
      linewidth = 2,
      tickwidth = 2,
      gridcolor = '#FFF',
      gridwidth = 2
    )
  )
)

fig

Reference

See https://plotly.com/r/reference/#polar for more information and options!

What About Dash?

Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.

Learn about how to install Dash for R at https://dashr.plot.ly/installation.

Everywhere in this page that you see fig, you can display the same figure in a Dash for R application by passing it to the figure argument of the Graph component from the built-in dashCoreComponents package like this:

library(plotly)

fig <- plot_ly() 
# fig <- fig %>% add_trace( ... )
# fig <- fig %>% layout( ... ) 

library(dash)
library(dashCoreComponents)
library(dashHtmlComponents)

app <- Dash$new()
app$layout(
    htmlDiv(
        list(
            dccGraph(figure=fig) 
        )
     )
)

app$run_server(debug=TRUE, dev_tools_hot_reload=FALSE)