Introduction to R - 4th lesson (graphics)

Nathalie Villa-Vialaneix - http://www.nathalievialaneix.eu
September 14-16th, 2015

Master TIDE, Université Paris 1

R

Introduction

R

  • basic concepts
  • graphics parameters
  • graphics devices

Basic concepts on making a plot

  • When making a plot, the user must first choose to what device the plot is sent (a new window, a png file, a postscript file,…) and set the graphic parameters (with par)
  • A plot is made in several steps:
  1. first, a high level graphic function is called (plot, boxplot, pie…) that initiates the graphic;
  2. then, one or several (or even none) low level graphic functions are called (legend, points, lines…) to add elements to the graphic.

Eventually, the graphic can be exported in a file (png, postscript, pdf, …) after.

Graphics parameters

data(airquality)
par(bg="lightyellow", mfcol=c(1,2), pch="+")
plot(airquality$Ozone)
plot(factor(airquality$Month))

plot of chunk parGraphics

Graphics parameters

Important graphics parameters include:

  • pch: the plotting symbol (default is an open circle)
  • lty: the line type (default is a solide line)
  • lwd: the line width, specified as an integer multiple
  • col: the plotting color (default is black)
  • las: the orientation of the axis label on the plot (default is 0, which corresponds to labels parallel to the axes, 1 is for horizontal labels, 2 for labels perpendicular to the axes and 3 to vertical labels)
  • bg: the background color (default: white)

Graphics parameters

Important graphics parameters include:

  • mar: the margin size (default is c(5,4,4,2), bottom, left, top, right)
  • oma: the outer margin size (default is 0)
  • mfrow: number of plots per row, column (plots are filled row-wise)
  • mfcol: number of plots per row, column (plots are filled col-wise)

Graphics device

By default graphics are shown on screen but this can be changed using a specific command like png, svg, pdf… Available graphics devices can be found using ?Device.

png("myFistGraphic.png", width=600,
    height=600)
par(bg="transparent", lty=2, lwd=2, 
    col="darkred")
plot(airquality$Wind, airquality$Ozone)
dev.off() # close the graphics device

Exporting a graphic

A graphics made on the screen can be exported in a graphic device (but this is not an exact operation)

par(bg="transparent", lty=2, lwd=2,
    mar=rep(2,4), col="darkred")
plot(airquality$Wind, airquality$Ozone)

plot of chunk exportGraphics2

dev.print(png, file="mySecondGraphic.png",
          width=600, height=600)
png 
  2 

Generic function plot

R

  • numeric/non numeric variables
  • one or two variables

plot for one numeric variable

This function displays the value of the variable versus its index, as a scatterplot (default) or a line (type="l"):

plot(airquality$Ozone, type="l")

plot of chunk plot1NumVar

… meaningful only for e.g., time series.

plot for one numeric variable

The standard graphics paramaters can be passed to the function (see also argument type in help):

plot(airquality$Ozone, main="Ozone",
     type="b", pch=19, col="darkred")

plot of chunk plot1NumVar2

plot for one non numeric variable

This function displays the barplot of the distribution of the variable:

plot(factor(airquality$Month), main="Month",
     col="pink", lwd=2, las=3, 
names=c("May","June","July","Aug.","Sept"))

plot of chunk plot1nonNumVar

plot for two numeric variables

This function displays the value of the first variable versus the value of the second variable.

par(mfrow=c(1,2))
plot(airquality$Wind, airquality$Ozone)
plot(airquality$Ozone~airquality$Wind)

plot of chunk plot2NumVar

plot for two variables: one numeric and one non numeric

This function has different behaviors depending on its syntax:

par(mfrow=c(1,2))
plot(airquality[,3], factor(airquality[,5]))
plot(factor(airquality[,5]), airquality[,3])

plot of chunk plot2nonNumNumVar

plot for two variables: one numeric and one non numeric

par(mfrow=c(1,2))
plot(airquality[,3]~factor(airquality[,5]))
plot(factor(airquality[,5])~airquality[,3],
col=rainbow(nlevels(factor(airquality[,5]))))

plot of chunk plot2nonNumNumVar2

plot for two non numeric variables

This function displays a barplot of the cross distribution (alternative syntax with ~ also usable)

plot(factor(airquality[,5]),
     factor(airquality[,1]>30), col=c(3,2),
     xlab="Month", ylab="Bad quality")

plot of chunk plot2nonNumVar

High level graphical functions

R

  • boxplot
  • histogram
  • barplot
  • pie

Boxplot (one numeric variable)

boxplot(airquality$Wind, ylab="Wind speed",
        main="Wind speed distribution")

plot of chunk boxplot1

Conditional boxplot (numeric vs non numeric variables)

boxplot(airquality$Wind~
          factor(airquality$Month),
        ylab="Wind speed", 
names=c("May","June","July","Aug.","Sept."))

plot of chunk boxplot2

Histogram (one numeric variable)

Breakpoints are given as a number or as a vector; areas are proportional to the counts.

par(mfrow=c(1,2))
hist(airquality$Ozone, breaks=20)
hist(airquality$Ozone, col="pink",
     breaks=c(0,10,20,40,60,80,100,170))

plot of chunk hist1

Barplot (one non numeric variable)

barplot is used on the results of the function table:

barplot(table(airquality$Month),
        ylab="Frenquency", col="lightblue")

plot of chunk barplot1

Barplot (two non numeric variables)

barplot(table(airquality$Ozone>50,
              airquality$Month),
        ylab="Frenquency",
        col=c("lightgreen", "pink"))

plot of chunk barplot2

Conditional barplot (two non numeric variables)

CT <- table(airquality$Ozone>50,
            airquality$Month)
barplot(sweep(CT, 2, colSums(CT), "/"),
        ylab="Frenquency",
        col=c("lightgreen", "pink"))

plot of chunk barplot2b

Pie chart (one non numeric variable)

pie is used on the results of the function table:

pie(table(airquality$Month), col=rainbow(5),
    main="Months",
    labels=paste(round(table(airquality$Month)/
      nrow(airquality),2),"%"))

plot of chunk pie1

Low level graphical functions

R

  • lines and segments
  • points
  • texts and legends

Adding straight lines

plot(airquality$Ozone, type="h")
abline(h=50, col="darkred", lty=2, lwd=2)
abline(v=50, col="darkblue", lwd=2)

plot of chunk vhLines

Adding straight lines

plot(airquality$Ozone~airquality$Wind)
abline(0, 1, col="darkgreen")

plot of chunk vhLines2

Adding lines

plot(airquality$Wind[1:100], col="darkred",
     type="l")
lines(25:78, airquality$Wind[100:153],
      col="darkgreen")

plot of chunk lines

Adding segments

plot(airquality$Ozone~airquality$Wind)
segments(airquality[1:5,3],
         airquality[1:5,1],
         airquality[141:145,3],
         airquality[141:145,1], col="darkred")

plot of chunk segments

Adding arrows

plot(airquality$Ozone~airquality$Wind)
arrows(airquality[1:5,3],
       airquality[1:5,1],
       airquality[141:145,3],
       airquality[141:145,1],
       col="darkred")

plot of chunk arrows

Adding points

plot(airquality$Ozone, type="l")
points(airquality$Wind, pch=19)

plot of chunk points

Adding points

plot(airquality$Temp~airquality$Wind,
     pch=19)
points(airquality$Wind, airquality$Ozone, 
       col="darkred", pch=19)

plot of chunk points2

Adding points (adjusting y range)

plot(airquality$Temp~airquality$Wind,
     pch=19,
     ylim=range(airquality$Ozone,
                na.rm=TRUE))
points(airquality$Wind, airquality$Ozone, 
       col="darkred", pch=19)

plot of chunk points3

Adding texts

plot(airquality$Temp, type="l")
text(90, 70, "It's much\n too hot",
     cex=3, col="darkred")
text(0, 90, "Too cold...", col="blue", cex=3,
     pos=4) # pos=4 <=> "to the right of"

plot of chunk text

Adding legends

barplot(table(factor(airquality$Ozone>50),
              factor(airquality$Month)),
        col=c(3,2))
legend(1.5, 20, cex=2, pch=15, 
       legend=c("good","bad"), col=c(3,2))

plot of chunk legend

Adding legends (second graphic)

par(mfrow=c(1,2))
barplot(table(factor(airquality$Ozone>50),
              factor(airquality$Month)),
        col=c(3,2))
plot.new()
legend("center", cex=4, pch=15, 
       legend=c("good","bad"), col=c(3,2))

plot of chunk legend2

Exercise 1 (1/4)

data(CO2)

How to make the following graphics? plot of chunk ex1-boxplot

Exercise 1 (2/4)

plot of chunk ex1-scatter

Exercise 1 (3/4)

plot of chunk ex1-barplot

Exercise 1 (4/4)

plot of chunk ex1-condBox