# annotated image : arrows and text # ===================================== # an elegant arrow # http://stackoverflow.com/questions/5731432/change-arrowhead-of-arrows curve(dnorm(x), from=-3.5, to=3.5, col="violetred3", xlab="x", ylab="probability", main="Annotated Image") z.a <- -2 poly.xs <- seq(from=-3.5, to=z.a, by=.01) polygon(x=c(-3, poly.xs, z.a), y=c(0, dnorm(poly.xs),0), col="orange") arrows(-2.4, 0.045, -2.2, 0.015, angle=10, length=0.1, lwd=1, col="dark red") text(-2.5, 0.055, expression(alpha), col="dark red") # ===================================== # acceptance and rejection regions for McNemar's Test (Rosner, 2011, p.376) curve(dchisq(x, df=1), from=0, to=7, col="violetred3", xlab="Value", ylab="frequency", main="McNemar's Test") X.a <- 3 poly.xs <- seq(from=X.a, to=7, by=.01) polygon(x=c(X.a, poly.xs, 7), y=c(0, dchisq(poly.xs, df=1),0), col="orange") # distribution arrows(.2, dchisq(.2, df=1), .2 + .45, dchisq(.2, df=1) + .1, code=1, angle=10, length=0.1, lwd=1, col="dark red") text(.65, dchisq(.2, df=1) + .15, pos=4, expression(chi[1]^2), col="dark red") text(.95, dchisq(.2, df=1) + .148, pos=4, "distribution", col="dark red") # alternative : use paste() to mix expressions and regular text # text(.65, dchisq(.2, df=1) + .15, pos=4, expression(paste(chi[1]^2," distribution")), col="dark red") # acceptance region arrows(.6, .2, .6 + .7, .2 + .15, code=1, angle=10, length=0.1, lwd=1, col="dark red") text(1.7, .4, "acceptance region", col="dark red") # rejection region arrows(4, .01, 4 + .7, .01 + .15, code=1, angle=10, length=0.1, lwd=1, col="dark red") text(4.8, .20, "rejection region", col="dark red") # ========================================== # plotmath help(plotmath) demo(plotmath) # ========================================== # use image and expression # http://www.biostat.wustl.edu/archives/html/s-news/2000-01/msg00147.html # help page for image : note mention of heat.colors, terrain.colors and topo.colors ?image() # example from the help page for image require(grDevices) # for colours x <- y <- seq(-4*pi, 4*pi, len=27) r <- sqrt(outer(x^2, y^2, "+")) image(z = z <- cos(r^2)*exp(-r/6), col=gray((0:32)/32)) # add an "enhanced" x-label image(z, axes = FALSE, main = "Math can be beautiful ...", xlab = expression(alpha[3] + cos(xi^2) * e^{-r/6})) # add irrelevant but fancy text text(.5, .9, expression(hat(beta) == (X^t * X)^{-1} * X^t * y)) # ===================================== # add arrows to a plot # http://stat.ethz.ch/R-manual/R-devel/library/graphics/html/arrows.html # ===================================== # arrows and segments ?arrows ?segments x <- stats::runif(12); y <- stats::rnorm(12) i <- order(x,y); x <- x[i]; y <- y[i] plot(x,y, main="arrows(.) and segments(.)") ## draw arrows from point to point : s <- seq(length(x)-1)# one shorter than data arrows(x[s], y[s], x[s+1], y[s+1], col= 1:3) s <- s[-length(s)] segments(x[s], y[s], x[s+2], y[s+2], col= 'pink') # ===================================== # arrow.plot ?arrow.plot library(fields) plot(x,y, type="n") arrow.plot( x,y,u,v, arrow.ex=.2, length=.1, col='green', lwd=2) # ===================================== # arrowheads # http://stackoverflow.com/questions/5731432/change-arrowhead-of-arrows plot(c(0:10),type="n") arrows(1,0,2,1,length=0.2,angle=20) arrows(1,1,2,2,length=0.1,angle=40,lwd=3) invisible(mapply(arrows, rep(c(3,6),each=4),rep(3:6,2), rep(c(5,8),each=4),rep(5:8,2), angle=seq(10,40,length.out=8), length=rep(seq(0.1,0.3,length.out=4),2), lwd=rep(1:4,each=2)) ) # elegant arrow (my favorite from the above collection) arrows(3, 1, 4, 2, angle=10, length=0.1, lwd=1, col="dark red") # ===================================== # arrowheads # http://www.oga-lab.net/RGM2/func.php?rd_id=shape:Arrows # ===================================== # Annotating plots: grid lines, arrows, lines, and identifying interesting points # http://connectmv.com/tutorials/r-tutorial/annotating-plots-grid-lines-arrows-lines-and-identifying-interesting-points/ # ===================================== # my.arrows in TeachingDemos package # http://stackoverflow.com/questions/5731432/change-arrowhead-of-arrows # ==========================================