Javatpoint Logo
Javatpoint Logo

R Pie Charts

R programming language has several libraries for creating charts and graphs. A pie-chart is a representation of values in the form of slices of a circle with different colors. Slices are labeled with a description, and the numbers corresponding to each slice are also shown in the chart. However, pie charts are not recommended in the R documentation, and their characteristics are limited. The authors recommend a bar or dot plot on a pie chart because people are able to measure length more accurately than volume.

The Pie charts are created with the help of pie () function, which takes positive numbers as vector input. Additional parameters are used to control labels, colors, titles, etc.

There is the following syntax of the pie() function:

Here,

  1. X is a vector that contains the numeric values used in the pie chart.
  2. Labels are used to give the description to the slices.
  3. Radius describes the radius of the pie chart.
  4. Main describes the title of the chart.
  5. Col defines the color palette.
  6. Clockwise is a logical value that indicates the clockwise or anti-clockwise direction in which slices are drawn.

Example

Output:

R Pie Charts

Title and color

A pie chart has several more features that we can use by adding more parameters to the pie() function. We can give a title to our pie chart by passing the main parameter. It tells the title of the pie chart to the pie() function. Apart from this, we can use a rainbow colour pallet while drawing the chart by passing the col parameter.

Note: The length of the pallet will be the same as the number of values that we have for the chart. So for that, we will use length() function.

Let's see an example to understand how these methods work in creating an attractive pie chart with title and color.

Example

Output:

R Pie Charts

Slice Percentage & Chart Legend

There are two additional properties of the pie chart, i.e., slice percentage and chart legend. We can show the data in the form of percentage as well as we can add legends to plots in R by using the legend() function. There is the following syntax of the legend() function.

Here,

  • x and y are the coordinates to be used to position the legend.
  • legend is the text of legend
  • fill is the color to use for filling the boxes beside the legend text.
  • col defines the color of line and points besides the legend text.
  • bg is the background color for the legend box.

Example

Output:

R Pie Charts

3 Dimensional Pie Chart

In R, we can also create a three-dimensional pie chart. For this purpose, R provides a plotrix package whose pie3D() function is used to create an attractive 3D pie chart. The parameters of pie3D() function remain same as pie() function. Let's see an example to understand how a 3D pie chart is created with the help of this function.

Example

Output:

R Pie Charts

Example

Output:

R Pie Charts
Next TopicR Bar Charts




Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA