R graphics : base graphics

  1. Introduction
  2. Plot types
  3. Focus on the plot() function
  4. Inside a plot object
  5. Customizing plots
  6. Interactivity
  7. Combining plots
  8. Exporting plots to files

Introduction

The 'base' graphic system uses an incremental approach, where the different elements of a graph can be added and drawn on top of each other. One can draw elements one after another but not remove or change elements.
It follows a simple model, where there is no internal representation of the graphics, apart from the actual drawing.

set.seed(1)
x <- runif(50)
y <- 2 * x + 0.5 + rnorm(length(x))/5
groups <- ifelse(y < 1, 1, 2)

par(las = 1)
plot(x, y, col = c("red", "blue")[groups], pch = c(1, 2)[groups], xlab = "X values", 
    ylab = "Y values")
abline(lm(y ~ x))
abline(h = 1, lty = 2)
rug(x)
rug(y, side = 2)
text(0, 2, paste("Correlation = ", round(cor(x, y), 2)), pos = 4)
legend(x = "top", legend = c("Y < 1", "Y > 1"), pch = c(1, 2), col = c("red", 
    "blue"))

plot of chunk unnamed-chunk-1

See this page for a more complete overview of all the possibilities offered by R 'base' : R Graphics by Paul Murrell.


Plot types

Many plot types exist.

set.seed(1)
x <- runif(50)
y <- 2 * x + 0.5 + rnorm(length(x))/5
plot(x, main = "1D Scatterplot")
plot(x, y, main = "2D Scatterplot")
boxplot(x, main = "Boxplot")
hist(x, breaks = 10, main = "Histogram")

plot of chunk unnamed-chunk-2


Focus on the plot() function

Different types of drawing

Point display can change by using different values for parameter 'type'.

set.seed(1)
x <- rnorm(100, mean = 0, sd = 1.5)
plot(x, type = "p", main = "type = p")
plot(x, type = "l", main = "type = l")
plot(x, type = "b", main = "type = b")
plot(x, type = "h", main = "type = h")

plot of chunk unnamed-chunk-3

High density scatter plots

One can use non-filled point characters, act on the alpha value color points (transparency), or use smoothScatters().

a <- rnorm(10000)
b <- rnorm(10000)
oldpar <- par()
par(mfrow = c(1, 3))
plot(a, b)
plot(a, b, col = "#00880022", pch = 16)
smoothScatter(a, b)

plot of chunk unnamed-chunk-4

## Generates warnings (some parameters can't be edited)
par(oldpar)

Adaptability of plot()

plot is an S3 function. It can adapt to the object provided if there is a corresponding function.

Example of the plot.lm function generating multiple plots:

set.seed(1)
x <- runif(50)
y <- 2 * x + 0.5 + rnorm(length(x))/5
plot(lm(y ~ x))

plot of chunk unnamed-chunk-5

If more than 2 data columns are provided, multiple scatterplots are automatically displayed by plot().

set.seed(1)
x <- runif(50)
y <- 2 * x + 0.5 + rnorm(length(x))/5
z <- 1.33 * x - 0.25 + rnorm(length(x))/6
plot(data.frame(x, y, z))

plot of chunk unnamed-chunk-6


Inside a plot object

If plots are not displayed, the plotting function returns textual information about the graph. In the case of barplot, it'll be the X central position of the bars.

set.seed(1)
data <- runif(10)
barplot(data)
axis(side = 1, at = barplot(data, plot = FALSE))

plot of chunk unnamed-chunk-7

barplot(data, plot = FALSE)
##       [,1]
##  [1,]  0.7
##  [2,]  1.9
##  [3,]  3.1
##  [4,]  4.3
##  [5,]  5.5
##  [6,]  6.7
##  [7,]  7.9
##  [8,]  9.1
##  [9,] 10.3
## [10,] 11.5

Plot objects can be stored in variables just like any other object. They are effectively returned by the function calls, but as invisible objects (if not used, printed, stored, they are not visible in the workspace).

set.seed(1)
x <- runif(50)
y <- runif(50)
bp <- boxplot(x, y, names = c("a", "b"))

plot of chunk unnamed-chunk-8

bp
## $stats
##         [,1]    [,2]
## [1,] 0.01339 0.05893
## [2,] 0.34035 0.31627
## [3,] 0.56294 0.45717
## [4,] 0.76984 0.76631
## [5,] 0.99191 0.96062
## 
## $n
## [1] 50 50
## 
## $conf
##        [,1]   [,2]
## [1,] 0.4670 0.3566
## [2,] 0.6589 0.5577
## 
## $out
## numeric(0)
## 
## $group
## numeric(0)
## 
## $names
## [1] "a" "b"

Customizing plots

Graphics functions are the R functions with the most parameters… Help on all functions as well as the list of all possible parameters can be accessed by the usual R help system.

