Back to R Code

Histograms

Console Help Command: ?hist()

hist(object)

  • object must be a “numeric” vector.

To control the axis labels and main title:

hist(object, xlab="WriteLabelHere", ylab="WriteLabelHere", main="WriteTitleHere")

| R Overview |

Example Code



Boxplots

Console Help Command: ?boxplot()

boxplot(object)

  • If object is a vector of numbers, then this will give a single boxplot.

To control the axis labels and main title:

hist(object, xlab="WriteLabelHere", ylab="WriteLabelHere", main="WriteTitleHere")

For side-by-side boxplots…

boxplot(variable1 ~ variable2, data=NameOfYourData)

  • variable1 is a quantitative variable.
  • variable2 is a qualitative variable.
  • NameOfYourData is the name of the dataset containing variable1 and variable2.

| Textbook |

Example Code



Dot Plots

Console Help Command: ?stripchart()

stripchart(object)

  • If object is a vector of numbers, then this will give a single dotplot.

To control the axis labels and main title:

dotplot(object, xlab="WriteLabelHere", ylab="WriteLabelHere", main="WriteTitleHere")

For side-by-side dotplots…

stripchart(variable1 ~ variable2, data=NameOfYourData)

  • variable1 is a quantitative variable.
  • variable2 is a qualitative variable.
  • NameOfYourData is the name of the dataset containing variable1 and variable2.

| Textbook |

Example Code



Scatterplots

Console Help Command: ?plot()

plot(y ~ x, data=NameOfYourData)

  • y is the response variable and x is the explanatory variable. Both must be numeric.

To control the axis labels and main title:

plot(y ~ x, data=NameOfYourData, xlab="WriteLabelHere", ylab="WriteLabelHere", main="WriteTitleHere")

| Textbook |

Example Code



Bar Charts

Console Help Command: ?barplot()

barplot(table(object))

  • object is some qualitative variable.

To control the axis labels and main title:

barplot(table(object), xlab="WriteLabelHere", ylab="WriteLabelHere", main="WriteTitleHere")

| Textbook |

Example Code



QQ-Plots

Console Help Command: ?qqnorm()

qqnorm(object)

qqline(object)

  • object is some numeric vector, i.e., a quantitative variable.

A nicer version of the Normal Probability Plot (QQ-Plot) is in the car (classification and regression) library. To use it, install the car package, then run the following commands.

library(car)

qqPlot(object)

| Textbook |

Example Code



Custom Plots

A fast way to make custom plots is with the mPlot() command of the mosaic package.

In the Console type:

> library(mosaic)

> mPlot(data.frame)

Do not use the mPlot() command in an R Markdown file.

| Textbook | Examples: |