Any sufficiently advanced technology is equivalent to magic.

Top

Site Menu

Graphical Summary - Numerical Data

This page will only include very basic visualizations. More complex and customizable graphics can be made using the ggplot2 package along with others.

Histograms

The hist() function will produce a histogram of the provided numerical data. A few of the additional arguments for hist() are listed below along with their use:
Example: Using the iris dataset from R

# no pec # Basic Histogram of Petal.Length hist(iris$Petal.Length) # Customized Histogram with Frequencies hist(iris$Petal.Length, main = "Histogram of Iris Petal Lengths (Frequency)", xlab = "Petal Length", xlim = c(0, 8), ylim = c(0, 60), breaks = 8, labels = TRUE, col = "skyblue") # Customized Histogram with Probability Densities hist(iris$Petal.Length, main = "Histogram of Iris Petal Lengths (Density)", xlab = "Petal Length", xlim = c(0, 8), ylim = c(0, 1), breaks = 8, labels = TRUE, freq = FALSE, col = "tomato")
If you want to produce histograms of the Petal.Length variable but separated by Species, separate histograms must be produced with subsetted data (using logical operators and '[ ]' notation).
Example: Using the iris dataset from R

# no pec # Histograms for the Petal Length variable hist(iris$Petal.Length, main = "Histogram of Petal Lengths", xlab = "Petal Length", xlim = c(1, 7), ylim = c(0, 40)) # Now separated between the different species hist(iris$Petal.Length[iris$Species == "setosa"], main = "Histogram of Petal Lengths (Setosa)", xlab = "Petal Length", xlim = c(1, 7), ylim = c(0, 40), breaks = 2, col = "lightseagreen") hist(iris$Petal.Length[iris$Species == "versicolor"], main = "Histogram of Petal Lengths (Versicolor)", xlab = "Petal Length", xlim = c(1, 7), ylim = c(0, 40), col = "mediumseagreen") hist(iris$Petal.Length[iris$Species == "virginica"], main = "Histogram of Petal Lengths (Virginica)", xlab = "Petal Length", xlim = c(1, 7), ylim = c(0, 40), col = "darkseagreen")

Video Tutorial:

Boxplots

The boxplot() function will produce a boxplot of the provided numerical data. A few of the additional arguments for boxplot() are listed below along with their use:
Example: Using the iris dataset from R

# no pec # Basic Boxplot of Petal.Length boxplot(iris$Petal.Length) # Customized Boxplot boxplot(iris$Petal.Length, main = "Boxplot of Iris Petal Lengths", ylab = "Petal Length", ylim = c(0, 8), range = 0.5, col = "skyblue")

If you want to produce a boxplot of the Petal.Length variable but separated by Species, you will use '~' notation.

Example: Using the iris dataset from R

# no pec # Basic Boxplot separated by Species boxplot(iris$Petal.Length ~ iris$Species) # Customized Boxplot of Petal Length separated by Species boxplot(iris$Petal.Length ~ iris$Species, main = "Boxplot of Iris Petal Lengths", xlab = "Species", ylab = "Petal Length", names = c("Setosa", "Versicolor", "Virginica"), ylim = c(0, 8), col = c("lightseagreen", "mediumseagreen", "darkseagreen"))
When using '~' notation, the numerical variable comes first followed by the categorical variable that will be used to group the numerical variable.

Video Tutorial: