An R graphics gallery has been created with simple scripts for long-term reference and to expedite data visualization.
Simple Line Plot
1 2 3 4 5 6 7 8 |
x <- 1:10 y <- c(4, 3, 2, 5, 6, 7, 1, 8, 9, 7) plot(x, y, type = "b", main = "Simple Plot", xlab = "X-axis Title", ylab = "Y-axis Title", col = "red3") |
Bar Plot of Vector
1 2 3 4 5 6 7 8 |
# Bar plot of vector set.seed(1) y.vec <- sample(-10:10,10, rep = TRUE) barplot(y.vec, col = "steelblue4", names = LETTERS[1:10], ylim=c(-10,10)) abline(h = 0) |
Bar Plot of Matrix: #1
1 2 3 4 5 6 7 8 |
# Bar plot of matrix: #1 set.seed(10) y.mat <- matrix(sample(1:10, 60, rep = TRUE), nrow = 6) barplot(y.mat, col = colorRampPalette(c("steelblue4", "green"))(6), names=LETTERS[1:10], ylim=c(0, 50)) abline(h = 0) |
Bar Plot of Matrix: #2
1 2 3 4 5 6 7 8 |
# Bar plot of matrix: #2 barplot(y.mat, col=colorRampPalette(c("steelblue4", "green"))(6), names=LETTERS[1:10], horiz = TRUE, legend = paste("Sensor", 1:6), xlim=c(0,50)) abline(v = 0) |
Box Plot
1 2 3 4 5 6 7 8 |
# Box plot y <- split(rnorm(1000), rpois(1000,2.2)) boxplot(y, col="orange", main="Boxplot", xlab="Poisson Distribution", ylab="Normal Distribution", ylim=c(-3, 3)) |
In order to access the values behind the chart, simply set the boxplot() argument plot=FALSE. A statistical summary is generated.
Contour Plot
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# Contour plot # Generate data rx <- range(x <- 10 * 1:nrow(volcano)) ry <- range(y <- 10 * 1:ncol(volcano)) ry <- ry + c(-1, 1) * (diff(rx) - diff(ry))/2 # Define graph parameters tcol <- colorRampPalette(c("forestgreen", "maroon", "red"))(12) opar <- par(mfrow = c(2, 2), mar = rep(0, 4)) par(opar) opar <- par(pty = "s", bg = "white") u <- par("usr") # Open and configure blank device plot(x = 0, y = 0, type = "n", xlim = rx, ylim = ry, xlab = "", ylab = "") # Add rectangle layer u <- par("usr") rect(u[1], u[3], u[2], u[4], col = "ivory") # Add contours contour(x, y, volcano, col = tcol, lty = "solid", add = TRUE, vfont = c("sans serif", "plain")) # Add plot elements title("Topography of Maunga Whau Volcano", font = 4) abline(h = 200*0:4, v = 200*0:4, col = "black", lty = 2, lwd = 0.2) abline(h = 341, v = 300, col = "blue", lwd = .4) |
Dot Chart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# Dot chart # Configure data x <- mtcars[order(mtcars$mpg), ] x$cyl <- factor(x$cyl) x$color[x$cyl == 4] <- "violetred" x$color[x$cyl == 6] <- "green3" x$color[x$cyl == 8] <- "royalblue" dotchart(x$mpg, labels = row.names(x), cex = 0.85, pch = 16, groups = x$cyl, main = "Gas Milage By # of Cylinders and Model", xlab = "Miles Per Gallon", color = "black", color = x$color) |
Histogram
1 2 3 4 5 6 7 8 9 10 11 12 |
# Histogram set.seed(9) rnormMix <- ifelse(runif(5000) < 0.25, rnorm(5000, 3, 0.5), rnorm(5000)) hist(rnormMix, breaks = 75, freq = FALSE, xaxp = c(-5, 5, 10), col = "steelblue", main = "Histogram With Line Density") lines(density(rnormMix, n = 1000), col = "orange", lwd = 5 ) |
The hist() function returns a useful statistical summary if the argument plot=FALSE is used. The summary defines the breakpoints for the bins and counts per bin. For example:
1 2 3 4 5 |
> hist(Abeline.WSpeed, plot = FALSE) $breaks: [1] 0 2 4 6 8 10 12 14 16 $counts: [1] 1 2 7 4 7 1 1 2 |
The following example shows how bin summary data can be used. Function pretty() takes a vector argument and divides its range into n equally spaced, ordered values, as shown in the histogram below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# Horizontal Histogram # Generate simulated wind speed data set.seed(3) Abilene <- rweibull(1500, shape = 2.00, scale = 6.0) Lubock <- rweibull(1500, shape = 1.90, scale = 9.5) # Summarize observation by WS bin WSbins <- pretty(c(Abilene, Lubock), n = 10) h1 <- hist(Abilene, breaks = WSbins, plot = FALSE)$counts h2 <- hist(Lubock, breaks = WSbins, plot = FALSE)$counts # Generate plot barplot(matrix(c(h1, -h2), ncol = 14, byrow = TRUE), beside = T, space = c(-1,0), col = c("turquoise4", "plum2"), xlim = c(-400, 400), axes = FALSE, horiz = TRUE, legend.text = c("Abilene, TX", "Lubock, TX"), ylab = "December WindSpeed", xlab = "Observation Frequency: abs(x)") axis(1, at = seq(-400, 400, by = 100), line = -1.35) axis(2, at = WSbins) |
Using segments()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
n = 17 theta = seq(0, 2 * pi, length = n + 1)[1:n] x = sin(theta) y = cos(theta) plot.new() plot.window(xlim = c(-1, 1), ylim = c(-1, 1), asp = 1) for (i in 2:n){ for (j in 1:(i-1)){ segments(x[i], y[i], x[j], y[j], col = "magenta4") } } title(main = "A Rosette Plot") |
Using polygon()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# Define data and color ramps x <- c(-1, 1, 1, -1) y <- c(-1, -1, 1, 1) n <- 25 i1 <- 1:4 i2 <- c(2:4, 1) ramp <- colorRamp(c("navy", "palegreen")) c.ramp <- rgb(ramp(seq(0, 1, length = n)), max = 255) # Plot loop plot.new() plot.window(xlim = c(-1, 1), ylim = c(-1, 1), asp = 1) for(i in 1:n){ polygon(x, y, col = c.ramp[i], border="grey50") x = 0.9 * x[i1] + .1 * x[i2] y = 0.9 * y[i1] + .1 * y[i2] } title(main = "A Spiral Square") |
Image Plot
1 2 3 4 5 6 7 8 9 |
# Image chart x <- 10*(1:nrow(volcano)) y <- 10*(1:ncol(volcano)) image(x, y, volcano, col = terrain.colors(100), axes = FALSE) contour(x, y, volcano, levels = seq(90, 200, by = 5), add = TRUE, col = "peru") axis(1, at = seq(100, 800, by = 100)) axis(2, at = seq(100, 600, by = 100)) box() title(main = "Topography of Maunga Whau Volcano") |
Matplot
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# Matplot table(iris$Species) # a data.frame with 'Species' as factor iS <- iris$Species == "setosa" iV <- iris$Species == "versicolor" matplot(c(1, 8), c(0, 4.5), type = "n", xlab = "Length", ylab = "Width", main = "Iris Petal and Sepal Dimensions By Species") u <- par("usr") rect(u[1], u[3], u[2], u[4], col = "grey97") matpoints(iris[iS,c(1,3)], iris[iS,c(2,4)], pch = 16, col = c("skyblue", "green")) matpoints(iris[iV,c(1,3)], iris[iV,c(2,4)], pch = 17, col = c("skyblue4","green4")) abline(h = 0:4, v = 0:8, col = "black", lty = 2, lwd = 0.2) legend(1, 4, c(" Setosa Petals", " Setosa Sepals", "Versicolor Petals", "Versicolor Sepals"), pch = c(16, 17), col = c("skyblue","skyblue4", "green","green4")) |
Pairs Plot
1 2 3 4 5 |
# Pairs plot pairs(iris[1:4], main = "Anderson's Iris Data", pch = 21, bg = c("red", "green2", "steelblue4")[unclass(iris$Species)]) |
Perspective Plot
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# Perspective plot z <- 2 * volcano # Exaggerate the relief x <- 10 * (1:nrow(z)) # 10 meter spacing (S to N) y <- 10 * (1:ncol(z)) # 10 meter spacing (E to W) persp(x, y, z, theta = 135, phi = 30, col = "green2", scale = FALSE, ltheta = -120, shade = 0.75, border = NA, box = FALSE) title(main = "Topography of Maunga Whau Volcano") |
Pie Chart
1 2 |
# Pie chart pie(rep(1, 24), col = rainbow(24), radius = .9) |
QQNorm Plot
1 2 3 4 5 6 7 8 9 10 |
# QQNorm plot y <- rt(200, df = 5) qqnorm(y, pch = 16, col = "steelblue4", bg = "ivory") abline(h = -4:4, v = -4:4, col = "black", lty = 2, lwd = 0.2) qqline(y, col = "green2", lwd=2.5) |