Pie Charts
Learn how to generate pie charts and explore their best use cases.
We'll cover the following...
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.
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 different products in a store.
# We use a custom dataset in this exercisedata <- 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 ...