Site icon R-bloggers

Creating Interactive Radar Charts in R with the ‘fmsb’ Library

[This article was first published on Steve's Data Tips and Tricks, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
< section id="introduction" class="level1">

Introduction

Radar charts, also known as spider, web, polar, or star plots, are a useful way to visualize multivariate data. In R, we can create radar charts using the fmsb library. Here are several examples of how to create radar charts in R using the fmsb library:

< section id="examples" class="level1">

Examples

< section id="example-1-basic-radar-chart" class="level2">

Example 1: Basic radar chart

The following code creates a basic radar chart using the radarchart() function from the fmsb package. The input data format is specific, where each row represents an entity and each column is a variable. The first row should be the maximum values of the data, and the second row should be the minimum values. The default radar chart can be customized using various options, such as line color, fill color, line width, and axis label color.

# Load the fmsb package
library(fmsb)

# Create sample data
data <- as.data.frame(matrix(sample(2:20, 10, replace = T), 
                             ncol = 10))
colnames(data) <- c("Var1", "Var2", "Var3", "Var4", "Var5", 
                    "Var6", "Var7", "Var8", "Var9", "Var10")
data <- rbind(rep(20, 10), rep(0, 10), data)

# Create a basic radar chart
radarchart(data)

< section id="example-2-customized-radar-chart" class="level2">

Example 2: Customized radar chart

The following code creates a customized radar chart using the radarchart() function. The axistype argument is set to 1 to customize the polygon, and the pcol, pfcol, and plwd arguments are used to customize the grid and line properties.

# Create sample data
data <- as.data.frame(matrix(sample(2:20, 10, replace = T), 
                             ncol = 10))
colnames(data) <- c("Var1", "Var2", "Var3", "Var4", "Var5", 
                    "Var6", "Var7", "Var8", "Var9", "Var10")
data <- rbind(rep(20, 10), rep(0, 10), data)

# Customize the radar chart
radarchart(data, axistype = 1, pcol = rgb(0.2, 0.5, 0.5, 0.9), 
           pfcol = rgb(0.2, 0.5, 0.5, 0.5), plwd = 4, cglcol =
          "grey", cglty = 1)

< section id="example-3-radar-chart-with-multiple-groups" class="level2">

Example 3: Radar chart with multiple groups

The following code creates a radar chart with multiple groups using the radarchart() function. The input data frame should have more than three variables as axes, and the rows indicate cases as series. The first row should show the maximum values, the second row should show the minimum values, and the actual data should be given as row 3 and lower rows.

# Create sample data
set.seed(1)
data <- data.frame(rbind(rep(10, 8), rep(0, 8), 
                         matrix(sample(0:10, 24, replace = TRUE),
                                nrow = 3)))
colnames(data) <- paste("Var", 1:8)

# Create a radar chart with multiple groups
radarchart(data, axistype = 1, pcol = 1, plwd = 2, 
           pdensity = 10, pangle = 40, cglty = 1, 
           cglcol = "gray")

< section id="conclusion" class="level1">

Conclusion

These are just a few examples of how to create radar charts in R using the fmsb library. With some creativity, you can customize the charts to suit your needs. I encourage you to try creating your own radar charts using the fmsb library and experiment with different options to see what works best for your data.

< section id="references" class="level1">

References:

To leave a comment for the author, please follow the link and comment on their blog: Steve's Data Tips and Tricks.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Exit mobile version