轴标签与轴值重叠问题

3

我在正确绘制以下图表的标签方面有问题:

scatterhist = function(x, y, xlab="", ylab=""){
  zones=matrix(c(2,0,1,3), ncol=2, byrow=TRUE)
  layout(zones, widths=c(4/5,1/5), heights=c(1/5,4/5))
  xhist = hist(x, plot=F, breaks=10)
  yhist = hist(y, plot=F, breaks=10)
  top = max(c(xhist$counts, yhist$counts))

  par(mar=c(3,3,1,1))
  plot(x, y)

  par(mar=c(0,3,1,1))
  barplot(xhist$counts, axes=FALSE, ylim=c(0, top), space=0)

  par(mar=c(3,0,1,1))
  barplot(yhist$counts, axes=FALSE, xlim=c(0, top), space=0, horiz=TRUE)

  par(oma=c(3,3,0,0))
  mtext(xlab, side=1, line=1, outer=TRUE, adj=0, 
    at=.8 * (mean(x)-min(x))/(max(x)-min(x)))
  mtext(ylab, side=2, line=1, outer=TRUE, adj=0, 
    at=.8 * (mean(y)-min(y))/(max(y)-min(y)))
}

当我输入时:
scatterhist(x[,6], x[,7], xlab="Cost of Taxi", ylab="Cost of Fuel")

标签与坐标轴的值重叠。但是,如果我键入:
scatterhist(x[,6], x[,7], xlab="Cost of Taxi", ylab="Cost of Fuel")
scatterhist(x[,6], x[,7], xlab="Cost of Taxi", ylab="Cost of Fuel")

第二张图正确显示了... 有人能帮我解决这个问题吗? 我也尝试在第一次绘制之前通过 windows() 打开图形窗口,但这并不起作用... 谢谢!Stefano
1个回答

2

在第一个par(mar=...)调用之前,应该先添加您的par(oma=c(3,3,0,0))行,因为它应用于整个设备区域(即如果您已经绘制了一些图形,则无法更改外边距的大小)。

scatterhist = function(x, y, xlab="", ylab=""){
    zones=matrix(c(2,0,1,3), ncol=2, byrow=TRUE)
    layout(zones, widths=c(4/5,1/5), heights=c(1/5,4/5))
    par(oma=c(3,3,0,0))

    xhist = hist(x, plot=F, breaks=10)
    yhist = hist(y, plot=F, breaks=10)
    top = max(c(xhist$counts, yhist$counts))

    par(mar=c(3,3,1,1))
    plot(x, y)

    par(mar=c(0,3,1,1))
    barplot(xhist$counts, axes=FALSE, ylim=c(0, top), space=0)

    par(mar=c(3,0,1,1))
    barplot(yhist$counts, axes=FALSE, xlim=c(0, top), space=0, horiz=TRUE)

    mtext(xlab, side=1, line=1, outer=TRUE, adj=0, 
        at=.8 * (mean(x)-min(x))/(max(x)-min(x)))
    mtext(ylab, side=2, line=1, outer=TRUE, adj=0, 
        at=.8 * (mean(y)-min(y))/(max(y)-min(y)))
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接