Categorical, or qualitative, data are pieces of information that allow us to classify the objects under investigation into various categories. We usually begin working with categorical data by summarizing the data into a **frequency table.**

### Frequency Table

A frequency table is a table with two columns. One column lists the categories, and another for the frequencies with which the items in the categories occur (how many items fit into each category).

### Example 1

An insurance company determines vehicle insurance premiums based on known risk factors. If a person is considered a higher risk, their premiums will be higher. One potential factor is the color of your car. The insurance company believes that people with some color cars are more likely to get in accidents. To research this, they examine police reports for recent total-loss collisions. The data is summarized in the frequency table below.

Color | Frequency |

Blue | 25 |

Green | 52 |

Red | 41 |

White | 36 |

Black | 39 |

Grey | 23 |

Sometimes we need an even more intuitive way of displaying data. This is where charts and graphs come in. There are many, many ways of displaying data graphically, but we will concentrate on one very useful type of graph called a bar graph. In this section we will work with bar graphs that display categorical data; the next section will be devoted to bar graphs that display quantitative data.

### Bar graph

A **bar graph** is a graph that displays a bar for each category with the length of each bar indicating the frequency of that category.

To construct a bar graph, we need to draw a vertical axis and a horizontal axis. The vertical direction will have a scale and measure the frequency of each category; the horizontal axis has no scale in this instance. The construction of a bar chart is most easily described by use of an example.

### Example 2

Using our car data from above, note the highest frequency is 52, so our vertical axis needs to go from 0 to 52, but we might as well use 0 to 55, so that we can put a hash mark every 5 units:

Notice that the height of each bar is determined by the frequency of the corresponding color. The horizontal gridlines are a nice touch, but not necessary. In practice, you will find it useful to draw bar graphs using graph paper, so the gridlines will already be in place, or using technology. Instead of gridlines, we might also list the frequencies at the top of each bar, like this:

In this case, our chart might benefit from being reordered from largest to smallest frequency values. This arrangement can make it easier to compare similar values in the chart, even without gridlines. When we arrange the categories in decreasing frequency order like this, it is called a **Pareto chart**.

### Pareto chart

A **Pareto chart** is a bar graph ordered from highest to lowest frequency

### Example 3

Transforming our bar graph from earlier into a Pareto chart, we get:

### Example 4

In a survey^{[1]}, adults were asked whether they personally worried about a variety of environmental concerns. The numbers (out of 1012 surveyed) who indicated that they worried “a great deal” about some selected concerns are summarized below.

Environmental Issue | Frequency |

Pollution of drinking water | 597 |

Contamination of soil and water by toxic waste | 526 |

Air pollution | 455 |

Global warming | 354 |

This data could be shown graphically in a bar graph:

To show relative sizes, it is common to use a pie chart.

### Pie Chart

A **pie chart** is a circle with wedges cut of varying sizes marked out like slices of pie or pizza. The relative sizes of the wedges correspond to the relative frequencies of the categories.

### Example 5

For our vehicle color data, a pie chart might look like this:

Pie charts can often benefit from including frequencies or relative frequencies (percents) in the chart next to the pie slices. Often having the category names next to the pie slices also makes the chart clearer.

### Example 6

The pie chart to the right shows the percentage of voters supporting each candidate running for a local senate seat.

If there are 20,000 voters in the district, the pie chart shows that about 11% of those, about 2,200 voters, support Reeves.

Pie charts look nice, but are harder to draw by hand than bar charts since to draw them accurately we would need to compute the angle each wedge cuts out of the circle, then measure the angle with a protractor. Computers are much better suited to drawing pie charts. Common software programs like Microsoft Word or Excel, OpenOffice.org Write or Calc, or Google Docs are able to create bar graphs, pie charts, and other graph types. There are also numerous online tools that can create graphs.^{[2]}

### Try it Now 1

Create a bar graph and a pie chart to illustrate the grades on a history exam below.

