Open In App

How to Make Lines of Radar Chart Round in R Using Plotly

The Radar chart is used to represent multivariate independent data graphically. It’s a circular chart where each independent variable has its own axis and all those axes are merged at the center of the radar chart. It is also known as Web Chart because it looks like Spider Web. This chart is used when one wants to analyze the relationships among independent variables. In R Programming this can be Implemented using plot_ly() function.

To install and load the plot_ly, use the commands given below.



install.packages("plotly")
library(plotly)

By default the lines that are used to plot the data in the radar chart are linear to make them rounded we have to specify line shape as spline in plot_ly() function. (The spline in mathematics is known as the rounded curve that passes through the points).

Syntax: plot_ly(type,mode,r,theta,fill,line,layout())



where,

  • type – used to set the type of plot (scatterpolar for radar chart)
  • mode – used the specify the mode of the plot border(line)
  • r – vector containing the points to be plotted on the radius of axes
  • theta – Sectors the radar plot axes based on the number of values provided to it
  • line – used to specify the type of line as borders of the plot (spline)
  • layout() – used to customize the layout of the plot 

Now, we can start with the simple radar chart.




# Plotting the radar plot using plot_ly() function
fig <- plotly::plot_ly(
  type = 'scatterpolar',
  mode='lines',
  r = c(14,12,8,8,6,14),
  theta = c('RCB','CSK','MI','RR','KKR','RCB'),
  fill = 'toself',
  line = list(shape = 'spline'),
  name="2016"
)
# Specifying the layout of radar plot
fig <- fig %>% layout(polar=list(radialaxis = 
                                 list(visible = T,range = c(0,20))),
                      title="Radar Plot using Plotly")
fig

Output:

 

We can also plot Nested Radar Plot for 2 or more traces. Let’s see the code for that.




fig <- plotly::plot_ly(
  type = 'scatterpolar',
  mode='lines',
  r = c(14,12,8,8,6,14),
  theta = c('RCB','CSK','MI','RR','KKR','RCB'),
  fill = 'toself',
  line = list(shape = 'spline'),
  name="2016"
)
# Adding another trace to the plot
fig <- fig %>% add_trace(r = c(8,12,12,10,6,8),
                         theta = c('RCB','CSK','MI',
                                   'RR','KKR','RCB'),
                         fill = 'toself',
                         line = list(shape = 'spline'),
                         name="2017")
# Specifying the layout of radar plot
fig <- fig %>% layout(polar=list(radialaxis = 
                                 list(visible = T,
                                      range = c(0,20))),
                      title="Nested Radar Plot using Plotly")
fig

Output:

 

Let’s Customized Radar Plot background.




# Plotting the radar plot using plot_ly() function
fig <- plotly::plot_ly(
  type = 'scatterpolar',
  mode='lines',
  r = c(14,12,8,8,6,14),
  theta = c('RCB','CSK','MI','RR','KKR','RCB'),
  fill = 'toself',
  line = list(shape = 'spline'),
  name="2016")
  
# Specifying the layout of radar plot
fig <- fig %>% layout(polar=list(radialaxis = list(visible = T,
                                 range = c(0,20),gridcolor="blue",
                                   linecolor="red"),bgcolor="pink"),
                                   title="Customized Radar Plot using Plotly")
fig

Output:

 


Article Tags :