Skip to content

Instantly share code, notes, and snippets.

@mw55309
Created January 10, 2016 22:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mw55309/456bbedf6a1e0ef48ca4 to your computer and use it in GitHub Desktop.
Save mw55309/456bbedf6a1e0ef48ca4 to your computer and use it in GitHub Desktop.
R code to create Excel-like barplot
x <- data.frame(d=runif(12), g=rep(1:4, each =3))
my.col <- c("deepskyblue3","darkorange2","darkgray","gold")
spacer <- c(1, 0.1, 0.1, 1, 0.1, 0.1, 1, 0.1, 0.1, 1, 0.1, 0.1)
bw <- 0.8
xmax <- (sum(spacer) * bw) + (nrow(x) * bw)
barplot(x$d, col=my.col[x$g], yaxt="n", xaxt="n", width=bw, xlim=c(0,15), space=spacer, border=NA)
axis(side=2, at=seq(0,1,by=0.1), labels=TRUE, tick=FALSE, lwd=0, las=2, cex.axis=0.8)
midbar <- function(x) {
pos <- (sum(spacer[1:x]) * bw) + (bw * x) - (bw/2)
return(pos)
}
xtks <- apply(array(c(2,5,8,11)),1,midbar)
axis(side=1, at=xtks, labels=1:4, tick=TRUE, lwd=0)
for (h in seq(0.1,0.9,by=0.1)) {
lines(c(0,xmax+1), c(h,h))
}
barplot(x$d, col=my.col[x$g], yaxt="n", xaxt="n", width=bw, xlim=c(0,15), space=spacer, add=TRUE, border=NA, main="IFL Excel")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment