- High-level graphics
- Custom graphics
- Layered graphics in
ggplot2
ggplot2
hist()
, boxplot()
, plot()
, points()
, lines()
, text()
, mtext()
, axis()
, etc. form a suite that plot graphs and add features to the graphpar()
can be used to set or query graphical parametersx <- state.x77[, 2] # 50 average state incomes in 1977 hist(x)
hist(x, breaks = 8, col="pink", xlab="Income", main="Histogram of State Income in 1977")
## Univariate data: Histogram with a density plot
hist(x, breaks = 8, col="pink", freq = FALSE, xlab="Income", main="Histogram of State Income in 1977") lines(density(x), lwd=3)
y <- quakes$depth # 1000 earthquake depths hist(y, seq(0, 700, by = 70), xlab="Earthquake Depth", main="Histogram of Earthquake Depths")
y <- quakes$depth # 1000 earthquake depths hist(y, seq(0, 1000, by = 70), freq = FALSE, col = "pink", xlab="Earthquake Depth", main="Histogram of Earthquake Depths") hist(y+ 200, seq(0, 1000, by = 70), freq = FALSE, col = rgb(0,0.5, 0.5, 0.5), add = TRUE)
Function plot.ecdf()
provides data for empirical cdf
plot.ecdf(x)
Can add vertical lines and remove dots
plot.ecdf(x, verticals = T, pch = "", xlab = "Income", main = "ECDF of State Income in 1977")
plot.ecdf(y, verticals = T, pch = "", xlab = "Earthquake Depth", main = "ECDF of Earthquake Depths")
qqnorm()
and qqplot()
qqnorm()
plots the quantiles of a data set against the quantiles of a Normal distributionqqplot()
plots the quantiles of a first data set against the quantiles of a second data setqqnorm()
and qqplot()
qqnorm(x) # qq plot for the earthquake depths qqline(x, col = "red") # red reference line
qqnorm()
and qqplot()
qqnorm(y, pch=20) # qq plot for the earthquake depths qqline(y, col = "red") # red reference line
qqnorm()
and qqplot()
x <- rexp(1000) y <- rexp(1000) qqplot(x,y) abline(a=0,b=1)
boxplot(count ~ spray, data = InsectSprays)
plot(x, y)
plot(quakes$long, quakes$lat, xlab="Latitude", ylab="Longitude", main="Location of Earthquake Epicenters")
plot(x, y)
symbols(quakes$long, quakes$lat, circles = 10 ^ quakes$mag, xlab="Latitude", ylab="Longitude", main="Location of Earthquake Epicenters")
pairs(x)
pairs(trees)
contour()
contour(crimtab, main="Contour Plot of Criminal Data")
image()
image(crimtab, main="Image Plot of Criminal Data")
phi <- dnorm(seq(-2,2,length=50)) normal.mat <- phi %o% phi image(normal.mat)
image(normal.mat, col=heat.colors(20))
image(normal.mat, col=topo.colors(20))
image(normal.mat, col=terrain.colors(20)) contour(normal.mat, add = TRUE)
persp(crimtab, theta=30, main="Perspective Plot of Criminal Data")
pie.sales = c(0.12, 0.30, 0.26, 0.16, 0.04, 0.12) names(pie.sales) = c("Blueberry", "Cherry", "Apple", "Boston Creme", "Other", "Vanilla Creme") pie(pie.sales, col = c("blue", "red", "green", "wheat", "orange", "white"))
dotchart()
and barplot()
also available
barplot(VADeaths, beside = T, legend = T, main = "Virginia Death Rates per 1000 in 1940")
ts.plot(AirPassengers, xlab="Date", ylab="Passengers (in thousands)", main="International Airline Passengers")
ts.plot(presidents, xlab="Date", ylab="Approval Rating", main="Presidential Approval Ratings")
par()
can be used to set or query graphical parametersadj
: text justificationbg
: background colorcol
, col.axis
, col.lab
, …: color specificationlty
: line type, e.g. dashed, dotted, solid (default), longdash, …lwd
: line width (helpful to increase for presentation plots)mfcol
and mfrow
: subsequent figures will be drawn in an nr-by-nc array on the devicepch
: point typesxlog
: plots to log scale if TRUE
Plot of binomial distribution with \(n=5\) and \(p=.4\)
x <- 0:5 y <- dbinom(x, 5, 2 / 5) plot(x, y, type = "h", main="Binomial Distribution", xlab="Value", ylab="Probability")
Probability density function for the standard Normal distribution from -3 to 3
x <- seq(-3, 3, by = 0.01) y <- dnorm(x) plot(x, y, type = "l", main="Normal Distribution", ylab="f(x)")
Puromycin
datasetx <- Puromycin$rate[Puromycin$state == "treated"] y <- Puromycin$rate[Puromycin$state == "untreated"]
Puromycin
datasetplot.ecdf(x, verticals = TRUE, pch = "", xlim = c(60, 200), main="Treated versus Untreated") lines(ecdf(y), verticals = TRUE, pch = "", xlim = c(60, 200), col="blue") legend("bottomright", c("Treated", "Untreated"), pch = "", col=c("black", "blue"), lwd = 1)
postscript()
, pdf()
, tiff()
, jpeg()
, …dev.off()
pdf("2cdfs.pdf", width=6, height=4) plot.ecdf(x, verticals = TRUE, pch = "", xlim = c(60, 200), main="Treated versus Untreated") lines(ecdf(y), verticals = TRUE, pch = "", xlim = c(60, 200), col="blue") legend("bottomright", c("Treated", "Untreated"), pch = "", col=c("black", "blue"), lwd = 1) dev.off()
## png ## 2
x <- seq(0, 2 * pi, length = 100) sine <- sin(x) cosine <- cos(x) matplot(x, cbind(sine, cosine), col = c(1, 1), type = "l")
par(mfrow = c(2, 2)) boxplot(precip) hist(precip) plot.ecdf(precip) qqnorm(precip)
par(mfrow = c(1, 1))
plot(rate ~ conc, data = Puromycin, pch = 15 * (state == "treated") + 1) legend("bottomright", legend = c("Untreated", "Treated"), pch = c(1, 16))
persp()
for wire meshx <- seq(-8, 8, length = 100) y <- x f <- function(x, y) sin(sqrt(x ^ 2 + y ^ 2)) / (sqrt (x ^ 2 + y ^ 2)) z <- outer(x, y, f) persp(x, y, z, xlab = "", ylab = "", zlab = "", axes = F, box = F)