You are on page 1of 4

Graphics with R

The plot() function


> Fibonacci <- c(1,1,2,3,5,8,13)
> plot(Fibonacci)

Add title, subtitle and axes labels


> plot(Fibonacci,
+
main = "Fibonacci series", # title
+
sub = "An elegant series", # subtitle
+
xlab = "Index",
# x-axis label
+
ylab = "Fibonacci"
# y-axis label
+)

Other customisations
> plot (x = Fibonacci,
+
main = "The first 7 Fibonacci numbers",
# title
+
sub = "An elegant series",
# subtitle
+
xlab = "Position in the sequence",
# x-axis label
+
ylab = "The Fibonacci number",
# y-axis label
+
font.main = 1,
# plain text for title
+
font.sub = 2,
# boldface font
+
font.lab = 3,
# italics
+
font.axis = 4,
# bold italics
+
col.main = "red",
# title font colour
+
col.sub = "gray25",
# subtitle font colour
+
col.lab = "black",
# label font colour
+
col.axis = "springgreen4",
# axis font colour
+
cex.main = 1.2,
# title font size
+
cex.sub = 1,
# subtitle font size
+
cex.lab = 1,
# label font size
+
cex.axis = 1,
# axis font size
+
type = "b",
# plot type: "p" point, "l" line, "b" both,
+
# "o" line over the top of points,
+
# "h" histogram-like vertical bars
+
# "s" or "S" staircase,
+
# "c" connecting lines from "b" version
+
col = "blue",
# plot colour
+
pch = 19,
# plot character
+
cex = 5,
# plot 5x the usual size
+
lty = 2,
# change line type to dashed
+
lwd = 4,
# change line width to 4x usual
+
xlim = c(0, 15),
# x scale limits
+
ylim = c(0, 15),
# y scale limits
+)

For plot character and line types, see p156 Navarro.

Histograms
> load("aflsmall.Rdata")
> hist(afl.margins)
> hist(afl.margins, breaks = 3)
> hist(afl.margins, breaks = 0:116)

# specify the number of bins


# give a vector to specify where breaks occur

> hist(x = afl.margins,


+
main = "2010 AFL margins",
# plot title
+
xlab = Margin,
# set the x-axis label
+
density = 10,
# draw shading lines, 10 per inch
+
angle = 40,
# set the angle of shading lines to 40 degrees
+
col = "gray80",
# set the colour of shading lines
+
border = "gray20",
# set the colour of the borders of the bars
+
labels = TRUE,
# add frequency labels to each bar
+
ylim = c(0,40)
# change the scale of y-axis
+)

Stem and leaf, Boxplots


> stem(afl.margins)

Draw a single boxplot:


> boxplot(x = afl.margins,
+
xlab = "AFL games, 2010",
+
ylab = "Winning Margin",
+
border = "grey50",
# dim the border of the box
+
frame.plot = FALSE,
# don't draw a frame
+
staplewex = 0,
# don't draw staples
+
whisklty = 1
# solid line type for whisker
+)

Draw multiple boxplots e.g. margin variable for each year:


> load("aflsmall2.Rdata")
> who(TRUE)
-- Name --- Class -- -- Size -afl2
data.frame 4296 x 2
$margin
numeric
4296
$year
numeric
4296
> head(afl2)
margin year
1
33 1987
2
59 1987
3
45 1987
4
91 1987
5
39 1987
6
1 1987

> boxplot(formula = margin ~ year,


# the formula
+
data = afl2,
# the dataset
+
xlab = AFL season,
+
ylab = Winning Margin,
+
frame.plot = FALSE,
# don't draw a frame
+
staplewex = 0,
# don't draw staples
+
staplecol = white,
# (fixes a tiny display issue)
+
boxwex = 0.75,
# narrow the boxes slightly
+
boxfill = grey80,
# lightly shade the boxes
+
whisklty = 1,
# solid line for whiskers
+
whiskcol = grey70,
# dim the whiskers
+
boxcol = grey70,
# dim the box borders
+
outcol = grey70,
# dim the outliers
+
outpch = 20,
# outliers as solid dots
+
outcex = 0.5,
# shrink the outliers
+
medlty = blank,
# no line for the medians
+
medpch = 20,
# instead, draw solid dots
+
medlwd = 1.5
# make them larger
)

Scatterplots
A simple scatterplot:

> library(car)
# scatterplot() is contained in this library
> scatterplot(dan.grump ~ dan.sleep,
+
data = parenthood,
+
smooth = FALSE
# prevents drawing a smooth trendline
+)

To get scatter plot matrix, use the pairs() function:

> cor(x = parenthood)


# this is the correlation matrix we want to plot
dan.sleep baby.sleep dan.grump
day
dan.sleep 1.00000000 0.62794934 -0.90338404 -0.09840768
baby.sleep 0.62794934 1.00000000 -0.56596373 -0.01043394
dan.grump -0.90338404 -0.56596373 1.00000000 0.07647926
day
-0.09840768 -0.01043394 0.07647926 1.00000000
> pairs(x = parenthood)
# gives a 4x4 scatterplot matrix

You can specify which variables you want for the matrix:

> pairs(formula = ~ dan.sleep + baby.sleep + dan.grump,


# exclude the day variable
+
data = parenthood
# gives a 3x3 scatterplot matrix
+)

Barplots
e.g. display the number of finals each team played over the time spanned by afl dataset.
> afl.freq <- tabulate(afl.finalists)
> teams <- levels(afl.finalists)

# create a vector with info to be displayed


# create a variable with team names

> barplot(height = afl.freq,


# specify the argument name
+
names.arg = teams,
# label each bar with the name of the team
+
las = 2
# rotate labels: text is perpendicular to the axis
+
ylab = Number of finals,
+
main = Finals played, 1987-2010,
+
density = 10,
# shade the bars
+
angle = 20,
# shade the lines angle
+)

You might also like