R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
Pie charts are used to display the proportion of each category in a categorical dataset. Each slice of the pie represents a particular category, and its size is proportional to the number of occurrences or the percentage of that category.
In this tutorial, we'll cover:
ggplot2
Pie charts are suitable for data that represents categorical variables with relatively few levels. However, they can become hard to interpret when there are too many categories, or when the difference in proportions between categories is small.
R's base graphics package provides a simple pie()
function to create pie charts.
Example:
Suppose we have a dataset that represents the number of products sold in different categories:
# Sample data products <- c("Laptop" = 25, "Mobile" = 50, "Tablet" = 15, "Desktop" = 10) # Create Pie Chart pie(products, main="Products Sold", col=rainbow(length(products)), labels=paste(names(products), "\n", products))
The labels show both the product name and the count, and the col
argument assigns a different color to each slice.
ggplot2
ggplot2
is a versatile plotting package in R that can create more polished graphics, including pie charts, with additional functionality.
First, you need to install and load the ggplot2
package:
install.packages("ggplot2") library(ggplot2)
Now, let's create a pie chart:
# Convert the products vector to a data frame df <- data.frame(Category = names(products), Count = as.numeric(products)) # Create Pie Chart ggplot(df, aes(x = "", y = Count, fill = Category)) + geom_bar(width = 1, stat = "identity") + coord_polar(theta = "y") + labs(title = "Products Sold", x = NULL, y = NULL) + theme_void()
The coord_polar
function converts the bar chart (which is the default for geom_bar
) into a pie chart. The theme_void
removes axis labels and other unnecessary details for a pie chart.
Pie charts are a simple way to visualize categorical data in R. While the base graphics approach offers straightforward functionality, ggplot2
provides more advanced customization options. However, always ensure that a pie chart is the appropriate visualization for your data, and avoid using them when you have many categories or when the differences in proportions are too subtle to distinguish visually.
Creating pie plots with ggplot2 in R:
# Example using ggplot2 library(ggplot2) data <- data.frame(Category = c("A", "B", "C", "D", "E"), Value = c(30, 25, 20, 15, 10)) ggplot(data, aes(x = "", y = Value, fill = Category)) + geom_bar(stat = "identity", width = 1, color = "white") + coord_polar("y") + theme_void() + labs(title = "Pie Chart with ggplot2", fill = "Category")
R code for pie chart visualization:
pie()
function in base R to create a simple pie chart.# Example using base R data <- c(30, 25, 20, 15, 10) labels <- c("A", "B", "C", "D", "E") pie(data, labels = labels, main = "Simple Pie Chart")
Customizing colors and labels in R pie charts:
# Example with custom colors and labels colors <- c("#66c2a5", "#fc8d62", "#8da0cb", "#e78ac3", "#a6d854") labels <- c("Category A", "Category B", "Category C", "Category D", "Category E") pie(data, labels = labels, col = colors, main = "Customized Pie Chart")
Exploding and highlighting slices in R pie charts:
# Example with exploded slice exploded_slice <- c(0, 0, 0.1, 0, 0) pie(data, labels = labels, explode = exploded_slice, main = "Exploded Pie Chart")
Interactive pie charts in R with Shiny:
# Example Shiny app with interactive pie chart library(shiny) ui <- fluidPage( plotOutput("pie_chart") ) server <- function(input, output) { output$pie_chart <- renderPlot({ # Dynamic pie chart based on user input # ... }) } shinyApp(ui, server)
3D pie charts in R:
# Example using plotly package library(plotly) plot_ly(labels = labels, values = data, type = "pie", 3D = TRUE, textinfo = "percent+label", insidetextorientation = "radial", hoverinfo = "label+percent") %>% layout(title = "3D Pie Chart")
Handling small percentages in pie charts with R:
# Example handling small percentages threshold <- 5 small_percentages <- data[data < threshold] data_combined <- c(sum(data[data >= threshold]), small_percentages) labels_combined <- c("Combined", paste("Small <", threshold)) pie(data_combined, labels = labels_combined, main = "Handling Small Percentages")
Combining multiple pie charts in R:
# Example combining multiple pie charts par(mfrow = c(1, 2)) pie(data, labels = labels, main = "Pie Chart 1") pie(data * 1.5, labels = labels, main = "Pie Chart 2")