Using Pie Chart to display data

The problem with pie chart is that they force us to compare areas (or angles), which is pretty hard. As you can see in the following graph, how hard it is to visualize and compare categorical data.

table(mtcars$cyl)
## 
##  4  6  8 
## 11  7 14
pie(table(mtcars$cyl), 
    col=rainbow(length(mtcars$cyl)), 
    main="Number of cars by Cyliders")

Using Bar Plot to display data

Bar chart is a better option for categorical data as it lets us compare the different objects by their length, which is one dimensional. As comparing objects along one dimension is a lot easier, it makes comparing the length of bars a lot easier than the areas of pie slices. Below is a plot of same data using bar chart:

ggplot(mtcars, aes(x = factor(cyl))) + 
  geom_bar(fill="#0080c4") + ggtitle("Number of cars by Cyliders") + 
  xlab("Cyliders") + ylab("Number of Cars")

Using Histogram to display data

Histogram takes continuous variable where the data is split up into bins and the frequency of those bins is displayed. The major difference between the bar chart and histogram is the former uses nominal data sets to plot while histogram plots the continuous data sets.

A histogram allows to plot frequency distribution of a data set and offers an at a glance picture of a distribution pattern, charted in specific categories. A single glance at a histogram gives us some idea about the shape and spread of the data.

mtcars$mpg
##  [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
## [16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
## [31] 15.0 21.4
ggplot(mtcars, aes(x = mpg)) + geom_histogram(binwidth = 5, color="black", fill="#0080c4") + 
  ggtitle("Histogram to show MPG in Cars dataset") + 
  xlab("Miles per gallon")