Creating a Pie Chart in R with Custom Colors

Creating a Pie Chart in R with Custom Colors

In this article, we will explore how to create a pie chart in R that displays gender-specific vote data for green topics. We will cover the basics of creating a pie chart, including customizing colors and labels.

Introduction to Pie Charts

A pie chart is a circular graph that shows how different categories contribute to an entire group or total. It’s a useful tool for displaying information that needs to be broken down into distinct parts. In this article, we’ll use the ggplot2 package in R to create a pie chart with custom colors and labels.

Prerequisites

Before we begin, make sure you have R installed on your computer. If you don’t have ggplot2, you can install it using the following command:

install.packages("ggplot2")

Step 1: Load Required Libraries

To create a pie chart with custom colors and labels, we need to load the required libraries. In this case, we’ll only need ggplot2.

library(ggplot2)

Creating the Data Frame

The next step is to create a data frame that contains the vote data for men and women. Since we have percentages for both genders, we can use these values directly in our code.

# Create a data frame with vote data
slices <- c(36, 64) # Percentage of votes for men and women respectively
lbls <- c("male", "female") # Labels for the pie chart

Creating the Pie Chart

Now we can create the pie chart using ggplot2. We’ll use the geom_col() function to create a column chart, but since this is a pie chart, we won’t be plotting values on an x-axis. Instead, we’ll plot them in a circular arrangement.

# Create the pie chart
ggplot(data = data.frame(x = 1, y = slices, Sex = lbls),
       aes(x, y, fill = Sex)) +
  geom_col() +
  coord_polar(theta = "y") +
  theme_void() +
  geom_text(aes(label = paste0(y, "%")), colour = "white", 
            position = position_stack(vjust = 0.5), size = 6) +
  scale_fill_manual(values = c("deepskyblue4", "tomato2")) +
  ggtitle("Gender-specific vote for green topics") +
  theme(title = element_text(size = 16, face = 2, hjust = 0.5))

Explanation of the Code

Let’s break down the code above:

  • ggplot() creates a new plot object.
  • The data argument specifies the data frame that contains our vote data.
  • In the aes() function, we map the values in the y column to the size of each wedge in the pie chart. We also specify the color using the fill aesthetic. Since we’re plotting percentages, it makes sense to use a color palette with blue for women and red for men.
  • The geom_col() function creates a column chart, but since this is a pie chart, we don’t plot values on an x-axis. Instead, we plot them in a circular arrangement using the coord_polar() function.
  • We customize the appearance of our pie chart by adding colors and labels. The geom_text() function adds text labels to each wedge in the pie chart. We use the position_stack() function to position these labels on top of their corresponding wedges.

Conclusion

In this article, we explored how to create a pie chart in R with custom colors using the ggplot2 package. We created a data frame that contained vote data for men and women, loaded the required libraries, and customized our pie chart with colors and labels. By following these steps, you should be able to create your own pie charts with custom colors and labels.

Additional Resources

If you’re interested in learning more about ggplot2 or R programming in general, here are some additional resources:

Example Use Cases

Pie charts can be used in a variety of scenarios, such as:

  • Voting systems
  • Market research
  • Survey analysis

Here is an example use case where we might want to create a pie chart with custom colors:

Suppose you’re conducting a survey and want to analyze the results. You have data on how people voted for different candidates, and you want to visualize the results in a pie chart.

# Create a data frame with voting data
voting_data <- data.frame(
  candidate = c("Candidate A", "Candidate B", "Candidate C"),
  votes = c(100, 50, 20)
)

# Calculate the total number of votes
total_votes <- sum(votes)

# Calculate the percentage of each candidate's votes
candidate_votes <- voting_data %>%
  group_by(candidate) %>%
  summarise(votes = votes / total_votes * 100) %>%
  ungroup()

# Create a pie chart with custom colors
ggplot(data = candidate_votes, aes(x = 1, y = votes, fill = candidate)) +
  geom_col() +
  coord_polar(theta = "y") +
  theme_void() +
  geom_text(aes(label = paste0(round(votes, 2), "%")), colour = "white", 
            position = position_stack(vjust = 0.5), size = 6) +
  scale_fill_manual(values = c("blue", "red", "green")) +
  ggtitle("Voting Results") +
  theme(title = element_text(size = 16, face = 2, hjust = 0.5))

In this example, we created a pie chart with custom colors to visualize the results of our survey. The x-axis represents the candidates and the y-axis represents the percentage of votes each candidate received.


Last modified on 2025-03-18