Customize diagrams: Add additional elements

TODO

  • link to diagDevice for regions, diagFormat, diagScatter

Install required packages

Hmisc, mvtnorm, plotrix

Identify coordinates in scatterplots

To later add elements to the plot

$x
[1] 4.952304 7.076921 2.068896

$y
[1] 0.09391669 -0.13407651 -0.88645406

Add elements to arbitrary device regions

Each device has its own coordinate system.

$x
[1] -1.304

$y
[1] -2.027974
$x
[1] -0.6236 11.6236

$y
[1]  2.252680 -1.802676
plot of chunk rerDiagAddElements01
plot of chunk rerDiagAddElements01

Points and lines

Points and lines

plot of chunk rerDiagAddElements02
plot of chunk rerDiagAddElements02

Gridlines, line segments, and arrows

plot of chunk rerDiagAddElements03
plot of chunk rerDiagAddElements03

Rectangles, polygons, and text

Rectangles and text

plot of chunk rerDiagAddElements04
plot of chunk rerDiagAddElements04

Polygons, mathematical symbols, and custom axes

Filled polygon

mu    <- 0
sigma <- 3
xLims <- c(mu-4*sigma, mu+4*sigma)
X     <- seq(xLims[1], xLims[2], length.out=100)
Y     <- dnorm(X, mu, sigma)
selX  <- seq(mu-sigma, mu+sigma, length.out=100)
selY  <- dnorm(selX, mu, sigma)
cdf   <- pnorm(X, mu, sigma)

par(mar=c(5, 4, 4, 5), cex.lab=1.4)
plot(X, Y, type="n", xlim=xLims-c(-2, 2), xlab=NA, ylab=NA,
     main="Normal PDF and CDF N(0, 3)")
box(which="plot", col="gray", lwd=2)
polygon(c(selX, rev(selX)), c(selY, rep(-1, length(selX))),
        border=NA, col="lightgray")
lines(X, Y, lwd=2)
par(new=TRUE)
plot(X, cdf, xlim=xLims-c(-2, 2), type="l", lwd=2, col="blue", xlab="x",
     ylab=NA, axes=FALSE)
axis(side=4, at=seq(0, 1, by=0.1), col="blue")
segments(x0=c(mu-sigma, mu, mu+sigma),
         y0=c(-1, -1, -1),
         x1=c(mu-sigma, mu, mu+sigma),
         y1=c(pnorm(mu-sigma, mu, sigma), pnorm(mu, mu, sigma),
              pnorm(mu+sigma, mu, sigma)),
         lwd=2, col=c("darkgreen", "red", "darkgreen"), lty=2)
segments(x0=c(mu-sigma, mu, mu+sigma),
         y0=c(pnorm(mu-sigma, mu, sigma), pnorm(mu, mu, sigma),
              pnorm(mu+sigma, mu, sigma)),
         x1=xLims[2]+10,
         y1=c(pnorm(mu-sigma, mu, sigma), pnorm(mu, mu, sigma),
              pnorm(mu+sigma, mu, sigma)),
         lwd=2, col=c("darkgreen", "red", "darkgreen"), lty=2)
arrows(x0=c(mu-sigma+0.2, mu+sigma-0.2), y0=-0.02,
       x1=c(mu-0.2, mu+0.2),             y1=-0.02,
       code=3, angle=90, length=0.05, lwd=2, col="darkgreen")
mtext(text="F(x)", side=4, line=3, cex=1.4)
rect(-8.5, 0.92, -5.5, 1.0, col="lightgray", border=NA)
text(-7.2, 0.9, labels="probability")
text(-7.1, 0.86, expression(of~interval~group("[", list(-sigma, sigma), "]")))
text(mu-sigma/2, 0,    expression(sigma), col="darkgreen", cex=1.2)
text(mu+sigma/2, 0,    expression(sigma), col="darkgreen", cex=1.2)
text(mu+0.5,     0.02, expression(mu),    col="red",       cex=1.2)
plot of chunk rerDiagAddElements05
plot of chunk rerDiagAddElements05

Polygon with shading lines

[1] 0.8
[1] 2.529822
[1] 1.833113
[1] 0.7544248
plot of chunk rerDiagAddElements06
plot of chunk rerDiagAddElements06

As opposed to polygon(), function polypath() can draw polygons with holes.

Function curves and mathematical symbols

plot of chunk rerDiagAddElements07
plot of chunk rerDiagAddElements07

See ?plotmath and demo(plotmath) for explanations and further demos for mathematical expressions.

Custom axes, custom grid, and plot legend

plot of chunk rerDiagAddElements08
plot of chunk rerDiagAddElements08

Error bars

Simulate data

Using plotCI() from package plotrix

plot of chunk rerDiagAddElements09
plot of chunk rerDiagAddElements09

Means and error bars in a two-way design

plot of chunk rerDiagAddElements11
plot of chunk rerDiagAddElements11

Using arrows()

plot of chunk rerDiagAddElements12
plot of chunk rerDiagAddElements12

Raster images

See packages imager or adimpro to read in image files of various formats.

2-D color ramp square pattern

Gabor patch: oriented 2-D cosine with contrast following a 2-D normal distribution

plot of chunk rerDiagAddElements13
plot of chunk rerDiagAddElements13

Detach (automatically) loaded packages (if possible)

Get the article source from GitHub

R markdown - markdown - R code - all posts