精细控制条形图中的条形宽度 [英] Fine control of bar width in barplot

查看:42
本文介绍了精细控制条形图中的条形宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在同一图中组合4个图形.每个 barplot image 图相关.我希望条形宽度与 image 图中的网格宽度完全相同.上图和左图.

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.

我粘贴了要遵循的代码.我如何为此目的进行修改?还是可以为此使用 ggplot2 ?

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

推荐答案

如果未指定 xlim ylim ,则图将填充可用空间.然后只需为上方的图提供 xaxs ='i',为左侧和右侧的图提供 yaxs ='i'.

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)

这篇关于精细控制条形图中的条形宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