Plotting in R


There are no example plots here. These notes are to assist in understanding some key options in the functions as used in R.  See Module 6 Plotting in R for examples.

  1. To explain  option in basic plots (plot, hist, boxplot)
  2. How to position plots on a page
  3. Explaining options in package lattice
  4. Explaining options for ggplot from library(ggplot2)

Back to Top
You need to know your data. For instance you need to know the variable names (R is case sensitive), and you may need the range of data. Use the functions names() and summary() to get this information. You also need to be aware of the type of data, is your variable a numeric or a factor?

1. To explain options in basic plots (plot, hist, boxplot)

To follow this code let:

dat be the dataset,

var2, var2  be a continuous variables and

grp1, grp2  be the grouping/categorical variables
hist(var, breaks=c(5,10,15,20)) # the breaks option determines the cut points for the histogram
A density plot: dens(var) or  hist(var,freq=FALSE)
If you assign the histogram to an object you can then add lines to it:
myhist < - hist(var,freq=FALSE)
To make a boxplot:
boxplot(var~grp, data=dat)
This gives a box and whisker plot. The default whiskers are extended to the max and min of the data or 1.5 times the IQR, whichever is the smaller.

To ensure they do cover the range you can change the range option:

boxplot(var~ grp, data=dat, range=0)

Plotting a scatterplot, with two continuous variables, as two columns in the dataset

?View Code RSPLUS
myplot &lt;- plot(dat, main="plot title", ylab="y axis label", xlab="x axis label")

Type of plot, for a line type=’l’, for a points type=’p”, and for both type=’b’.
Changing colours of points, use option col=”red”, “blue”, “purple”,

To get a list of colours use colours()
Changing point type, use pch=19 through to pch=25
Adding lines, after applying a regression model, fitted lines can be added.See Module 3 Regression
The plot() function is quite powerful in R. The type of plot you get depends on the R object it is applied to.

Note if the object is a dataset, or a model it will give different plots. The defaults for particular object types are considered the sensible option, or useful for that object.
Back to top

2. How to position plots on a page

When plotting, we need to use a function to help us place the plot on a page. The default is one plot on the page, but if we wish to change this we need to save the original graphics setting.

The code here saves the original setting, then plots 2 side by side and then replaces the original  page setting.

?View Code RSPLUS
old.par =  par(mfrow=c(1,2))
plot(myplot1, myplot2)

So to get

  • side by side plots  use                        par(mfrow=c(1,2)),
  • to have one above the other use      par(mfcol=c(1,2)), and
  • to get 4 per page                                 par(mfrow=c(2,2))

Back to top

3 Explaining options in lattice plots

The package lattice allows the plotting of facets of the data, here we have a plot of var1 versus grp1 with facets for each of the second grp2. This plotting function uses the formula code for the plot.

?View Code RSPLUS
xyplot(var~grp | grp2, data=dat)

In some cases you may need to ensure the grouping variable is a factor.
This can be also added in the code
xyplot(var~grp | factor(grp2), data=dat)
To add trend lines for regression
xyplot(var~grp | factor(grp2), data=dat, type=c("p", "r"))

To add a plot title and axis title

?View Code RSPLUS
xyplot(var~grp | factor(grp2), data=dat,
type=c("p", "r"),
main ="My first Lattice plot",
ylab ="Y axis label",
xlab ="X axis label"  )

To change font size in titles and on the scale

?View Code RSPLUS
xyplot(var~grp | factor(grp2), data=dat,
type=c("p", "r"),
main =list (
label ="My first Lattice plot", cex=0.75)
ylab =list ( label ="Y axis label", cex=0.75)
xlab =list ( label ="X axis label", cex=0.75),
scales =list(cex=0.5))

Other types of plot available in library(lattice) are:
Bar charts                                           barchart()
Box and Whisker plot                      bwplot()
One dimensional strip plot            stripplot()
Three dimensional plots                 cloud()
Three dimensional surface plots  wireframe()

Using the Help features
Once you come familiar use the R HELP and the package lattice help and documentation.
Back to top

4. Explaining options for ggplot from library(ggplot2)

This package has its own way of specifying the data and the plot. Some examples are given in Module 6.
The function is ggplot() but it is from library(ggplot2).

?View Code RSPLUS
mygplot &lt; - ggplot(dat, aes(x=xvar, y=yvar) + geom_point() + stat_smooth())

There are specifications for aesthetics, defining the variables and the geom or stat to define the style of plot, and finally the line style with stat.

Summary table of some useful aspects and options for the package ggplot2
ggplot2 Comment
Requires data in a data frame Define the data set. It must be a data frame
Mapping data to the plot    aes  (aesthetics) aes(x=xvar, y= yar) This maps the data to the visual elements in the plot (aesthetics)
Terminology geom object or a stat object.  How to display the data geom_bar()   geom_histogram bar chart. You can specify binwidth
geom_point()  scatterplot
geom_line()     line connecting observations in x order
geom_boxplot()  box and whisker
geom_smooth() smooth conditioned mean
geom_path()  connecting observations in original order
Terminology  for stat stat_bin() bar chart
stat_identity() scatter or line plot
stat_identity() connecting observations in original order
Stat_smooth() smooth conditioned mean . Choose method=lm  to add a regression, or method=loess
Axis title control  use xlab and ylab
For Main title   opts(title=”Overall plot title”)
Other features Facet- grid  to make a row and column array of plots, scales, colour,

See also the main plotting Module 6 for selected examples

Please contact us and ask for particular plots you would like explained.
Back to top