gpt4 book ai didi

r - 轴标签与轴值重叠

转载 作者:行者123 更新时间:2023-12-04 09:34:02 26 4
gpt4 key购买 nike

我在正确绘制下图的标签时遇到问题:

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() 打开图形窗口,但这不起作用......

谢谢!斯特凡诺

最佳答案

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

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)))
}

关于r - 轴标签与轴值重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12368997/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com