Open In App

Polar Charts in R

Polar charts, sometimes referred to as radial charts or spider charts, are effective tools for data visualization that show data points in a circular, two-dimensional layout. In R Programming Language These graphs are very helpful for showing multivariate data, highlighting patterns, and comparing various variables across various categories or data points. Polar charts are simple to make in R using a variety of tools, with ggplot2 being one of the most common options due to its adaptability and customization possibilities.

Understanding Polar Charts

Polar charts are essentially a variation of the standard Cartesian coordinate system. Instead of using x and y coordinates, they use a radial system where data points are plotted on a circle. The angle (θ) from the center of the circle to the data point represents one variable, and the distance from the center to the data point (r) represents another variable. Each category or data point is represented by a separate spoke extending from the center of the circle.



In this article, we will explore how to create polar charts in R using ggplot2 and demonstrate how to customize them to suit your specific data visualization needs.

Creating a Basic Polar Chart

Let’s start by creating a basic polar chart to visualize a fictitious dataset. Suppose you have a dataset with three variables: Category, Value1, and Value2. We will use these variables to construct a simple polar chart.






library(ggplot2)
 
# Sample data
data <- data.frame(
  Category = c("A", "B", "C", "D", "E"),
  Value1 = c(3, 4, 2, 5, 1),
  Value2 = c(4, 2, 5, 1, 3)
)
 
# Creating a basic polar chart
polar_chart <- ggplot(data, aes(x = Category)) +
  geom_polygon(aes(y = Value1, fill = "Value1"), stat = "identity", alpha = 0.5) +
  geom_polygon(aes(y = Value2, fill = "Value2"), stat = "identity", alpha = 0.5) +
  scale_fill_manual(values = c("Value1" = "blue", "Value2" = "red")) +
  coord_polar(start = 0) +
  theme_minimal()
 
# Display the chart
print(polar_chart)

Output:

Polar Charts in R

We create a sample dataset named data with two variables, Value1 and Value2, and a categorical variable Category.

Radial Stacked Bar Polar Chart

In this example, we’ll create a polar chart that represents a stacked bar chart in a radial format. We’ll use sample data with categories and subcategories.




# Sample data
data <- data.frame(
  Category = rep(LETTERS[1:5], each = 3),
  Subcategory = rep(c("X", "Y", "Z"), times = 5),
  Value = runif(15, min = 1, max = 10)
)
 
# Create a radial stacked bar polar chart
polar_chart <- ggplot(data, aes(x = Category, y = Value, fill = Subcategory)) +
  geom_bar(stat = "identity") +
  coord_polar(start = 0) +
  theme_minimal()
 
# Display the chart
print(polar_chart)

Output:

Polar Charts in R

polar_chart <- ggplot(data, aes(x = Category, y = Value, fill = Subcategory)): This line initializes a ggplot object named polar_chart. It specifies that the “Category” variable will be mapped to the x-axis, the “Value” variable will be mapped to the y-axis, and the “Subcategory” variable will be used to fill the bars. This means that each Category will have a stacked bar, and the Subcategories will determine the segments of the stacked bars.

Polar Histogram




# Sample data
# Generate random angles
data <- data.frame(
  Angle = runif(100, 0, 2 * pi
)
 
# Create a polar histogram
polar_hist <- ggplot(data, aes(x = Angle)) +
  geom_histogram(binwidth = 0.2, fill = "blue", alpha = 0.7) +
  coord_polar(start = 0) +
  theme_minimal() +
  labs(title = "Polar Histogram")
 
# Display the chart
print(polar_hist)

Output:

Polar Charts in R

polar_hist <- ggplot(data, aes(x = Angle)) +: This line initializes a ggplot object named polar_hist. It specifies that the “Angle” variable from the data data frame will be mapped to the x-axis of the plot.

Advanced Polar Chart




# Load the required library
library(ggplot2)
 
# Create a corrected sample dataset
data <- data.frame(
  Name = rep(c("Person A", "Person B", "Person C"), each = 3),
  Category = rep(c("Coding", "Communication", "Problem Solving"), times = 3),
  Score = c(90, 75, 85, 80, 70, 95, 70, 90, 85)
)
 
# Create a polar chart
ggplot(data, aes(x = Category, y = Score, group = Name, color = Name)) +
  geom_line(size = 1) +
  geom_point(size = 3) +
  coord_polar(start = 0) +  # Set the starting angle to 0 degrees
  theme_minimal() +
  labs(title = "Skill Assessment",
       subtitle = "Comparison of Skills in Different Categories",
       caption = "Source: Example Data",
       color = "Name") +
  theme(
    axis.text = element_text(size = 12),
    axis.title = element_blank(),
    plot.title = element_text(size = 16, hjust = 0.5),
    plot.subtitle = element_text(size = 14, hjust = 0.5),
    plot.caption = element_text(size = 10, hjust = 0),
    legend.title = element_text(size = 12),
    legend.text = element_text(size = 10)
  )

Output:

Polar Charts in R

We create a sample dataset (data) that contains the names of individuals (Name), skill categories (Category), and corresponding scores (Score).

Conclusion

Polar charts in R are an effective way to visualize multivariate data in a circular format. By understanding the basics and using examples like the ones provided, you can create informative and customized polar charts to reveal patterns and relationships in your data. Remember that ggplot2 offers extensive customization options, so you can tailor your charts to your specific data visualization needs.


Article Tags :