Basic plots with R

 

Some examples in this entry have been adapted from “Producing Simple Graphs with R” by Frank McCown: http://www.harding.edu/fmccown/r/

Generation of random data for the examples

We generate a vector x of 15 values from a normal random distribution with mean=0 and standard deviation=1 and a vector y of 15 values from a normal random distribution with mean=0.2 and standard deviation=1:

set.seed(123)
x<-rnorm(15,0,1)
i<-c(1:15)
set.seed(4)
y<-rnorm(15,0.2,1)

Line charts

The simplest graphical representation of a numerical variable is the line chart provided by the command plot()

plot(x)  #plots the values of x (vertical axis) as a function of the index of each value (horizontal axis)

plot of chunk unnamed-chunk-2

col: specifies the color of the points

  • col=1 black
  • col=2 red
  • col=3 green
  • col=4 blue
  • col=5 light blue
plot(x,col=3) #color=green

plot of chunk unnamed-chunk-3

pch: specifies the symbol for the points

pch=1 symbol=cercle

pch=2 symbol=triangle

pch=3 symbol=plus

pch=4 symbol=star

pch=5 symbol=dyamond

plot(x,col=3, pch=4) # color=green, symbol=star

plot of chunk unnamed-chunk-4

main adds a title to the plot. main is specified within the plot() function

plot(x,col=3, main="line chart") #color=green

plot of chunk unnamed-chunk-5

title adds a title to the plot. title is specified outside the plot() function

plot(x,col=3)
title("line chart")

plot of chunk unnamed-chunk-6

type: You can add lines between the points of different types

plot(x, type="o")  # add lines between points

plot of chunk unnamed-chunk-7

plot(x, type="b")  # add lines between points without touching the points

plot of chunk unnamed-chunk-8

xlab specifies the label of the x axis

plot(x, type="o", xlab="this is the x label")

plot of chunk unnamed-chunk-9

plot(x, type="o", xlab="")  # removes the x lable

plot of chunk unnamed-chunk-10

points(): add additional points to an existing plot

plot(x, type="o", xlab="")  # removes the x lable
points(y, col=2)      # add points y in red (col=2)

plot of chunk unnamed-chunk-11

lines(): add points and lines to an existing plot

plot(x, type="o", xlab="")  # removes the x lable
lines(y, type="o", col=2)

plot of chunk unnamed-chunk-12

ylim: define the limits of y axis

plot(x, type="o", xlab="", ylim=c(-3, 3))
lines(y, type="o", col=2)

plot of chunk unnamed-chunk-13

range: range(x,y)=(min(x,y), max(x,y)) among two samples x and y

range(x,y)
## [1] -1.265061  2.096540
plot(x, type="o", xlab="", ylim=range(x,y))
lines(y, type="o", col=2)

plot of chunk unnamed-chunk-14

Dot plot

dotchart provides a dot plot of a numerical variable. A dot plot is similar to a line chart but the values of the variable are in the horizontal axis and the indeces in the vertical axis

dotchart(x, labels=i)

plot of chunk unnamed-chunk-15

The following dot plot provides a graphical representation of the ranking of variable x

dotchart(x[order(x)], labels=order(x))

plot of chunk unnamed-chunk-16

Histograms

hist(): The histogram is one of the main important plots of a numerical variable

hist(x)  # title and x label are included by default

plot of chunk unnamed-chunk-17

hist(x, col=5)

plot of chunk unnamed-chunk-18

Boxplot

boxplot()

boxplot(x)
title("boxplot of x", ylab="x")

plot of chunk unnamed-chunk-19

boxplot(x, horizontal=T)
title("boxplot of x", xlab="x")

plot of chunk unnamed-chunk-20

This is an example of a plot containing both a histogram and a boxplot:

hist(x,main='histogram and boxplot of x',xlab='x')

plot of chunk unnamed-chunk-21

ylim: range of the y axis

we need to extend the y axis in order to make room for the boxplot

hist(x,main='histogram and boxplot of x',xlab='x',ylim=c(0,12))

plot of chunk unnamed-chunk-22

add=T : this allows the addition of the boxplot in the histogram

axes=F: we remove the axes of the boxplot

