R Pie Chart using pie() function - r - learn r - r programming
- In this article, you'll learn to create pie chart in R using the pie() function. You'll also learn to label them and color them.
- R Programming language has numerous libraries to create charts and graphs.
- A pie-chart is a representation of values as slices of a circle with different colors.
- The slices are labeled and the numbers corresponding to each slice is also represented in the chart.
- In R the pie chart is created using the pie() function which takes positive numbers as a vector input.
- The additional parameters are used to control labels, color, title etc.
- This function takes in a vector of non-negative numbers.
- Let us consider the above data represents the monthly expenditure breakdown of an individual.
Example: Simple pie chart using pie()
- Now let us draw a simple pie chart out of this data using the pie() function.
- We can see above that a pie chart was plotted with 5 slices.
- The chart was drawn in anti-clockwise direction using pastel colors.
- We can pass in additional parameters to affect the way pie chart is drawn.
- Some of the frequently used ones are, labels-to give names to slices, main-to add a title, col-to define colors for the slices and border-to color the borders.
- We can also pass the argument clockwise=TRUE to draw the chart in clockwise fashion.
Example 2: Pie chart with additional parameters
- As seen in the above figure, we have used the actual amount as labels.
- Also, the chart is drawn in clockwise fashion.
- Since the human eye is relatively bad at judging angles, other types of charts are appropriate than pie charts.
- A bar chart or dot chart is a preferable way of displaying this type of data.