R – Pie Charts

R – Pie Charts in detail

A pie chart is a circular statistical graphic divided into slices to illustrate numerical proportions. Each sector (or slice) represents the relative sizes of data. It is also known as a circle graph, where a circular chart is cut into segments to describe relative frequencies or magnitudes.

The R programming language provides the pie() function to create pie charts. It takes positive numbers as a vector input.

Syntax:

pie(x, labels, radius, main, col, clockwise)

Parameters:

  • x: A vector containing numeric values used in the pie chart.
  • labels: Descriptions for the slices in the pie chart.
  • radius: Defines the radius of the circle (value between -1 and +1).
  • main: Title of the pie chart.
  • clockwise: Logical value indicating whether slices are drawn clockwise or counterclockwise.
  • col: Specifies colors for the pie slices.

Creating a Simple Pie Chart

By using the above parameters, we can create a basic pie chart with labels.

Example:

# Create data for the graph
values <- c(30, 50, 40, 60)
labels <- c("Apple", "Banana", "Grapes", "Mango")

# Plot the chart
pie(values, labels)

Output:

Pie Chart with Title and Colors

We can enhance the pie chart by adding a title and colors using the col parameter.

Example:

# Create data for the graph
values <- c(25, 45, 35, 55)
labels <- c("New York", "London", "Tokyo", "Sydney")

# Plot the chart with title and rainbow color palette
pie(values, labels, main = "City Pie Chart",
    col = rainbow(length(values)))

Output:

Pie Chart with Color Palettes

Using the RColorBrewer package to add colors to a pie chart.

# Load necessary library
library(RColorBrewer)

# Create data for the graph
sales <- c(40, 60, 30, 50)
cities <- c("New York", "Los Angeles", "Chicago", "Houston")

# Assign colors using brewer.pal
colors <- brewer.pal(length(sales), "Set2")

# Plot the pie chart
pie(sales, labels = cities, col = colors)

Output:

Modify Border Line Type

Using the lty argument to change the border style.

# Load necessary library
library(RColorBrewer)

# Create data for the graph
sales <- c(40, 60, 30, 50)
cities <- c("New York", "Los Angeles", "Chicago", "Houston")

# Assign colors using brewer.pal
colors <- brewer.pal(length(sales), "Set2")

# Plot the pie chart with modified border type
pie(sales, labels = cities, col = colors, lty = 2)

Output:

Add Shading Lines

Using the density and angle arguments to add shading.

# Load necessary library
library(RColorBrewer)

# Create data for the graph
sales <- c(40, 60, 30, 50)
cities <- c("New York", "Los Angeles", "Chicago", "Houston")

# Assign colors using brewer.pal
colors <- brewer.pal(length(sales), "Set2")

# Plot the pie chart with shading lines
pie(sales, labels = cities, col = colors, density = 50, angle = 45)

Output:

3D Pie Chart

Using the plotrix package to create a 3D pie chart.

# Load necessary library
library(plotrix)

# Create data for the graph
sales <- c(40, 60, 30, 50)
cities <- c("New York", "Los Angeles", "Chicago", "Houston")

# Calculate percentages
sales_percent <- round(100 * sales / sum(sales), 1)

# Plot the 3D pie chart
pie3D(sales, labels = sales_percent,
      main = "Sales Distribution", col = rainbow(length(sales)))

# Add a legend
legend("topright", cities, cex = 0.5, fill = rainbow(length(sales)))

Output:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *