Introduction

It is often very useful to visualize data frames rather than simply observe a table. Visualizations can make subtle relationships evident, and those relationships could be missed by just looking at a data table. Earlier tutorials described visualization techniques for descriptive and frequency data and this tutorial applies the same consideration to quantitative data.


Histogram

A histogram is a graph that shows the disribution of data that are continuous in nature, for example, age or height. A histogram resembles a bar chart but there is an important difference: a histogram is used for continuous data while a bar chart is used for categorical data. To emphasize that difference, histograms are normally drawn with no space between the bars (the data are continuous along the entire x-axis) while bar charts are normally drawn with a small space between bars (the data are categorical along the x-axis). As an example of a histogram, the next figure shows the New York city high temperature from May to September 1973 from the airquality data frame.

Notice that there is not a separate bar for each temperature; rather, R has clustered five temperatures into a single bar. Thus, there is a bar that combines the temperatures 70-74 and not separate bars for each of those temperatures. This histogram helps researchers determine if temperature is normally distributed, that is, does it display a typical “bell” curve. It should be fairly obvious from this graph that there are more temperatures around the 80° level than at either extreme so this histogram does indicate that the data are normally distributed.

As another example, the following figure shows a histogram for the body temperature of a beaver recorded every 10 minutes over the course of several hours, as found in the beaver1 data frame. This histogram shows normally-distributed data though there is an outlier at a temperature of 37.6°.

Histograms can also indicate data that are skewed and this would be important to researchers during a project’s exploratory phase. Consider, for example, the following figure which is the shape of petroleum rock samples in the rock data frame. While this histogram still indicates a normal distribution with levels falling off from a peak, there is a longer “tail” to the right so the data have a positive skew.

Finally, consider the histogram in the following figure, which is taken from the peri vector in the rock data frame. This shows a bi-modal distribution where there are two clear peaks in the data. It is critical for researchers to know that the data are bi-modal before they begin analyzing it since having two modes can cause certain statistical tests to fail.

Demonstration: Histogram

The following script creates an example histogram.

  • Line 2: This is the beginning of the histogram function (it ends on Line 8). For this histogram the Speed variable from the morley data frame is specified as the data source for the histogram.
  • Lines 3-5: This creates the main title of the histogram along with the labels for the x-axis and y-axis.
  • Line 6: To create a histogram, R analyzes the values contained in a variable and creates “bins” for those values. That means that many of the continuous values will be grouped into a single bin for analysis. The “breaks” parameter tells R how many breaks to allow in the variable. In this case, eight breaks are specified, which would create nine bins. R will analyze the data and use the “breaks” parameter as a “suggestion” and will only use that number of breaks if it makes sense for the data being graphed. Often, changing the number of breaks by just one or two will not change the histogram produced so researchers should play around with the “breaks” number to get the best possible representation of the data.
  • Line 7: This specifies that 10 colors will be used from the “cm.color” palette to shade the various bars in the histogram. Researchers need to experiment a bit with the color palette and number of colors to get the best result; however, “cm.color” along with the number of bars in the histogram seems to work well for this histogram. (Note: setting colors on graphs was introduced in Visualizing Descriptives.)
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIEhpc3RvZ3JhbVxuaGlzdChtb3JsZXkkU3BlZWQsXG4gIG1haW4gPSBcIk1vcmxleSdzIEV4cGVyaW1lbnRcIixcbiAgeGxhYiA9IFwiU3BlZWRcIixcbiAgeWxhYiA9IFwiRnJlcXVlbmN5XCIsXG4gIGJyZWFrcyA9IDgsXG4gIGNvbCA9IGNtLmNvbG9ycygxMClcbikifQ==

The DataCamp interface generates graphics in a Plots tab but because of the size of the interface those plots are “squished” and impossible to read. Click the double-headed arrow button on the Plots tab to open the graph in a larger window for evaluation and copying to a document. If the graphic does not open in a larger window then temporarily pause the browser’s pop-up blocker.

Skill Check: Histogram

Using the USJudgeRatings data frame, create a histogram of the CONT vector. The histogram should have these characteristics:

  • Main title: US Judge Ratings
  • X-axis title: Integrity Rating
  • Y-axis title: Frequency
  • Breaks: 11
  • Colors: cm.colors(8)
eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IiMgTm8gcHJlLWV4ZXJjaXNlIGNvZGUgZm9yIHRoaXMgZXhlcmNpc2UiLCJzYW1wbGUiOiIjIENyZWF0ZSB0aGUgaGlzdG9ncmFtIGFzIHNwZWNpZmllZCBhYm92ZS4iLCJzb2x1dGlvbiI6Imhpc3QoVVNKdWRnZVJhdGluZ3MkSU5URyxcbiAgbWFpbiA9IFwiVVMgSnVkZ2UgUmF0aW5nc1wiLFxuICB4bGFiID0gXCJJbnRlZ3JpdHkgUmF0aW5nXCIsXG4gIHlsYWIg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2tfYXJnKFwieFwiKSAlPiVcbiAgY2hlY2tfZXF1YWwoaW5jb3JyZWN0X21zZyA9IGV4MTJuZXEpXG5zdGF0ZTEgJT4lXG4gIGNoZWNrX2FyZyhcIm1haW5cIikgJT4lXG4gIGNoZWNrX2VxdWFsKGluY29ycmVjdF9tc2cgPSBleDEzbmVxKVxuc3RhdGUxICU+JVxuICBjaGVja19hcmcoXCJ4bGFiXCIpICU+JVxuICBjaGVja19lcXVhbChpbmNvcnJlY3RfbXNnID0gZXgxNG5lcSlcbnN0YXRlMSAlPiVcbiAgY2hlY2tfYXJnKFwieWxhYlwiKSAlPiVcbiAgY2hlY2tfZXF1YWwoaW5jb3JyZWN0X21zZyA9IGV4MTVuZXEpXG5zdGF0ZTEgJT4lXG4gIGNoZWNrX2FyZyhcImJyZWFrc1wiKSAlPiVcbiAgY2hlY2tfZXF1YWwoaW5jb3JyZWN0X21zZyA9IGV4MTZuZXEpXG5zdGF0ZTEgJT4lXG4gIGNoZWNrX2FyZyhcImNvbFwiKSAlPiVcbiAgY2hlY2tfZXF1YWwoaW5jb3JyZWN0X21zZyA9IGV4MTduZXEpXG5cblxuc3VjY2Vzc19tc2coXCJHb29kIEpvYiEgSGlzdG9ncmFtcyBhcmUgdmVyeSBpbXBvcnRhbnQgdG9vbHMgZm9yIGRhdGEgYW5hbHlzaXMuXCIpIn0=

Density Plot

A density plot provides the same information as a histogram but it is smoothed out so it is easier to read. Here is the same NY City temperature data from an earlier histogram but drawn as a density plot.

Given the previous plot it is natural to wonder, “What is density?” This is a calculated value such that the total area under the curve is assumed to be one and then each point along the x-axis is calculated to contribute the correct proportional amount to that total density. For many purposes it is adequate to just consider a density plot to be a smoothed histogram. As just one other example, following is a density plot of the bi-modal histogram presented earlier. The density plot makes the bi-modal nature of the data very evident.

The following script creates a density plot of the speed variable in the morley data frame. The lines are very similar to the last script except R plots the density of the data in Line 2 rather than just a count of the various bins.

eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIERlbnNpdHkgUGxvdFxucGxvdChkZW5zaXR5KG1vcmxleSRTcGVlZCksXG4gIG1haW4gPSBcIk1vcmxleSdzIEV4cGVyaW1lbnRcIixcbiAgeGxhYiA9IFwiU3BlZWRcIixcbiAgeWxhYiA9IFwiRGVuc2l0eVwiLFxuICBsd2QgPSAyLFxuICBjb2wgPSBcImJsdWU0XCJcbikifQ==

Demonstration: Density Plot

The following script creates a density plot for the Rape vector in the USArrests data frame.

  • Line 2: Create a density plot for USArrests$Rape.
  • Line 3: The main title for the graph.
  • Line 4: The title for the x-axis.
  • Line 5: The title for the y-axis.
  • Line 6: The type of line to draw. “Lwd 2” is a relitively thick line that is easy to see on a graph.
  • Line 7: The color for this graph is “magenta 4”
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIERlbnNpdHkgcGxvdCBvZiBVUyBBcnJlc3RzIGZvciByYXBlLlxucGxvdChkZW5zaXR5KFVTQXJyZXN0cyRSYXBlKSxcbiAgbWFpbiA9IFwiVVMgUmFwZSBBcnJlc3RzXCIsXG4gIHhsYWIgPSBcIkFycmVzdHNcIixcbiAgeWxhYiA9IFwiRGVuc2l0eVwiLFxuICBsd2QgPSAyLFxuICBjb2wgPSBcIm1hZ2VudGE0XCJcbikifQ==

Skill Check: Density Plot

Using the USJudgeRatings data frame, create a density plot of the CONT vector. The plot should have these characteristics:

  • Main title: US Judge Ratings
  • X-axis title: Integrity Rating
  • Y-axis title: Frequency
  • Lwd: 2
  • Col: blue4
eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IiMgTm8gcHJlLWV4ZXJjaXNlIGNvZGUgZm9yIHRoaXMgZXhlcmNpc2UiLCJzYW1wbGUiOiIjIENyZWF0ZSB0aGUgZGVuc2l0eSBwbG90IGFzIHNwZWNpZmllZCBhYm92ZS4iLCJzb2x1dGlvbiI6InBsb3QoZGVuc2l0eShVU0p1ZGdlUmF0aW5ncyRJTlRHKSxcbiAgbWFpbiA9IFwiVVMgSn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C0gZXgoKSAlPiUgY2hlY2tfZnVuY3Rpb24oXCJkZW5zaXR5XCIpXG5zdGF0ZTEgJT4lXG4gIGNoZWNrX2FyZyhcInhcIikgJT4lXG4gIGNoZWNrX2VxdWFsKGluY29ycmVjdF9tc2cgPSBleDIybmVxKVxuXG5zdGF0ZTIyIDwtIGV4KCkgJT4lIGNoZWNrX2Z1bmN0aW9uKFwicGxvdFwiKVxuc3RhdGUyMiAlPiVcbiAgY2hlY2tfYXJnKFwibWFpblwiKSAlPiVcbiAgY2hlY2tfZXF1YWwoaW5jb3JyZWN0X21zZyA9IGV4MjNuZXEpXG5zdGF0ZTIyICU+JVxuICBjaGVja19hcmcoXCJ4bGFiXCIpICU+JVxuICBjaGVja19lcXVhbChpbmNvcnJlY3RfbXNnID0gZXgyNG5lcSlcbnN0YXRlMjIgJT4lXG4gIGNoZWNrX2FyZyhcInlsYWJcIikgJT4lXG4gIGNoZWNrX2VxdWFsKGluY29ycmVjdF9tc2cgPSBleDI1bmVxKVxuc3RhdGUyMiAlPiVcbiAgY2hlY2tfYXJnKFwibHdkXCIpICU+JVxuICBjaGVja19lcXVhbChpbmNvcnJlY3RfbXNnID0gZXgyNm5lcSlcbnN0YXRlMjIgJT4lXG4gIGNoZWNrX2FyZyhcImNvbFwiKSAlPiVcbiAgY2hlY2tfZXF1YWwoaW5jb3JyZWN0X21zZyA9IGV4MjduZXEpXG5cblxuc3VjY2Vzc19tc2coXCJHb29kIEpvYiEgRGVuc2l0eSBwbG90cyBhcmUgdmVyeSBpbXBvcnRhbnQgdG9vbHMgZm9yIGRhdGEgYW5hbHlzaXMuXCIpIn0=

Line Graph

Line graphs display the frequency of some value in a linear form that makes trend detection easier. These types of graphs are especially useful with what is called “time series” data; that is, data that are gathered over a long period of time. As an example, consider the following from the airmiles dataset which charts the number of passenger miles on US commercial airlines from 1937 to 1960.

The following figure shows the number of accidental deaths in the United States from 1973 until 1979 by month taken from the USAccDeaths data frame. This line graph clearly shows a seasonal difference where there are more accidental deaths in the summer months than winter and detecting seasonal variation is one of the strengths of a line graph.

As one final example, the following figure shows the approval rating for US Presidents from 1945 until 1975 taken from the presidents data frame. This line graph shows a very high approval rating in 1945 (Roosevelt at the end of WWII) with dips in the early 1950’s (Truman and the Korean Conflict) and about 1974 (Watergate and Nixon’s resignation). Notice that there are two gaps in the line (late 1940’s and 1973) caused by missing data in the data frame.

