Hello and welcome! In today's lesson, we will venture into the world of Pie Charts in R. Pie charts are an elegant way to showcase proportions in relation to the total — for instance, illustrating populations by continents. Today, you will learn how to create and customize pie charts in R using the ggplot2
package.
Pie charts are a graphical representation in which a circle, or "the pie," is divided into individual "slices," each representing a category. Picture a pizza: each slice represents a distinct fruit type — apples, oranges, bananas, and grapes. The size of each piece represents the quantity of the fruit. We'll be using the versatile R data visualization library, ggplot2
, to generate interactive pie charts.
Let's create a pie chart that depicts the favorite fruits of a group of individuals. This could represent consumer preferences in a market study. Here's the dataset:
R1# In R, we can use data.frame to create a dataframe for plotting 2df <- data.frame(Fruits = c('Apples', 'Bananas', 'Cherries', 'Dates'), 3 Counts = c(15, 30, 45, 10))
Let's start creating a pie chart for this dataset.
Pie Chart Components:
In crafting the Pie Chart with the ggplot2
library, we can create an informative and attractive pie chart. We'll first generate a bar plot and then use coord_polar()
to convert it to a pie chart.
Pie charts are essentially bar plots transformed into a circular format. ggplot2
doesn't have a direct geom
function for pie charts, so we craft a bar plot first and then transform it. Using coord_polar()
is a convenient way to convert bar plots into pie charts because it translates the Cartesian coordinates of a bar plot into polar coordinates, thus giving us the circular shape of a pie chart.
R1library(ggplot2) 2 3# Create a basic bar plot 4plot <- ggplot(df, aes(x="", y=Counts, fill=Fruits)) + geom_bar(width = 1, stat = "identity") 5 6# Convert the bar plot into a pie chart 7plot <- plot + coord_polar("y", start=0) + ggtitle('Fruits Preferences')
This chart helps us visualize how preferences are divided—similar to illustrating market shares in business.
Our pie charts can appear even more visually appealing with custom colors. ggplot2
enables us to assign a specific color to each slice of our pie via the scale_fill_manual()
function.
Let's examine an example with scaling and custom colors:
R1# Array of custom colors 2colors <- c('lightgreen', 'yellow', 'pink', 'skyblue') 3# Convert the bar plot into a pie chart 4plot <- plot + scale_fill_manual(values=colors) + coord_polar("y", start=0) + ggtitle('Fruits Preferences')
We have now added distinct colors to each slice of our pie chart!
Let's add labels directly to the pie chart slices, which include both the fruit name and the corresponding percentage.
R1# Calculate percentages 2df$Percentage <- round(df$Counts / sum(df$Counts) * 100, 1) 3 4# Create the basic bar plot 5plot <- ggplot(df, aes(x="", y=Counts, fill=Fruits)) + 6 geom_bar(width=1, stat="identity") + 7 coord_polar("y", start=0) + 8 ggtitle('Fruits Preferences') + 9 geom_text(aes(label=paste(Fruits, Percentage, "%")), 10 position=position_stack(vjust=0.5), 11 size=5)
In ggplot2
, positioning text labels correctly on a pie chart involves calculating the appropriate positional coordinates for each text label. Here's how R achieves this:
position_stack(vjust=0.5)
: The position_stack
function helps to vertical-center the text within each "slice" of the pie by adjusting the vertical justification (vjust
).Bravo! You have mastered the fundamentals of pie charts, constructed one using R's ggplot2
package, and personalized it for better visualization. Enhancing your knowledge comes with practice. Our next activity will involve practising the creation and customization of pie charts. This task will consolidate your understanding and enhance your R data visualization skills. Let's keep going!