Basic skills in R: Basic graphics
Summary of high-level plotting functions
High-level plotting functions are designed to generate a complete plot of the data passed as arguments to the function. Where appropriate, axes, labels and titles are automatically generated (unless you request otherwise.) High-level plotting commands always start a new plot, erasing the current plot if necessary.
One of the most frequently used plotting functions in R is the function plot(), where the output depends on the type or class of the first argument.
plot(x, y)plot(xy) |
If x and y are vectors, plot(x, y) produces a scatter plot of y against x. The same effect can be produced by supplying xy as either a list containing two elements x and y or a two-column matrix. |
plot(f)plot(f, y) |
Here, f is a factor object and y is a numeric vector. The first form generates a bar graph of f; The second form produces box-and-whisker plots of y for each level of f. |
plot(df) |
If df is a data frame, distributional plots of the variables in the data frame are produced. |
Other commonly used high-level plotting functions are listed below.
hist(x) hist(x, nclass = ...)hist(x, breaks = ...) |
The function hist() produces a histogram of the numeric vector x. A sensible number of classes is usually chosen, but a suggestion can be given with the argument nclass. Alternatively, the breaks can be specified exactly with the argument breaks. |
barplot(x) |
If x is a numeric vector, a bar diagram is produced of which the heights of the bars are given by the vector. If x is a crosstable or a matrix and the argument beside = TRUE is given, then each bar of the diagram corresponds to a column of x, with the values in the column giving the heights of juxtaposed sub-bars making up the bar. |
boxplot(y)boxplot(y ~ x) |
If y is a numeric vector, a box-and-whisker diagram is produced. In case a formula is used has the form y ~ x where y is a numeric vector, then the box-and-whisker diagrams are grouped according to the value of the (categorical) variable x. |
qqnorm(x) |
The functions produce a quantile-quantile diagram, better known as QQ-plot. The first form plots the numeric vector x against the expected normal distribution quantiles and the second adds a straightline to such a plot by drawing a line through the distribution and data quantiles. The third form plots the quantiles of x against those of y to compare their respective distributions. |
Unlock full access