hist(x,main='histogram and boxplot of x',xlab='x',ylim=c(0,12))
boxplot(x,horizontal=TRUE,at=10,add=TRUE,axes=FALSE)

plot of chunk unnamed-chunk-23

boxwex: specifies the width of the box

hist(x,main='histogram and boxplot of x',xlab='x',ylim=c(0,12))
boxplot(x,horizontal=TRUE,at=10,add=TRUE,axes=FALSE, boxwex = 5)

plot of chunk unnamed-chunk-24

Scatter plots

plot() applied to two variables provides a scatter plot

y<-x^2   # Define y as the squared values of x
plot(x,y)

plot of chunk unnamed-chunk-25

plot(x,y, main="scatter plot x and y")

plot of chunk unnamed-chunk-26

Multiple Data Sets on One Plot

points(): add additional points to an existing plot

set.seed(123)
x<-rnorm(15,0,1)
y<-x^2
plot(x,y)

plot of chunk unnamed-chunk-27

plot(x,y)
x1 <- c(-1,1)  # we add points x=1 and x=-1
y1 <- x1^2
points(x1,y1,col=2)

plot of chunk unnamed-chunk-28

plot(x,y)
points(x1,y1,col=2, pch=3) # symbol=plus

plot of chunk unnamed-chunk-29

We add lables to the points

plot(x,y)
points(x1,y1,col=2, pch=3) # symbol=plus
text(x1+0.1, y1+0.2, col=2, c("A", "B"))

plot of chunk unnamed-chunk-30

plot(x,y)
points(x1,y1,col=2, pch=3) # symbol=plus

plot of chunk unnamed-chunk-31

legend: x and y coordinates on the plot to place the legend followed by a list of labels to use

plot(x,y)
legend(-1,2,c("Original","new"),col=c(1,2),pch=c(1,4))

plot of chunk unnamed-chunk-32

col.main: specifies the color of the title

plot(x,y)
points(x1,y1,col=2, pch=4) # symbol=star
legend(-1,3,c("Original","new"),col=c(1,2),pch=c(1,4))
title("scatter plot", col.main=3)

plot of chunk unnamed-chunk-33

font.main: specifies the font of the title

plot(x,y)
points(x1,y1,col=2, pch=4) # symbol=star
legend(-1,3,c("Original","new"),col=c(1,2),pch=c(1,4))
title("scatter plot", font.main=3)

plot of chunk unnamed-chunk-34

cex(): Proportion of reduction or amplification of a font

plot(x,y)
points(x1,y1,col=2, pch=4) # symbol=star
legend(-1,3,c("Original","new"),col=c(1,2),pch=c(1,4), cex=0.7)
#font.main: specifies the font of the title
title("scatter plot", font.main=3)

plot of chunk unnamed-chunk-35

axes=F: remove axes

axis(): define new axes

plot(x,y,axes=FALSE)
points(x1,y1,col=2, pch=4) # symbol=star
axis(1,pos=c(-0.5,0),at=seq(-2,2,by=0.4))
axis(2,pos=c(-1.5,-0.5),at=seq(0,3,by=0.5))

plot of chunk unnamed-chunk-36

ann=F: removes annotation

lab: define new lables

plot(x,y,axes=FALSE, ann=F)
points(x1,y1,col=2, pch=4) # symbol=star
axis(1,pos=c(-0.5,0),at=seq(min(x),max(x),by=0.8), lab=c("a", "b", "c", "d"))
axis(2,pos=c(-1.5,-0.5),at=seq(0,3,by=0.5))

plot of chunk unnamed-chunk-37

abline(): abline(a,b) adds line y=a+bx

lty: type of line

plot(x,y,axes=FALSE, ann=F)
points(x1,y1,col=2, pch=4) # symbol=star
axis(1,pos=c(-0.5,0),at=seq(min(x),max(x),by=0.8), lab=c("a", "b", "c", "d"))
axis(2,pos=c(-1.5,-0.5),at=seq(0,3,by=0.5))
abline(1, -0.7, lty=3)

plot of chunk unnamed-chunk-38

box(): creates a box around the plot

plot(x,y,axes=FALSE, ann=F)
points(x1,y1,col=2, pch=4) # symbol=star
axis(1,pos=c(-0.5,0),at=seq(min(x),max(x),by=0.8), lab=c("a", "b", "c", "d"))
axis(2,pos=c(-1.5,-0.5),at=seq(0,3,by=0.5))
abline(1, -0.7, lty=3)
box()

plot of chunk unnamed-chunk-39

Multiple scatter plots

pairs(): Scatter plots of all pair of variables in a data frame

set.seed(123)
data<-data.frame(x1=runif(10), x2=runif(10), x3=runif(10), x4=runif(10))
pairs(data)

plot of chunk unnamed-chunk-40

Bar charts

barplot()

group<-c("A","B","C")
freq<-c(20, 50, 30)
barplot(freq)

plot of chunk unnamed-chunk-41

names.arg

barplot(freq, names.arg=group)

plot of chunk unnamed-chunk-42

density

barplot(freq, names.arg=group, density=c(5,30,70))

plot of chunk unnamed-chunk-43

border

barplot(freq, names.arg=group, density=c(5,30,70), border=3)

plot of chunk unnamed-chunk-44

Multiple bar plot

group<-c("A","B","C")
freq1<-c(20, 50, 30)
freq2<-c(40, 20, 10)
freq<-rbind(freq1,freq2)
freq
##       [,1] [,2] [,3]
## freq1   20   50   30
## freq2   40   20   10

names.arg

barplot(freq, names.arg=group)

plot of chunk unnamed-chunk-46

barplot(freq, names.arg=group, col=c(3,4))

plot of chunk unnamed-chunk-47

beside

barplot(freq, names.arg=group, beside=TRUE)

plot of chunk unnamed-chunk-48

barplot(freq, names.arg=group, beside=TRUE, col=c(2,3))

plot of chunk unnamed-chunk-49

Important graphical functions and parameters

par(): set graphical parameters

Before you change the graphical parameters it is convenient to store the default values

defaultpar<-par()

Example:

par(mfrow=c(2,3)) # puts 6 pictures in a plot distributed in 2 rows and 3 columns
hist(data$x1, main="Histogram x1")
hist(data$x2, main="Histogram x2")
hist(data$x3, main="Histogram x3")
boxplot(data$x1, main="Boxplot x1")
boxplot(data$x2, main="Boxplot x2")
boxplot(data$x3, main="Boxplot x3")

plot of chunk unnamed-chunk-51

par(defaultpar)  # reset the default graphical parameters

Important arguments:

mfrow: number of pictures per row and column in a plot

mar: specifies the margin sizes around the plotting area in order: c(bottom, left, top, right)

col: color of symbols

pch: type of symbols, samples: example(points)

lwd: size of symbols

cex.*: control font sizes

Creating a symbols and color chart

Make an empty chart

plot(1, 1, xlim=c(1,5.5), ylim=c(0,7), type="n", ann=FALSE)

plot of chunk unnamed-chunk-53

Plot digits 0-4 with increasing size and color

plot(1, 1, xlim=c(1,5.5), ylim=c(0,7), type="n", ann=FALSE)
text(1:5, rep(6,5), labels=c(0:4), cex=1:5, col=1:5)

plot of chunk unnamed-chunk-54

Plot symbols 0-4 with increasing size and color

plot(1, 1, xlim=c(1,5.5), ylim=c(0,7), type="n", ann=FALSE)
text(1:5, rep(6,5), labels=c(0:4), cex=1:5, col=1:5)

points(1:5, rep(5,5), cex=1:5, col=1:5, pch=0:4)
text((1:5)+0.4, rep(5,5), cex=0.6, (0:4))

plot of chunk unnamed-chunk-55

Plot symbols 5-9 with labels

plot(1, 1, xlim=c(1,5.5), ylim=c(0,7), type="n", ann=FALSE)
text(1:5, rep(6,5), labels=c(0:4), cex=1:5, col=1:5)
points(1:5, rep(5,5), cex=1:5, col=1:5, pch=0:4)
text((1:5)+0.4, rep(5,5), cex=0.6, (0:4))

points(1:5, rep(4,5), cex=2, pch=(5:9))
text((1:5)+0.4, rep(4,5), cex=0.6, (5:9))

plot of chunk unnamed-chunk-56

Plot symbols 10-14 with labels

plot(1, 1, xlim=c(1,5.5), ylim=c(0,7), type="n", ann=FALSE)
text(1:5, rep(6,5), labels=c(0:4), cex=1:5, col=1:5)
points(1:5, rep(5,5), cex=1:5, col=1:5, pch=0:4)
text((1:5)+0.4, rep(5,5), cex=0.6, (0:4))
points(1:5, rep(4,5), cex=2, pch=(5:9))
text((1:5)+0.4, rep(4,5), cex=0.6, (5:9))

points(1:5, rep(3,5), cex=2, pch=(10:14))
text((1:5)+0.4, rep(3,5), cex=0.6, (10:14))

plot of chunk unnamed-chunk-57

Plot symbols 15-19 with labels

plot(1, 1, xlim=c(1,5.5), ylim=c(0,7), type="n", ann=FALSE)
text(1:5, rep(6,5), labels=c(0:4), cex=1:5, col=1:5)
points(1:5, rep(5,5), cex=1:5, col=1:5, pch=0:4)
text((1:5)+0.4, rep(5,5), cex=0.6, (0:4))
points(1:5, rep(4,5), cex=2, pch=(5:9))
text((1:5)+0.4, rep(4,5), cex=0.6, (5:9))
points(1:5, rep(3,5), cex=2, pch=(10:14))
text((1:5)+0.4, rep(3,5), cex=0.6, (10:14))

points(1:5, rep(2,5), cex=2, pch=(15:19))
text((1:5)+0.4, rep(2,5), cex=0.6, (15:19))

plot of chunk unnamed-chunk-58

Plot symbols 20-25 with labels

plot(1, 1, xlim=c(1,5.5), ylim=c(0,7), type="n", ann=FALSE)
text(1:5, rep(6,5), labels=c(0:4), cex=1:5, col=1:5)
points(1:5, rep(5,5), cex=1:5, col=1:5, pch=0:4)
text((1:5)+0.4, rep(5,5), cex=0.6, (0:4))
points(1:5, rep(4,5), cex=2, pch=(5:9))
text((1:5)+0.4, rep(4,5), cex=0.6, (5:9))
points(1:5, rep(3,5), cex=2, pch=(10:14))
text((1:5)+0.4, rep(3,5), cex=0.6, (10:14))
points(1:5, rep(2,5), cex=2, pch=(15:19))
text((1:5)+0.4, rep(2,5), cex=0.6, (15:19))

points((1:6)*0.8+0.2, rep(1,6), cex=2, pch=(20:25))
text((1:6)*0.8+0.5, rep(1,6), cex=0.6, (20:25))

plot of chunk unnamed-chunk-59

Saving Graphics to Files

pdf() redirect the plots to a pdf file. Similarly: jpeg, png, ps, tiff

dev.off() shuts down the specified device

Example:

pdf("color_chart.pdf")  # creates a pdf file containing the following plot, a color chart

plot(1, 1, xlim=c(1,5.5), ylim=c(0,7), type="n", ann=FALSE)
text(1:5, rep(6,5), labels=c(0:4), cex=1:5, col=1:5)
points(1:5, rep(5,5), cex=1:5, col=1:5, pch=0:4)
text((1:5)+0.4, rep(5,5), cex=0.6, (0:4))
points(1:5, rep(4,5), cex=2, pch=(5:9))
text((1:5)+0.4, rep(4,5), cex=0.6, (5:9))
points(1:5, rep(3,5), cex=2, pch=(10:14))
text((1:5)+0.4, rep(3,5), cex=0.6, (10:14))
points(1:5, rep(2,5), cex=2, pch=(15:19))
text((1:5)+0.4, rep(2,5), cex=0.6, (15:19))

points((1:6)*0.8+0.2, rep(1,6), cex=2, pch=(20:25))
text((1:6)*0.8+0.5, rep(1,6), cex=0.6, (20:25))

dev.off()

Exercices

Create the following plots with R using data from “SNPdataset.txt”:

plot of chunk unnamed-chunk-61plot of chunk unnamed-chunk-61plot of chunk unnamed-chunk-61plot of chunk unnamed-chunk-61