`?`(plot)

asp

Aspect ratio between X and Y axis.

cex

Character extension, to increase/decrease text size. Different sub parameters allow to be more precise on what should change exactly.

col

Colors used to display graph elements, black being the default color.

set.seed(1)
a <- rnorm(10000)
b <- rnorm(10000)
plot(a, b)

plot of chunk unnamed-chunk-10

plot(a, b, pch = 16, col = "blue")

plot of chunk unnamed-chunk-10

One can also provide a vector of colors. If the number of colors is not sufficient for the data length, colors will be recycled.

set.seed(1)
a <- rnorm(10000)
b <- rnorm(10000)
myColors <- ifelse((a > 0 & b > 0), "red", "blue")
myColors[b < -2] <- "green"
plot(a, b, pch = 16, col = myColors)

plot of chunk unnamed-chunk-11

It can be useful to set the graph's frame color to light gray, so that it's less visible than the points themselves, or more generally, to change the background color.

  • To remove the plot border : bty=“n”
  • To change background/border color : plot, add a rectangle around the plot frame, and draw the points.
set.seed(1)
a <- rnorm(10000)
b <- rnorm(10000)
myColors <- ifelse((a > 0 & b > 0), "red", "blue")
myColors[b < -2] <- "green"
plot(x = NULL, bty = "n", xlab = "Index", ylab = "X values", xlim = c(-4, 4), 
    ylim = c(-4, 4))
rect(xleft = par("usr")[1], ybottom = par("usr")[3], xright = par("usr")[2], 
    ytop = par("usr")[4], col = "white", border = "gray")
rect(xleft = par("usr")[1] + 0.1, ybottom = par("usr")[3] + 0.1, xright = par("usr")[2] - 
    0.1, ytop = par("usr")[4] - 0.1, col = "#E9ECF7", border = "white")
points(a, b, pch = 16, col = myColors, xlim = c(-4, 4), ylim = c(-4, 4))

plot of chunk unnamed-chunk-12

lab

Position of the axis ticks.

mai/mar and omi/oma

Sets the margins or outer margins around the plot in the c(bottom, left, top, right) format.

pch

Character used for each point. By default, an empty circle.

a <- rnorm(10000)
b <- rnorm(10000)
plot(a, b)
plot(a, b, pch = 16)

plot of chunk unnamed-chunk-13

Other parameters

  • par(“usr”) get the actual size of a panel

Interactivity

identify() allows manual selection of a set of points.

x <- runif(50)
y <- runif(50)
plot(x, y)
manualSelection <- identify(x, y, LETTERS)
selectedElements <- cbind(x[manualSelection], y[manualSelection])
selectedElements

locator() returns position of mouse clicks in the plot's coordinate system.

See also package Shiny.


Combining plots

Combining in the same drawing area

set.seed(1)
data <- rnorm(100)
hist(data, main = "", xlim = c(-3, 3), probability = TRUE, col = "darkred")
lines(density(data), col = "blue", lwd = 2)

plot of chunk unnamed-chunk-15

Laying out multiple plots side by side

(Those systems are incompatible, use only one at a time.)

par(mfrow=c(x,y))

par(mfrow=c(x,y)) or par(mfcol=c(x,y)) fill by row (then columns) or by column (then by row).

oldpar <- par()
par(mfrow = c(1, 3))
set.seed(1)
data <- rnorm(100)
hist(data, main = "", col = "darkgreen", xlim = c(-3, -1))
hist(data, main = "", col = "gray", xlim = c(-1, 1))
hist(data, main = "", col = "darkred", xlim = c(1, 3))

plot of chunk unnamed-chunk-16

## Reset parameters (generates warnings as some of them are not editable)
par(oldpar)

layout()

Divides the graphical space into regions specified by a matrix.
plot.new() switches from one location to another, when a space needs to stay empty.

layout(matrix(c(1, 1, 2, 3), ncol = 2, byrow = TRUE))
set.seed(1)
data <- rnorm(100)
hist(data, main = "", xlim = c(-3, 3))
plot.new()
hist(data, main = "", xlim = c(-3, 3))

plot of chunk unnamed-chunk-17

layout(c(1)) resets the layout.

See also


Exporting plots to files

  • jpeg() produces a JPEG file
  • png() produces a PNG file
  • bmp() produces a bmp file
  • pdf() produces a pdf file
  • postscript() produces a postscript file

PDF

Create the pdf “device” where the plots will be written. Parameters called 'width' and 'height' (in inches) controls the available space size.

pdf("filename.pdf")

Any subsequent call to a graphic function will write onto the PDF “device”.

To effectively write the content of the device into a file, end your instructions with : dev.off()


Creative Commons License
This work by Celine Hernandez is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.