Pie Charts

Learn how to generate pie charts and explore their best use cases.

Features

A pie chart is a circular statistical graphic, which is divided into slices to illustrate numerical proportions. Each slice in a pie chart represents a proportion or percentage of the whole. We use pie charts to visualize the relative sizes of different categories, such as market shares, survey results, or budget allocations.

For example, a pie chart is a good option to represent the time we spend on different daily activities. We can show the share of each activity in a day, such as sleeping, eating, studying, and working out.

Press + to interact
Pie chart
Pie chart

Pie chart with pie()

We use the pie() built-in function to create pie charts in R. It takes numerical data and labels as input. We can use vectors to provide the data in pie()

pie(<data>, labels = c(<'label1'>,<'label2'>,...)

Here is a simple pie chart showing the monthly sale volumes of 55 different products in a store.

Press + to interact
# We use a custom dataset in this exercise
data <- c(1500, 1223, 1532,1932,550)
products <- c('Product A', 'Product B', 'Product C', 'Product D', 'Product E')
pie(data , labels = products)
  • Line 2: We create a custom data container representing the sales of the store.
  • Line 3: We create a container for the category names.
  • Line 4: We use the containers in the pie() function to generate the visualization.

Please notice that the pie() function assigns a distinct ...