5 Data Visualization in Base R

As was indicated by the title of this section, none of the functions in this section of the document require any external packages in order to be run.

We will begin this section by creating the data set that we will be working with. This data set will consist of a sample of 100 undergraduate students’ math and reading test scores. The test scores are on a scale of 0 to 100. Each individual has also been assigned to either the Paper Test format or the Electronic Test format in the TestFormat condition and either the Classroom setting or Home setting in the TestLocation condition. Using this data we will explore scatter plots, bar graphs, histograms, and box plots. The following lines of code create the data set and set up the data frames we will need:

5.1 Scatter Plot

The first data visualization we will be working with is the scatter plot.

The following line of code creates a basic scatter plot using the plot() function.

This is a very basic plot, and it is lacking a lot of the important details that most visualizations include. To make this a little nicer, lets begin with adding a title.

We can also add a subtitle to the graph as well.

Another adjustment we may want to make to the graph is changing the axis labels.

These can also be left blank by just putting xlab="" and lab="".

It may also be a good idea to change the axis ranges as well.

Depending on how you intend on using the graph, you may also decide to remove the border around it.

And maybe the axis scales as well.

Now, using everything we have gone over so far in context.

We can also plot our data based on the different groups we created earlier. The following four plots show the math vs reading scartter plots for each of our four marginal groups: Paper Test, Electronc Test, Classroom location, and Home location.

We can plot the opposing graphs (Paper and Electronc; Classroom and Home) side by side by setting the number of plots on the screen using the following code:

The first number sets the number of rows of graphs to be displayed and the second sets the number of columns.

Once you have set the number of graphs you want to appear at a time, you can create the graphs.

Running either of the next two lines resets the plots back to one per screen.

## null device 
##           1

We can also plot different conditions on the same graph using the points function col= can be used to change the points colors and pch= can be used to change their shapes.

Now that we are including multiple conditions on one plot, we might want to add a legend so we can ideantify which groups the colors and/or shapes indicate. This can be done using the legend function. The legend’s position can be changed by writing different locations such as: topleft, topright, bottomleft, and bottomright.

Finally, we can plot our four different condition combinations as seperate graphs.

Or all on one graph with different point colors and shapes.

We can also add lines and text to any plot, which we will explore in the Histogram section.

5.2 Bar Graph

The next data visualization we will play with is the bar graph. However, before we can begin working with the barplot function and its arguments, we have to calculate the group means and create a new variable containing the group names to use when labeling our bars within the graphs. We will only be using the means for the math grades for these examples, but the same rules apply for the reading grade means as well.

The following line of code creates a basic bar graph using the barplot() function.

The default bar color is just white. We can change this using the col= arguement.

We should also add some labels to make it more clear what our graph is displaying.

Sometimes a legend might also help. We have added one to the next graphy to help shorten the condition names.

Unfortunately our new legend is covering some of our bar visuals. This can be fixed by changing the display window size or the y-axis limits.

In some situations, you may want to change the orientation of the bar labels. Using the las= aregument you can keep the horizontal with las=1 or make them vertical with las=2.

Finally we can change the spacing of the bars to make them closer together. Each of the numbers indicates how much space should be between that bar and the one to its right. The value of the first number does not impact the spacing because there is no bar to the left of the first bar. The spacing also increases very quickly, so begin with values below 1 and increase them by 0.1 units at a time.

5.3 Histogram

Next we will work with the hist() function and its arguments to create histograms.

Here is a basic histogram of the math grades.

Just as we did in the bar graphs, we can also color in the bars and change the colors of the boarder around the bars.

Similarly, like all other plots we can also add a title and labels.

And we can add limits to the two axes.

For any of the plots we create, including the bar and scatter plots, we have the option to add lines and text using the abline() and text() functions, respectively. For example here I can add lines on the x and y axis and some text.

We can also change the color and size of the text by adding the col= and cex= arguments.

Once lines and text have been added to a plot, they cannot be removed. Luckily, simply rerunning the function to create the original plot will create a new clean version.

5.4 Box Plot

The final data visualization we will cover in this section is the box plot. We can make box plots for the math grades and reading grades using the boxplot() function.

By including both data sets, we can plot both grade distributions at once.

We can also give the plot a title using main=.

Each of the groups can also be given a label.

You can also notch the plots around their medians. The notches provide a rough guide for determining if there is a significance of difference of medians.

The box plot function can also remove points which it has indicated as outliers.

Just like the box plots, we can add color to the box plots as well.

Finally, box plots also provide the option to turn the graph horizontally.