问题
I want to combine 4 figures in the same plot. Each barplot
is related to the image
plot. I want the bar width to be exactly the same width as the grid width in the image
plot. The up-barplot and the left-barplot.
I have pasted my code to follow. How can I modify for this purpose? Or can I use ggplot2
for 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 packages:
# [1] datasets utils stats graphics grDevices methods base
#
# 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 a namespace (and not attached):
# [1] tools_3.1.1
回答1:
If you don't specify xlim
and ylim
, the plots will fill the available space. Then it's just a matter of providing xaxs='i'
for the upper plot, and yaxs='i'
for the left and 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)) # correct x-axis tick labels for 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)
来源:https://stackoverflow.com/questions/26707036/fine-control-of-bar-width-in-barplot