- Basic graphics
- Custom graphics
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")
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")
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"))
barplot(pie.sales, col = c("blue", "red", "green", "wheat", "orange", "white"))
barplot(pie.sales, col = c("blue", "red", "green", "wheat", "orange", "white"), horiz = TRUE)
plot(x, y)
# Simple Scatterplot attach(mtcars) plot(wt, mpg, main="Scatterplot Example", xlab="Car Weight ", ylab="Miles Per Gallon ")
detach(mtcars)
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")
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)
counts <- table(mtcars$vs, mtcars$gear) barplot(counts, main="Car Distribution by Gears and VS", xlab="Number of Gears", col=c("darkblue","red"), legend = rownames(counts))
counts <- table(mtcars$vs, mtcars$gear) barplot(counts, main="Car Distribution by Gears and VS", xlab="Number of Gears", col=c("darkblue","red"), legend = rownames(counts), beside=TRUE)
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")
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)
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 parameters (fonts, colors, axes, titles)col
, col.axis
, col.lab
, …: color specificationlty
: line type, e.g. dashed, dotted, solid (default), longdash, …lwd
: line width (helpful to increase for presentation plots)pch
: point typesopar <- par() par(col.lab="red") hist(mtcars$mpg)
cex
: number indicating the amount by which plotting text and symbols should be scaled relative to the default.cex.axis
cex.lab
cex.main
pch
lty
font
, font.axis
, font.lab
, font.main
ps
: font point sizefamily
: font family such as "serif", "sans".boxplot(mpg~cyl, data = mtcars, main="Milage by Car Weight", yaxt="n", xlab="Milage", horizontal=TRUE, col=terrain.colors(3)) legend("topright", inset=.05, title="Number of Cylinders", c("4","6","8"), fill=terrain.colors(3), horiz=TRUE)
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)
x <- seq(0, 2 * pi, length = 100) sine <- sin(x) cosine <- cos(x) matplot(x, cbind(sine, cosine), col = c(1, 1), type = "l")
mfrow
par(mfrow = c(2, 2)) boxplot(precip) hist(precip) plot.ecdf(precip) qqnorm(precip)
par(mfrow = c(1, 1))
layout()
attach(mtcars) layout(matrix(c(1,1,2,3), 2,2, byrow = TRUE)) hist(wt) hist(mpg) hist(disp)
detach(mtcars)
attach(mtcars) layout(matrix(c(1,1,2,3), 2,2, byrow = TRUE), widths = c(2,1), heights = c(1,1)) hist(wt) hist(mpg) hist(disp)
detach(mtcars)
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