A: 12 students, B: 19 students, C: 14 students, D: 4 students, F: 5 students

Don’t get fancy with graphs! People sometimes add features to graphs that don’t help to convey their information. For example, 3-dimensional bar charts like the one shown below are usually not as effective as their two-dimensional counterparts.

Here is another way that fanciness can lead to trouble. Instead of plain bars, it is tempting to substitute meaningful images. This type of graph is called a **pictogram**.

### Pictogram

A **pictogram** is a statistical graphic in which the size of the picture is intended to represent the frequencies or size of the values being represented.

### Example 7

A labor union might produce the graph to the right to show the difference between the average manager salary and the average worker salary.

Looking at the picture, it would be reasonable to guess that the manager salaries is 4 times as large as the worker salaries – the area of the bag looks about 4 times as large. However, the manager salaries are in fact only twice as large as worker salaries, which were reflected in the picture by making the manager bag twice as tall.

Another distortion in bar charts results from setting the baseline to a value other than zero. The baseline is the bottom of the vertical axis, representing the least number of cases that could have occurred in a category. Normally, this number should be zero.

### Example 8

Compare the two graphs below showing support for same-sex marriage rights from a poll taken in December 2008^{[3]}. The difference in the vertical scale on the first graph suggests a different story than the true differences in percentages; the second graph makes it look like twice as many people oppose marriage rights as support it.

### Try it Now 2

A poll was taken asking people if they agreed with the positions of the 4 candidates for a county office. Does the pie chart present a good representation of this data? Explain.

## Chapter 7 Relationships with Categorical Variables

We are familiar with categorical variables. They have a nominal or ordinal level of measurement, and can be binary – have only two possible values. In R , these variables are encoded as a factor class, and for shorthand, are referred to as *factors*. (See Lesson 2, section 2.4.1.1 .)

In inferential statistics, sometimes we want to make predictions about relationships with factors. For example, we want to understand the relationship between sex and fear of crime. Perhaps we think males will have lower levels of fear of crime than females would have. The variable, sex, in this case, is a binary factor with the two categories, male and female, whereas fear of crime is a numeric variable.

Today, we learn how to conduct more inferential statistical analyses, but specifically with categorical variables.

### 7.1.1 Activity 1: Our R prep routine

Before we start, we do the following:

Open up our existing R project

Install and load the required packages listed above with exception to vcdExtra the reason is vcdExtra will interfere with the package dplyr (see Lesson 1, section 1.3.3 on masking):

Download from Blackboard and load into R the British Crime Survey (BCS) dataset (*bcs_2007_8_teaching_data_unrestricted.dta* ). Be mindful of its data format because it will require certain codes and packages. Name this data frame BCS0708 .

Get to know the data with the View() function.

## The Attach Command

If we don't want to keep using the "

## Summary Function

There is also a summary function that gives a number of summaries on a numeric variable (or even the whole data frame!) in a nice vector format:

> summary(airquality$Ozone) **#note we don't need "na.rm" here**

Min. 1st Qu. Median Mean 3rd Qu. Max. NA's

1.00 18.00 31.50 42.13 63.25 168.00 37.00

Ozone Solar.R Wind Temp Month Day

Min. : 1.00 Min. : 7.0 Min. : 1.700 Min. :56.00 Min. :5.000 Min. : 1.00

1st Qu.: 18.00 1st Qu.:115.8 1st Qu.: 7.400 1st Qu.:72.00 1st Qu.:6.000 1st Qu.: 8.00

Median : 31.50 Median :205.0 Median : 9.700 Median :79.00 Median :7.000 Median :16.00

Mean : 42.13 Mean :185.9 Mean : 9.958 Mean :77.88 Mean :6.993 Mean :15.80

3rd Qu.: 63.25 3rd Qu.:258.8 3rd Qu.:11.500 3rd Qu.:85.00 3rd Qu.:8.000 3rd Qu.:23.00

Max. :168.00 Max. :334.0 Max. :20.700 Max. :97.00 Max. :9.000 Max. :31.00

Notice that "Month" and "Day" are coded as numeric variables even though they are clearly categorical. This can be mended as follows, e.g.:

Ozone Solar.R Wind Temp Month Day

Min. : 1.00 Min. : 7.0 Min. : 1.700 Min. :56.00 5:31 Min. : 1.00

1st Qu.: 18.00 1st Qu.:115.8 1st Qu.: 7.400 1st Qu.:72.00 6:30 1st Qu.: 8.00

Median : 31.50 Median :205.0 Median : 9.700 Median :79.00 7:31 Median :16.00

Mean : 42.13 Mean :185.9 Mean : 9.958 Mean :77.88 8:31 Mean :15.80

3rd Qu.: 63.25 3rd Qu.:258.8 3rd Qu.:11.500 3rd Qu.:85.00 9:30 3rd Qu.:23.00

Max. :168.00 Max. :334.0 Max. :20.700 Max. :97.00 Max. :31.00

Notice how the display changes for the factor variables.

Find the standard deviations (SDs) of all the numeric variables in the air quality data set, using the apply function.

**Summary Statistics in R: Mean, Standard Deviation, Frequencies, etc (R Tutorial 2.7) MarinStatsLectures** [Contents]

**attach**command to keep the data set as the current or working one in R, and then just call the variables by name. For example, the above can then be accomplished by:

Once we are finished working with this data set, we can use the detach() command to remove this data set from the working memory.

Never attach two data sets that share the same variable names- this could lead to confusion and errors! A good idea is to detach a data set as soon as you have finished working with it.

For now, let's keep this data set attached, while we test out some other functions.

By default you get the minimum, the maximum, and the three quartiles — the 0.25, 0.50, and 0.75 quantiles. The difference between the first and third quartiles is called the ** interquartile range** (IQR) and is sometimes used as an alternative to the standard deviation.

It is also possible to obtain other quantiles this is done by adding an argument containing the desired percentage cut points. To get the ** deciles,** use the

**sequence function:**

> pvec <- seq(0,1,0.1) #sequence of digits from 0 to 1, by 0.1

[1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0

0% 10% 20% 30% 40% 50% 60% 70% 80% 90% 100%

56.0 64.2 69.0 74.0 76.8 79.0 81.0 83.0 86.0 90.0 97.0

How would you use this method to get quintiles? Answer

## 9.1: Presenting Categorical Data Graphically

Quantitative, or numerical, data can also be summarized into frequency tables.

### Example

A teacher records scores on a 20-point quiz for the 30 students in his class. The scores are:

19 20 18 18 17 18 19 17 20 18 20 16 20 15 17 12 18 19 18 19 17 20 18 16 15 18 20 5 0 0

These scores could be summarized into a frequency table by grouping like values:

Score | Frequency |

0 | 2 |

5 | 1 |

12 | 1 |

15 | 2 |

16 | 2 |

17 | 4 |

18 | 8 |

19 | 4 |

20 | 6 |

Using the table from the first example, it would be possible to create a standard bar chart from this summary, like we did for categorical data:

However, since the scores are numerical values, this chart doesn’t really make sense the first and second bars are five values apart, while the later bars are only one value apart. It would be more correct to treat the horizontal axis as a number line. This type of graph is called a **histogram**.

### Histogram

A histogram is like a bar graph, but where the horizontal axis is a number line.

### Example

For the values above, a histogram would look like:

Notice that in the histogram, a bar represents values on the horizontal axis from that on the left hand-side of the bar up to, but not including, the value on the right hand side of the bar. Some people choose to have bars start at ½ values to avoid this ambiguity.

This video demonstrates the creation of the histogram from this data.

Unfortunately, not a lot of common software packages can correctly graph a histogram. About the best you can do in Excel or Word is a bar graph with no gap between the bars and spacing added to simulate a numerical horizontal axis.

If we have a large number of widely varying data values, creating a frequency table that lists every possible value as a category would lead to an exceptionally long frequency table, and probably would not reveal any patterns. For this reason, it is common with quantitative data to group data into **class intervals**.

### Class Intervals

Class intervals are groupings of the data. In general, we define class intervals so that

- each interval is equal in size. For example, if the first class contains values from 120-129, the second class should include values from 130-139.
- we have somewhere between 5 and 20 classes, typically, depending upon the number of data we’re working with.

### Example

Suppose that we have collected weights from 100 male subjects as part of a nutrition study. For our weight data, we have values ranging from a low of 121 pounds to a high of 263 pounds, giving a total span of 263-121 = 142. We could create 7 intervals with a width of around 20, 14 intervals with a width of around 10, or somewhere in between. Often time we have to experiment with a few possibilities to find something that represents the data well. Let us try using an interval width of 15. We could start at 121, or at 120 since it is a nice round number.

Interval | Frequency |

120 – 134 | 4 |

135 – 149 | 14 |

150 – 164 | 16 |

165 – 179 | 28 |

180 – 194 | 12 |

195 – 209 | 8 |

210 – 224 | 7 |

225 – 239 | 6 |

240 – 254 | 2 |

255 – 269 | 3 |

A histogram of this data would look like:

In many software packages, you can create a graph similar to a histogram by putting the class intervals as the labels on a bar chart.

The following video walks through this example in more detail.

### Try It

Other graph types such as pie charts are possible for quantitative data. The usefulness of different graph types will vary depending upon the number of intervals and the type of data being represented. For example, a pie chart of our weight data is difficult to read because of the quantity of intervals we used.

To see more about why a pie chart isn’t useful in this case, watch the following.

### Try It

The total cost of textbooks for the term was collected from 36 students. Create a histogram for this data.

$140 $160 $160 $165 $180 $220 $235 $240 $250 $260 $280 $285

$285 $285 $290 $300 $300 $305 $310 $310 $315 $315 $320 $320

$330 $340 $345 $350 $355 $360 $360 $380 $395 $420 $460 $460

When collecting data to compare two groups, it is desirable to create a graph that compares quantities.

### Example

The data below came from a task in which the goal is to move a computer mouse to a target on the screen as fast as possible. On 20 of the trials, the target was a small rectangle on the other 20, the target was a large rectangle. Time to reach the target was recorded on each trial.

One option to represent this data would be a comparative histogram or bar chart, in which bars for the small target group and large target group are placed next to each other.

### Frequency polygon

An alternative representation is a **frequency polygon**. A frequency polygon starts out like a histogram, but instead of drawing a bar, a point is placed in the midpoint of each interval at height equal to the frequency. Typically the points are connected with straight lines to emphasize the distribution of the data.

### Example

This graph makes it easier to see that reaction times were generally shorter for the larger target, and that the reaction times for the smaller target were more spread out.

The following video explains frequency polygon creation for this example.

## Histograms for Ordinal Variables

A distinguishing feature of bar charts for dichotomous and non-ordered categorical variables is that the bars are separated by spaces to emphasize that they describe non-ordered categories. When one is dealing with ordinal variables, however, the appropriate graphical format is a **histogram**. A histogram is similar to a bar chart, except that the adjacent bars abut one another in order to reinforce the idea that the categories have an inherent order. The frequency histogram below summarizes the blood pressure data that was presented in a tabular format in Table 4 on the previous page. Note that the vertical axis displays the frequencies or numbers of participants classified in each category.

**Figure 7 Frequency Histogram for Blood Pressure**

This histogram immediately conveys the message that the majority of participants are in the lower two categories of the distribution. A small number of participants are in the Stage II hypertension category. The histogram below is a relative frequency histogram for the same data. Note that the figure is the same, except for the vertical axis, which is scaled to accommodate relative frequencies instead of frequencies.

**Figure 8 - Relative Frequency Histogram for Blood Pressure**

Content �. All Rights Reserved.

Date last modified: May 17, 2016.

Created by Lisa Sullivan, PhD and Wayne W. LaMorte, MD, PhD, MPH,

## Representing Data Graphically: 3 Methods | Statistics

The following points highlight the three main techniques and methods used for representing data graphically. The methods are: 1. Histogram 2. Bar Chart 3. Pie Diagrams.

#### Method # 1. Histogram:

Graphically, one of the most important and useful methods of presenting the frequency distribution of both continuous and discontinuous types is called histogram. In a histogram, the class-boundaries are located on the horizontal axis of a graph paper and vertical rectangles are erected side by side over these class intervals in such a way that the area of any rectangle drawn is proportional to the frequency in that particular class.

#### Method # 2. Bar Chart:

**The following table shows the size-distribution of multi-member households of a particular year:**

**The above data is shown below in the form of a bar diagram.**

**The following table contains the data relating to plan provision and actual outlay by the government on the heads of expenditure for a particular year:**

The above sets of data can be presented by placing two or more bars representing the amount to be compared in juxta position.

Similarly, there can be bar charts of different forms and shapes, including the component part bar diagram for a given data. In a component part bar diagram, there is only one bar showing the different components of the data vertically.

#### Method # 3. Pie Diagrams:

The data relating to the size of the family given above can also be presented by cutting a circle into sectors in such a manner that these sectors have areas or central angles proportional to the different quantities that have to be compared.

## Scatter Plot

Scatter Plots are used to evaluate the relationship between two different continuous variables. These graphs compare changes in two different variables at once. For example, you could look at the relationship between height and weight. Both height and weight are continuous variables. You could not use a scatter plot to look at the relationship between number of children in a family and weight of each child because the number of children in a family is not a continuous variable: you can’t have 2.3 children in a family.

Figure 3: The relationship between height (in meters) and weight (in kilograms) of members of the girls softball team. “OLS example weight vs height scatterplot” by Stpasha is in the Public Domain

## How to create cool charts and graphs?

The short answer – with the right software tools.

In our high-tech era, there is a wide variety of premium or free graphing software tools that allow you to create amazing graphs and charts in minutes. They are interesting, visually-appealing, and easy to understand.

Here is a list of the most popular of them:

#### About The Author

##### Silvia Valcheva

Silvia Valcheva is a digital marketer with over a decade of experience creating content for the tech industry. She has a strong passion for writing about emerging software and technologies such as big data, AI (Artificial Intelligence), IoT (Internet of Things), process automation, etc.

### Leave a Reply Cancel Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

## 9.1: Presenting Categorical Data Graphically

Quantitative, or numerical, data can also be summarized into frequency tables.

### Example 9

A teacher records scores on a 20-point quiz for the 30 students in his class. The scores are:

19 20 18 18 17 18 19 17 20 18 20 16 20 15 17 12 18 19 18 19 17 20 18 16 15 18 20 5 0 0

These scores could be summarized into a frequency table by grouping like values:

Score | Frequency |

0 | 2 |

5 | 1 |

12 | 1 |

15 | 2 |

16 | 2 |

17 | 4 |

18 | 8 |

19 | 4 |

20 | 6 |

Using this table, it would be possible to create a standard bar chart from this summary, like we did for categorical data:

However, since the scores are numerical values, this chart doesn’t really make sense the first and second bars are five values apart, while the later bars are only one value apart. It would be more correct to treat the horizontal axis as a number line. This type of graph is called a **histogram**.

### Histogram

A histogram is like a bar graph, but where the horizontal axis is a number line

### Example 10

For the values above, a histogram would look like:

Notice that in the histogram, a bar represents values on the horizontal axis from that on the left hand-side of the bar up to, but not including, the value on the right hand side of the bar. Some people choose to have bars start at ½ values to avoid this ambiguity.

Unfortunately, not a lot of common software packages can correctly graph a histogram. About the best you can do in Excel or Word is a bar graph with no gap between the bars and spacing added to simulate a numerical horizontal axis.

If we have a large number of widely varying data values, creating a frequency table that lists every possible value as a category would lead to an exceptionally long frequency table, and probably would not reveal any patterns. For this reason, it is common with quantitative data to group data into **class intervals**.

### Class Intervals

Class intervals are groupings of the data. In general, we define class intervals so that:

- Each interval is equal in size. For example, if the first class contains values from 120-129, the second class should include values from 130-139.
- We have somewhere between 5 and 20 classes, typically, depending upon the number of data we’re working with.

### Example 11

Suppose that we have collected weights from 100 male subjects as part of a nutrition study. For our weight data, we have values ranging from a low of 121 pounds to a high of 263 pounds, giving a total span of 263-121 = 142. We could create 7 intervals with a width of around 20, 14 intervals with a width of around 10, or somewhere in between. Often time we have to experiment with a few possibilities to find something that represents the data well. Let us try using an interval width of 15. We could start at 121, or at 120 since it is a nice round number.

Interval | Frequency |

120 – 134 | 4 |

135 – 149 | 14 |

150 – 164 | 16 |

165 – 179 | 28 |

180 – 194 | 12 |

195 – 209 | 8 |

210 – 224 | 7 |

225 – 239 | 6 |

240 – 254 | 2 |

255 – 269 | 3 |

A histogram of this data would look like:

In many software packages, you can create a graph similar to a histogram by putting the class intervals as the labels on a bar chart.

Other graph types such as pie charts are possible for quantitative data. The usefulness of different graph types will vary depending upon the number of intervals and the type of data being represented. For example, a pie chart of our weight data is difficult to read because of the quantity of intervals we used.

### Try it Now 3

The total cost of textbooks for the term was collected from 36 students. Create a histogram for this data.

$140 $160 $160 $165 $180 $220 $235 $240 $250 $260 $280 $285

$285 $285 $290 $300 $300 $305 $310 $310 $315 $315 $320 $320

$330 $340 $345 $350 $355 $360 $360 $380 $395 $420 $460 $460

When collecting data to compare two groups, it is desirable to create a graph that compares quantities.

### Example 12

The data below came from a task in which the goal is to move a computer mouse to a target on the screen as fast as possible. On 20 of the trials, the target was a small rectangle on the other 20, the target was a large rectangle. Time to reach the target was recorded on each trial.

One option to represent this data would be a comparative histogram or bar chart, in which bars for the small target group and large target group are placed next to each other.

### Frequency polygon

An alternative representation is a **frequency polygon**. A frequency polygon starts out like a histogram, but instead of drawing a bar, a point is placed in the midpoint of each interval at height equal to the frequency. Typically the points are connected with straight lines to emphasize the distribution of the data.

### Example 13

This graph makes it easier to see that reaction times were generally shorter for the larger target, and that the reaction times for the smaller target were more spread out.

When a graph is distorted, it can quickly deceive the reader into thinking something other than what the data really says. There are several ways that graphs can be distorted.

Probably the most common way that graphs get distorted is when the distance along the vertical or horizontal axis is altered in relation to the other axis. Axes can be stretched or shrunk to create any desired result. For example, if you were to shrink the horizontal axis (X axis), it could make the slope of your line graph appear steeper than it actually is, giving the impression that the results are more dramatic than they are. Likewise, if you expanded the horizontal axis while keeping the vertical axis (Y axis) the same, the slope of the line graph would be more gradual, making the results appear less significant than they really are.

When creating and editing graphs, it is important to make sure the graphs do not get distorted. Oftentimes, it can happen by accident when editing the range of numbers in an axis, for example. Therefore it is important to pay attention to how the data comes across in the graphs and make sure the results are being presented accurately and appropriately, so as to not deceive the readers.