Demonstration: Line Graphs

Line graphs like those shown above are very useful for detecting a change in some variable, especially over time. The following script creates a line graph of the monthly number of lung disease deaths in the United Kingdom between 1974 and 1979 as found in the ldeaths data frame. It shows a very interesting cycle where there are consistently more deaths in the winter months than the summer months.

  • Line 2: The plot function is called for the ldeaths data frame. By itself, plot will create a scatter plot but Line 6 in this script specifies a line graph.
  • Lines 3-5: This creates the main title of the line graph along with the labels for the x-axis and y-axis.
  • Line 6: This specifies the type of line graph wanted. The possible values are: “p” for points, “l” for lines, “b” for both, “c” for the lines part alone of “b”, “o” for both ’overplotted,’ “h” for ‘histogram-like’ (or ‘high-density’) vertical lines, “s” for stair steps, “S” (that is a capital “S”) for other steps, “n” for no plotting. Researchers could quickly try several different settings to determine the best way to present the data and students working through this tutorial can change the value on Line 6 to see the different types of plots.
  • Line 7: The lwd parameter sets the width of the line. The default value is 1 so this line specifies a double-width line in order to make it easier to see.
  • Line 8: This sets the color of the line to red in order to set it off from the axis and text.
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIExpbmUgR3JhcGhcbnBsb3QobGRlYXRocyxcbiAgbWFpbiA9IFwiTHVuZyBEaXNlYXNlIERlYXRoc1wiLFxuICB4bGFiID0gXCJNb250aFwiLFxuICB5bGFiID0gXCJOdW1iZXJcIixcbiAgdHlwZT1cImxcIixcbiAgbHdkID0gMixcbiAgY29sID0gXCJyZWRcIlxuKSJ9

The above graph shows an interesting cyclic variation in the number of deaths over the course of a year. The years listed on the x-axis indicate January of that year. The number of deaths seems to spike high over the winther months and hit a low in about August or September. This type of cycle is very easy to see on this plot but would be difficult to detect in a table full of numbers.

Skill Check: Line Graphs

Using the UKgas data frame, create a line graph that meets this specification:

  • plot: UKgas
  • main: UK Quarterly Gas Consumption
  • xlab: Year
  • ylab: Millions of Therms
  • type: l
  • lwd: 2
  • col: lightskyblue
eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IiMgTm8gcHJlLWV4ZXJjaXNlIGNvZGUgZm9yIHRoaXMgZXhlcmNpc2UiLCJzYW1wbGUiOiIjIENyZWF0ZSBhIGxpbmUgZ3JhcGggdXNpbmcgdGhlIHNwZWNpZmljYXRpb25zIGxpc3RlZCBhYm92ZS4iLCJzb2x1dGlvbiI6IiMgTGluZSBHcmFwaFxucGxvdChVS2dhcyxcbiAgbWFpbiA9IFwiVUsgUXVhcnRlcmx5IEdhcyBDb25zdW1wdGlvblwiLFxuICB4bGFiID0gXCJZZWFyXCIsXG4gIHlsYWIgPSBcIk1pbGxpb25zIG9mIFRoZXJtc1wiLFxuICB0eXBlPVwibFwiLFxuICBsd2QgPSAyLFxuICBjb2wgPSBcImxpZ2h0c2t5Ymx1ZVwiXG4pIiwic2N0IjoiIyBUaGVzZSBhcmUgbnVtYmVyZWQgYnkgdGhlIGV4ZXJjaXNlIG51bWJlciBhbmQgYSBzZXF1ZW5jZSB3aXRoaW4gdGhlIGV4ZXJjaXNlLlxuIyBUaGUgc2VxdWVuY2UgbnVtYmVyIGlzIG5vdCBuZWNlc3NhcnkgaWYgdGhlcmUgaXMgb25seSBvbmUgY2hlY2suXG5leDMxbmVxIDwtIFwiQ2hlY2sgdGhlIGxpbmUgZ3JhcGggc2V0dGluZ3MgdG8gYmUgc3VyZSB0aGV5IG1hdGNoIHRoZSByZXF1aXJlbWVudC5cIlxuZXgzMm5lcSA8LSBcIkNoZWNrIHRoYXQgdGhlIGxpbmUgZ3JhcGggaXMgdXNpbmcgdGhlIFVLZ2FzIHZlY3Rvci5cIlxuZXgzM25lcSA8LSBcIkNoZWNrIHRoYXQgbWFpbiBpcyBVSyB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Y29ycmVjdF9tc2cgPSBleDM2bmVxKVxuc3RhdGUzICU+JVxuICBjaGVja19hcmcoXCJsd2RcIikgJT4lXG4gIGNoZWNrX2VxdWFsKGluY29ycmVjdF9tc2cgPSBleDM3bmVxKVxuc3RhdGUzICU+JVxuICBjaGVja19hcmcoXCJjb2xcIikgJT4lXG4gIGNoZWNrX2VxdWFsKGluY29ycmVjdF9tc2cgPSBleDM4bmVxKVxuXG5cbnN1Y2Nlc3NfbXNnKFwiUGVyZmVjdCEgQSBsaW5lIGdyYXBoIGNhbiByZXZlYWwgaW50ZXJlc3RpbmcgdHJlbmRzIG92ZXIgdGltZS5cIikifQ==

Plot

Plots (often called “scatter plots”) are used to show how two different variables are related. Scatter plots are often used in connection with correlation where they visually indicate the correlation between two variables. For example, The following figure is the plot of stopping distance vs. speed from the cars data frame.

The previous figure shows that as a car’s speed increases the stopping distance also increases, which is exactly what would be expected. (Note: this data were gathered on cars in the 1920s.) Often, a line of best fit is included with a plot to better visualize the relationship between the two variables, as illustrated in the following figure.

As a second example, The following figure is a plot of the eruption time for Old Faithful geyser, taken from the faithful data frame, as a function of the waiting time between eruptions.

The previous figure shows that as the time between eruptions increases the time that the eruption lasts also increases. Notice that this scatter plot also suggests that the data are bi-modal since there are two clusters of points and a researcher would want to explore that matter before doing much else with the data.

Demonstration: Plot

The following script creates a simple plot using the swiss data frame.

  • Line 2: This starts the plot function. Like many R functions, plot requires a formula input in the form of y ~ x where the dependent variable (the y-axis) is first in the formula and the independent variable (the x-axis) is second. For this plot, Education is the independent variable and will be on the x-axis while Fertility is the dependent variable on the y-axis. The researcher was answering the question “Does education level affect the number of children people have?”
  • Lines 3-5: This creates the main title and the labels for the x-axis and y-axis.
  • Line 6: The pch attribute is the type of point used on the graph. R has a number of pre-defined types of points and for this graph type number 21 is selected. That is a hollow circle where both the internal color and the circle colors can be specified.
  • Line 7: The fill color of the circle is specified using the bg attribute. This this case it is a shade of chartreuse (a yellow-green hue).
  • Line 8: The color of the circle is specified using the col attribute.
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFNpbXBsZSBQbG90XG5wbG90KHN3aXNzJEZlcnRpbGl0eSB+IHN3aXNzJEVkdWNhdGlvbixcbiAgbWFpbiA9IFwiU3dpc3MgSW5kaWNhdG9yc1wiLFxuICB4bGFiID0gXCJFZHVjYXRpb25cIixcbiAgeWxhYiA9IFwiRmVydGlsaXR5XCIsXG4gIHBjaCA9IDIxLFxuICBiZyA9IFwiY2hhcnRyZXVzZTNcIixcbiAgY29sID0gXCJibGFja1wiXG4pIn0=

The following script creates a plot with a line of best fit.

  • Lines 2-9: This demonstration plots two vectors from the attitude data frame, rating is the independent variable on the x-axis while complaints is the dependent variable on the y-axis. The researcher was answering the question “Do employees with higher overall ratings handle complaints better?” The other parameters of this plot function are defined for the previous plot and are not further discussed here.
  • Line 10: This starts the abline function that ends on line 13. Abline draws a line “from point A to point B” (which is why the function is named “abline”) on an existing plot. In this case, the line to be drawn is calculated with the lm function, which calculates the slope and y-intercept of the line of best fit for the two specified vectors. The lm function was introduced in the tutorial about Correlation.
  • Lines 11-12: These are the parameters for the line of best fit as drawn on the plot.
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIExpbmUgb2YgQmVzdCBGaXRcbnBsb3QoYXR0aXR1ZGUkY29tcGxhaW50cyB+IGF0dGl0dWRlJHJhdGluZyxcbiAgbWFpbiA9IFwiQXR0aXR1ZGUgRGF0YVwiLFxuICB4bGFiID0gXCJPdmVyYWxsIFJhdGluZ1wiLFxuICB5bGFiID0gXCJDb21wbGFpbnRzXCIsXG4gIHBjaCA9IDIxLFxuICBiZyA9IFwia2hha2kyXCIsXG4gIGNvbCA9IFwiYmxhY2tcIlxuKVxuYWJsaW5lKGxtKGF0dGl0dWRlJGNvbXBsYWludHMgfiBhdHRpdHVkZSRyYXRpbmcpLFxuICBjb2wgPSBcImRhcmtzZWFncmVlbjRcIixcbiAgbHdkID0gMlxuKSJ9

Skill Check: Plot

Using the rock data frame, create a plot of area (y-axis) as a function of peri (x-axis). The graph should use these specifications:

plot:

  • main: Rock Area by Perimeter
  • xlab: Perimeter
  • ylab: Area
  • pch: 21
  • bg: goldenrod3
  • col: black

abline:

  • col: violetred3
  • lwd: 2
eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IiMgTm8gcHJlLWV4ZXJjaXNlIGNvZGUgZm9yIHRoaXMgZXhlcmNpc2UiLCJzYW1wbGUiOiIjIENyZWF0ZSBhIHNjYXR0ZXIgcGxvdCB3aXRoIGEgbGluZSBvZiBiZXN0IGZpdCBhcyBzcGVjaWZpZWQgYWJvdmUuIiwic29sdXRpb24iOiIjIExpbmUgb2YgQmVzdCBGaXRcbnBsb3Qocm9jayRhcmVhIH4gcm9jayRwZXJpLFxuICBtYWluID0gXCJSb2NrIEFyZWEgYnkgUGVyaW1ldGVyXCIsXG4gIHhsYWIgPSBcIlBlcmltZXRlclwiLFxuICB5bGFiID0gXCJBcmVhXCIsXG4gIHBjaCA9IDIxLFxuICBiZyA9IFwiZ29sZGVucm9kM1wiLFxuICBjb2wgPSBcImJsYWNrXCJcbilcbmFibGluZShsbShyb2NrJGFyZWEgfiByb2NrJHBlcmkpLFxuICBjb2wgPSBcInZpb2xldHJlZDNcIixcbiAgbHdkID0gMlxuKSIsInNjdCI6ImV4NDAxbmVxIDwtIFwiQ2hlY2sgdGhhdCB0aGUgcGxvdCBmb3JtdWxhIGlzIHJvY2skYXJlYSB+IHJvY2skcGVyaS5cIlxuZXg0MDJuZXEgPC0gXCJDaGVjayB0aGF0IG1haW4gaXMgUm9jayBBcmVhIGJ5IFBlcmltZXRlci5cIlxuZXg0MDNuZXEgPC0gXCJDaGVjayB0aGF0IHhsYWIgaXMgUGVyaW1ldGVyLlwiXG5leDQwNG5lcSA8LSBcIkNoZWNrIHRoYXQgeWxhYiBpcyBBcmVhLlwiXG5leDQwNW5lcSA8LSBcIkNoZWNrIHRoYXQgcGNoIGlzIDIxLlwiXG5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GNoZWNrX2VxdWFsKGluY29ycmVjdF9tc2cgPSBleDQwN25lcSlcblxuc3RhdGU0MiA8LSBleCgpICU+JSBjaGVja19mdW5jdGlvbihcImxtXCIpXG5zdGF0ZTQyICU+JVxuICBjaGVja19hcmcoXCJmb3JtdWxhXCIpICU+JVxuICBjaGVja19lcXVhbChpbmNvcnJlY3RfbXNnID0gZXg0MDhuZXEpXG5cblxuc3RhdGU0MyA8LSBleCgpICU+JSBjaGVja19mdW5jdGlvbihcImFibGluZVwiKVxuc3RhdGU0MyAlPiVcbiAgY2hlY2tfYXJnKFwiY29sXCIpICU+JVxuICBjaGVja19lcXVhbChpbmNvcnJlY3RfbXNnID0gZXg0MDluZXEpXG5zdGF0ZTQzICU+JVxuICBjaGVja19hcmcoXCJsd2RcIikgJT4lXG4gIGNoZWNrX2VxdWFsKGluY29ycmVjdF9tc2cgPSBleDQxMG5lcSlcblxuc3VjY2Vzc19tc2coXCJQZXJmZWN0ISBBIHNjYXR0ZXJwbG90IGlzIG9uZSBvZiB0aGUgbW9zdCBjb21tb24gZ3JhcGhzIHVzZWQgaW4gcmVzZWFyY2guXCIpIn0=

Q-Q Plot

Very often, researchers in the exploratory phase of a project need to know whether a data frame is normally distributed. Creating a histogram or density plot is very helpful in that regard, but a Q-Q (“Quantile-Quantile”) Plot is a typical method used to determine if a data frame is normally distributed. The following figure is a Q-Q plot of the New York City temperatures in the airquality data frame.

An absolutely perfect normal distribution would generate a straight line Q-Q plot and the blue line in the previous figure is ideal. Interpreting a Q-Q plot is more art than science but as long as most values are near the ideal line then the data are considered normally distributed. The following figure is a Q-Q plot for the Old Faithful eruption, as plotted earlier in this tutorial.

The above plot shows a typical bi-modal pattern. On the left side of the plot is a reasonably flat area from -3 to -0.5 on the x-axis and then the plot skips upward and creates a second reasonably flat area between 0.5 and 3. Imagine two parallel lines running through the lower and upper parts of the plot and then notice that the green “ideal” line does not get very close to the slope of either of those two lines. This Q-Q plot shows that the data are not normally distributed.

The following figure shows a curved Q-Q plot that is typical for a data frame that is skewed. In this case, the plotted sunspot data have a heavy positive skew which is indicated by the long “tail” on the left side of the plot. That skew should be verified with a histogram or density plot.

Demonstration: Q-Q Plot

The following script creates a Q-Q plot for a normally-distributed data frame.

  • Lines 2-4: This executes the qqnorm function and passes that function the weight variable from the chickwts data frame. This function draws the Q-Q Plot. The only parameter needed is main, which adds the title to the plot.
  • Lines 6-9: This executes the qqline function and passes that function the weight variable from the chickwts data frame. This function draws the straight line that indicates a perfect Q-Q plot. The only parameters passed to the function in this script is to set the color to blue and the size to 2, in the same way that was found in the scatterplots above.
eyJsYW5ndWFnZSI6InIiLCJzYW1wbGUiOiIjIFEtUSBQbG90XG5xcW5vcm0oY2hpY2t3dHMkd2VpZ2h0LFxuICBtYWluID0gXCJRUSBQbG90OiBDaGljayBXZWlnaHRzXCJcbilcbiMgUT1RIExpbmVcbnFxbGluZShjaGlja3d0cyR3ZWlnaHQsXG4gIGNvbCA9IFwiYmx1ZVwiLFxuICBsd2QgPSAyXG4pIn0=

Skill Check: Q-Q Plot

Using the swiss data frame, create a Q-Q plot of the Fertility vector. The plot should have a main title of QQ Plot: Swiss Fertility and a red Q-Q line with a width of 2.

eyJsYW5ndWFnZSI6InIiLCJwcmVfZXhlcmNpc2VfY29kZSI6IiMgTm8gcHJlLWV4ZXJjaXNlIGNvZGUgZm9yIHRoaXMgZXhlcmNpc2UiLCJzYW1wbGUiOiIjIENyZWF0ZSBhIFEtUSBwbG90IG9mIHRoZSBGZXJ0aWxpdHkgdmVjdG9yIGluIHRoZSBzd2lzcyBkYXRhIGZyYW1lIHVzaW5nIHRoZSByZXF1aXJlbWVudHMgbGlzdGVkIGFib3ZlLiIsInNvbHV0aW9uIjoicXFub3JtKHN3aXNzJEZlcnRpbGl0eSxcbiAgbWFpbiA9IFwiUVEgUGxvdDogU3dpc3MgRmVydGlsaXR5XCJcbilcbnFxbGluZShzd2lzcyRGZXJ0aWxpdHksXG4gIGNvbCA9IFwicmVkXCIsXG4gIGx3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