An R Introduction to Statistics

Bar Graph

A bar graph of a qualitative data sample consists of vertical parallel bars that shows the frequency distribution graphically.

Example

In the data set painters, the bar graph of the School variable is a collection of vertical bars showing the number of painters in each school.

Problem

Find the bar graph of the painter schools in the data set painters.

Solution

We first apply the table function to compute the frequency distribution of the School variable.

> library(MASS)                 # load the MASS package 
> school = painters$School      # the painter schools 
> school.freq = table(school)   # apply the table function

Then we apply the barplot function to produce its bar graph.

> barplot(school.freq)         # apply the barplot function

Answer

The bar graph of the school variable is:

PIC

Enhanced Solution

To colorize the bar graph, we select a color palette and set it in the col argument of barplot.

> colors = c("red", "yellow", "green", "violet", 
+   "orange", "blue", "pink", "cyan") 
> barplot(school.freq,         # apply the barplot function 
+   col=colors)                # set the color palette

PIC

Exercise

Find the bar graph of the composition scores in painters.