Thursday 15 August 2013

r - Fine control of bar width in barplot -



r - Fine control of bar width in barplot -

i want combine 4 figures in same plot. each barplot related image plot. want bar width same width grid width in image plot. up-barplot , left-barplot.

i have pasted code follow. how can modify purpose? or can utilize ggplot2 this?

datmat <- matrix(rep(1, 100), nrow=10) op=par(oma=c(1, 1, 1, 1), mar=c(1, 1, 1, 1), lwd=0.2, cex=.5) nf <- layout(matrix(c(0, 1, 0, 2, 3, 4), 2, 3, byrow=true), widths=c(1, 6, 1), heights=c(1, 3), respect=true) par(mar=c(3, 0.5, 3, 0), mgp=c(0, 1, -2.2)) barplot(seq(1, 10), width=1, xlim=c(0, ncol(datmat)), xlab="", space=0, col="blue", border="white" ) box("inner", lty="dotted", col="green") box("outer", lty="solid", col="green") par(mar=c(0, 0, 0, 0), mgp=c(0, 1, -1)) barplot(-seq(1, 10), ylim=c(0, nrow(datmat)), width=1.025, horiz=true, ,axes =t, space=0, col="blue", border="white" ) par(mar=c(1, 0, 0, 0)) x.len <- ncol(datmat) y.len <- nrow(datmat) single.col <- 'chartreuse4' double.col <- 'blue4' triple.col <- '#ffff33' four.col <- '#ff7f00' five.col <- '#e41a1c' colors <- c('grey90', single.col, double.col, triple.col, four.col, five.col) image(x=1:x.len, y=1:y.len, z=t(datmat), col=colors, breaks=c(-0.5, 0.5 , 1.5 , 2.5 , 3.5 , 4.5 , 5.5), axes=false, ann=f) abline(h=seq(0.5, 0.5+y.len), col='white', lwd=0.5); abline(v=seq(0.5, 0.5+x.len), col='white', lwd=0.5) par(mar=c(0, 0, 0, 0), mgp=c(0, 1, 0)) barplot(seq(1, 10), ylim=c(0, nrow(datmat)), width=.5, horiz=true, axes=false, space=0, col="blue", border ="white" ) axis(side=1) sessioninfo() # r version 3.1.1 (2014-07-10) # platform: x86_64-apple-darwin13.1.0 (64-bit) # # locale: # [1] c/utf-8/c/c/c/c # # attached base of operations packages: # [1] datasets utils stats graphics grdevices methods base of operations # # other attached packages: # [1] xlsx_0.5.7 xlsxjars_0.6.1 # [3] rjava_0.9-6 nutshell_2.0 # [5] nutshell.audioscrobbler_1.0 nutshell.bbdb_1.0 # [7] faraway_1.0.6 mass_7.3-35 # # loaded via namespace (and not attached): # [1] tools_3.1.1

if don't specify xlim , ylim, plots fill available space. it's matter of providing xaxs='i' upper plot, , yaxs='i' left , right plots.

par(oma=c(1, 1, 1, 1), mar=c(1, 1, 1, 1), lwd=0.2, cex=0.5) nf <- layout(matrix(c(0, 2, 0, 3, 1, 4), 2, 3, byrow=true), widths=c(1, 6, 1), heights=c(1, 3), respect=true) par(mar=rep(0, 4)) m <- matrix(runif(100), ncol=10) image(x=seq_len(ncol(m)), y=seq_len(nrow(m)), z=t(m), axes=false, ann=f) abline(h=seq(0.5, 0.5 + nrow(m)), v=seq(0.5, 0.5 + ncol(m)), col="white") par(mar=c(1, 0, 0, 0)) barplot(seq(1, 10), xlab="", space=0, border="white", xaxs='i') par(mar=c(0, 0, 0, 1)) barplot(-seq(1, 10), horiz=true, space=0, border="white", yaxs='i', xaxt='n') axis(1, at=axticks(1), labels=-axticks(1)) # right x-axis tick labels left plot par(mar=c(0, 1, 0, 0)) barplot(seq(1, 10), horiz=true, axes=f, space=0, border="white", yaxs='i') axis(1)

r plot width bar-chart

No comments:

Post a Comment