STAT 200 – Introductory Statistics
December 24, 2025
After today’s lecture, you should:
create the following univariate graphics for categorical variables:
create the following bivariate graphics for categorical variables:
determine which graphic(s) are appropriate to tell the story of your data
interpret the presented graphics of others
modify graphics to make them presentation-worthy
To read into R the data we will use for this lecture, run the following code:
This will read the dataset in from Dr. Forsberg’s website.
If the .csv file is on your computer, use read.csv("file path"), i.e.
read.csv reads the .csv file into as a data frame.
There are three main packages to use while plotting in R:
base (This is the built-in package in R.)gridggplotThey all use a different way of thinking about how graphics should be created. grid and ggplot are packages that can be added to base R, and can make prettier plots.
We will discuss the base R functions in this lecture.
How should we think about our process for creating plots?
Some basic univariate categorical graphics consist of:
Some basic bivariate categorical graphics consist of:
You’re all familiar with pie charts. It is a circle split into segments of the size of the proportion of the sample in each category.
Be careful with pie charts. They can be hard to read and easy to misuse.
The function in R to make a pie chart is pie(table(var)).
The table function returns the count of each element in a vector.
You know bar charts as well. It is a graph with bars whose height represents the frequency of each category.
Often bar charts are used over pie charts for readability. Human beings are much better at comparing the areas of squares than circles or sectors of a circle.
The function in R to make a bar chart is barplot(table(var)).
The previous two kinds of charts help break down the distribution of a single variable.
What if we want to compare the distribution of two variables together?
A side-by-side bar chart essentially makes a bar chart of one variable for each category of the other variable.
The function in R to make a side-by-side bar chart is barplot(table(var1, var2), beside=TRUE).
A side-by-side bar chart makes a bar chart of one variable and colors in the segments of those bars according to the distribution of the other variable.
The function in R to make a side-by-side bar chart is barplot(table(var1, var2)).
A spine chart is a stacked bar chart where instead of the height of the bar reflecting the frequency, but the width.
The function in R to make a side-by-side bar chart is spineplot(table(var1, var2)).
A mosaic chart does the same as the spine chart, but has seperate boxes for the categories in the bars.
The function in R to make a side-by-side bar chart is mosaicplot(table(var1, var2)).
What we’ve done so far is the default settings of each plot type. We can customize our plots more, if we want to.
Here are some things to think about before starting your graphic:
Note: It is very helpful to search the Internet for ideas for your graphic. There are many, many resources available.
Now that you have thought about what your graphic will look like (the planning), let’s formally set the parameters of the graphic.
This code sets the margins to be 4 lines at the bottom, 4 at the left, 1 at the top, and 1 at the right:
This code requires the axes to meet at \((0, 0)\):
This code specifies the labels are 20% larger and values are 20% smaller than the base size:
The following code starts the graphic and sets the viewing window to (0, 10) × (0, 100).
This will start a generic window. Some graphing functions have their own “starting” functions. Those we are covering in this section do. So, the above two lines are not needed for those functions.
To create a bar chart with some interesting colors, let’s use this code:
R color cheat sheet: https://www.nceas.ucsb.edu/sites/default/files/2020-04/colorPaletteCheatsheet.pdf
The following code cleans up the graphic by adding label for the variables and adds a base line.
Here’s our fancy new bar chart after running all that code: