7 Pie Chart

Sandeep Kumar ­
A free video tutorial from Sandeep Kumar ­
Experienced Quality Manager • Six Sigma Coach • Consultant
4.5 instructor rating • 25 courses • 147,011 students

Learn more from the full course

Mastering Data Visualization with R

Visualize data using R Base Graphics, Lattice Package and ggplot (GGPlot2) for data analysis and data science

06:12:09 of on-demand video • Updated March 2018

  • Understand what plots are suitable for a type of data you have
  • Understand the data before you make a plot
  • Visualize data by creating various graphs using R base package, lattice and ggplot2 packages
  • A case study to select a diamond - to explain ggplot()
English The next plot for one Continuous and one Discrete Variable is the Pie Chart. Pie chart shows these values in terms of pies in a circle. Let's take a different example here. Let's say I am making water bottles in a plant and I am noting down the types of defects, which we have. So what I did was there were four common types of defects, which were in these bottles. So one was the loose cap, one was the labels wrong, then third one was the volume difference, the fourth one was the scratches on the bottle. So let's assume that these are the four types of defects, which we have on the water bottle, and I noted down the frequencies of these as well. So when I looked that number of these bottles thousands of these bottles I found out that there are 9 bottles, which have loose caps, there are 15 bottles, which have labels problem. Then there are 42 bottles, which have volume problem, and then there are 29bottles, which have some sort of a scratch on the bottle. So what I did was I created a Vector, Vector called ‘defects’, and in this Vector, I put that number of defects in that. So with this if I press control enter a Vector has been created, the Vector named as ‘defects’. And now what I want to do is I want to name these values, so 9, 15, 42, 29, what are these values? So what I can do is I can assign names to that, so for that the command is ‘names’. And in that, I can put the name of that Vector. So the name for Vector items is Loose cap, Labels, Volumes, Scratches and I put this also as a vector here. So with this if I press control enter, now I have put the names to this. So now, how does my defects Vector look like, let's see that defects. Now defect vector looks like this where I have the names of these items loose cap 9, labels 15, volume 42, scratch 29. Now I want to make a Pie Chart of this and that’s very simple thing, the command for that is Pie. And in Pie and in the bracket, I can put this particular vector, which is defects, so let's put that. So Pie and in that, I want to put defects. And with this, if I press enter I get a Pie Chart. Pie chart with the different colours, which shows that loose cap, labels, so by looking at this particular pie chart, I can see that the biggest problem is Volume, the next biggest problem is Scratch, Labels, Loose caps. But many times, it's really not possible to have a clear difference in these pies. So now we have very different pies, the size of pies is quite different 9, 15, 42. If the difference is small, let's say if I have one value as 40 another value as 42. You really cannot do visually look at that. So that's the reason Pie Chart is not very nice tool to show data. Same thing I could have done using Bar Chart and if I have a choice I will go with the Bar Charts rather than going with the Pie Chart. So the same data if I have to plot, I could have used the Bar Plot which we used earlier, ‘barplot(defects), press control enter to plot that, see the difference here. This is much more visually clear that what problems we have compared to what we get in the Pie Chart. Now let's come back to Pie Chart because that's the topic of this lecture, so we have plotted this. So what I have done is here is another approach, in the previous approach what I did was I created a vector called defects and I named those items. Other option could be I create a Vector called defects and I create another Vector called ‘Labs’. First thing is let's create a Vector called defects control enter, so now Vector called defects, now defects doesn't have those names, so now if you look at defects, defects is just a numbers only. Now I create another Vector which is called as Labs, this has four values press control enter, so now I if I look at labs, labs is another Vector which has four values. Now if I want to draw a Pie Chart, so the command is Pie, and as I earlier told that once you give the command and if you are looking for the arguments in that particular command you can press tab. If you press tab it tells you what all it needs. It needs value of x. Ok so value of (x = defects ‘ ,’ then it's looking for labels. labels = labs) because we created a Vector call that. And if I press enter with this, I get the same Pie Chart, that's another way to do that. In addition to that, if you want to add colours to this you can use col, let's copy this one up to this factor control c, and now with the up arrow I bring down this previous command comma press tab and look at colours is equal to, I put this, close the bracket and enter that. So with this I can colour these Pies with a different colour or the colour of my choice. So this was the Pie Chart, even though I don't prefer that, but I still want to show that, that this is an option in base ‘R’ commands.