Introduction to Pie Charts in R
Pie Chart in R is one of the basic chart features which are represented in the circular chart symbol. The section of the circle shows the data value proportions. The sections of the pie chart can be labeled with meaningful names. Pie charts are generally preferred for small-size vector variables. Pie charts can be of two-dimensional view or three-dimensional views based upon the R packages. Pie is the function in R language which is supporting two-dimensional pie charts. Pie charts are very useful for data analysis. Pie charts in R can be assigned with a meaning title using main as a parameter in the pie function.
Using the pie charts, patterns in the data can be understood easily, whereas if we go through the numeric figure, often understanding takes a while. For example, if we plot the above example as a pie chart, we can understand the amount of production and proportion of production within a minute.
There are various packages for plotting pie charts in R, and among those many options, we shall focus on two methods in this article.
Syntax
The above section provided a brief idea of the pie chart and its use. In this section, we shall learn about pie charts in R specifically. For those who are new to R, it is a programming language mainly used for data analysis and machine learning. R is quite rich in its functionality and provides hundreds of libraries for various use cases.
In R, it can be created by using a simple in-built function, and the syntax for the same is given below.
pie(x, labels, radius, main, col, clockwise)
Where,
- x is called a vector, and it contains the numeric values which are to be used in the pie chart, such as those production figures in the above example.
- labels take a name for those values in X, such as the name of chemicals.
- The radius argument is for the radius of the circle of the pie chart. Its typical value lies between −1 and +1.
- The main argument can be used to provide the title of the chart.
- col argument can be used to provide the colors to the chart.
- clockwise is a logical value that takes either True or False, indicating if the slices of charts are drawn in a clockwise or anti-clockwise manner.
How to create a pie chart in R?
Now that we understood the syntax of the pie chart as well let’s build a pie chart. For this, we will again use the same example in the introduction section above.
First of all, let’s convert the example above in the form of a table for easy understanding.
Name of chemical | Amount produced (in MT) |
AB1 | 90 |
AB2 | 50 |
AB3 | 100 |
AB4 | 40 |
AB5 | 20 |
Total | 300 |
First, we use the following two lines of R code to convert the table above into two vectors, one for the name of the chemical and the other for the volume of the chemical.
Now, we plot a simple pie chart by only providing the x value in the syntax above:
- chem <- c(“AB1″,”AB2″,”AB3″,”AB4″,”AB5”)
- vol <- c(90,50,100,40,20)
- pie(vol)
Its output is the figure below:
If you observe the output, it is not very clear as to what is represented by what. So to make it more intuitive, we input a few more arguments in the pie function and run again.
- chem <- c(“AB1″,”AB2″,”AB3″,”AB4″,”AB5”)
- vol <- c(90,50,100,40,20)
- pie(x=vol, labels = chem, radius = 1,main = “Pie chart for chemical production”, clockwise = T)
This picture is better to understand as it contains the name of the chemicals as well as a title. Please note the color scheme in both of these charts is coming by default which we can change as per our need or wish. We will do that in the section below.
How to change pie charts and fill color?
In this section, let’s learn how can be a change pie chart.
First, let’s show the number of chemicals in the chart instead of the name of chemicals.
- pie(x=vol, labels = vol, radius = 1,main = “Pie chart for chemical production”, clockwise = T)
- Run it yourself and see the output.
Next, lets change the color of the charts.
- chem <- c(“AB1″,”AB2″,”AB3″,”AB4″,”AB5”)
- vol <- c(90,50,100,40,20)
- pie(x=vol, labels = chem, radius = 1,main = “Pie chart for chemical production”, col=c(“red”,”blue”,”green”,”black”,”yellow”),clockwise = T)
Here we specified the colors that we want. The output is as below:
How to create a 3D pie chart?
In this section, we will learn how to build a 3D pie chart in R. for building a 3d pie chart; we need to install a library first as it can not be done from a basic inbuilt function.
You should install the library plotrix before running the code for the pie chart. To install the library, simply run the following command in R.
- Install.packages(“plotrix”)
After that, run the following two lines to get a 3d plot.
- chem <- c(“AB1″,”AB2″,”AB3″,”AB4″,”AB5”)
- vol <- c(90,50,100,40,20)
- library(plotrix)
- pie3D(vol,labels = chem,explode = 0.1, main = “Pie Chart for chemicals “)
The output is as below:
Conclusion
Pie charts are used a lot, and it is very intuitive and informative, which I believe is very clear by now. In financial domains and many other sectors too, pie charts are the basic visualization where almost all analysis begins. It is simple yet very powerful. In this article, we provided enough details which should help anyone start with building pie charts with great confidence and ease. Students and learners are also advised to look into the help menu of R, where they can learn more details and additional functionalities of pie charts. There is a great visualization package called ggplot2 in R, which provides many customization options to pie charts and all other visualization in general; candidates are advised to look into that as well. Finally, if there is any question or further doubt, you can always comment on this article and get in touch for more explanations, examples as well as theoretical discussions.
Recommended Articles
This has been a guide to Pie charts in R. Here; we discussed how to create a pie chart, How to change the pie chart and fill color, and How to create a 3D pie chart. You may also look at the following articles to learn more –
13 Online Courses | 20 Hands-on Projects | 120+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses