r - Barplot with deviations from a non zero number -
i have measurements of variable. values positive. in barchart want show measurements , deviation of measurements overall mean. bars go mean , go down. in order such graph tried using offset
barplot
function not work. found solution not ideal , prone mistakes. applying axes requires tweaking of numbers. there more elegant solution this? thanks!
(aaa <- c(1: 10)) (meanz <- mean(aaa)) (ccc <- aaa-meanz[1]) barplot(ccc, axes=false, ylim=c(-5, 5)) (yvals <- c(-5, 0, 5)) (yvalscorr <- yvals+meanz) axis(side=2, at=yvals, labels=yvalscorr, mgp=c(1,1,1), outer=true, lwd=2, las=1, cex.axis=1.0, tck=0.02, hadj=0.5, padj=0.5, pos=-0.2)
this more or less intend (though more flexibility setting axes values @ 0 needed)
what defining function this
x <- cumsum(runif(10)) 0 <- mean(x) offsetbarplot <- function(x, zero=0, ..., yaxt="c", ylim) { xo <- x-zero yax <- pretty(x) if (missing(ylim)) ylim<-range(yax-zero) else ylim<-ylim-zero barplot(xo, ..., yaxt=ifelse(yaxt=="c","n",yaxt), ylim=ylim) if(yaxt=="c") { axis(side=2, at=yax-zero, labels=yax) } } offsetbarplot(x, zero)
this way can use of barplot
code possible. can still pass many of same options. draw custom axis on side.
Comments
Post a Comment