Open In App

Polar Charts in R

Last Updated : 29 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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.

R




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:

gh

Polar Charts in R

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

  • The ggplot function is used to initialize the chart, and aes is used to specify the aesthetics, with Category on the x-axis.
  • We use geom_polygon to plot the data as polygons, with Value1 and Value2 as the y-values. The fill aesthetic is used to distinguish between the two variables.
  • scale_fill_manual is used to define custom colors for the two variables.
  • coord_polar(start = 0) is added to make the chart circular, and theme_minimal() provides a simple, clean background.
  • Finally, we print the polar_chart object to display the chart.

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.

R




# 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:

gh

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.

  • geom_bar(stat = “identity”): This line adds a bar chart to the ggplot object. The stat = “identity” argument indicates that the “Value” column contains the exact heights of the bars. As a result, it creates a stacked bar chart where each Category has a stacked bar, and the heights of the Subcategory segments are determined by the “Value” column.
  • coord_polar(start = 0): This line converts the Cartesian coordinates of the bar chart into polar coordinates, turning it into a radial stacked bar polar chart. The start = 0 argument specifies that the polar chart should start at the 0-degree angle (usually at the top).
  • theme_minimal(): This line sets the chart’s theme to a minimal style, which typically removes background gridlines and other non-essential elements to keep the chart clean and easy to read.

Polar Histogram

R




# 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:

gh

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.

  • geom_histogram(binwidth = 0.2, fill = “blue”, alpha = 0.7) +: This line adds a histogram layer to the ggplot object. It creates bins for the “Angle” variable with a bin width of 0.2 radians. The fill argument sets the fill color of the bars to blue, and the alpha argument sets the transparency of the bars to 0.7, making them partially transparent.
  • coord_polar(start = 0) +: This line converts the Cartesian coordinates of the histogram into polar coordinates, effectively creating a polar histogram. The start = 0 argument specifies that the polar chart should start at 0 degrees (usually at the top).
  • theme_minimal() +: This line sets the theme of the chart to a minimal style, removing background gridlines and other non-essential elements to keep the chart clean.
  • labs(title = “Polar Histogram”): This line adds a title to the chart, which is set to “Polar Histogram.”

Advanced Polar Chart

R




# 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:

gh

Polar Charts in R

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

  • We use ggplot2 to create the polar chart. We map the Category to the x-axis, Score to the y-axis, and color the lines and points by the individuals’ names.
  • We use geom_line() to draw lines connecting the data points and geom_point() to add points for each data point.
  • coord_polar(start = 0) is used to create the polar coordinate system, and we set the starting angle to 0 degrees.
  • We customize the chart’s appearance, including titles, labels, and colors using the theme() and labs() functions.

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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads