R中的条形图-恢复xlim并使用par(新=T)

R中的条形图-恢复xlim并使用par(新=T),r,R,我需要在R中做一个条形图,然后使用par(new=T)将一个散点图放在它上面,并使用一个新的轴 如果我设置了xlim,一切都很好: xx=c(0,12) k=barplot(1:10,xlim=xx) par(new=T) plot(x=k,y=1:10/2,xlim=xx,xaxt='n',yaxt='n') axis(1,at=k,labels=1:10) axis(4) 但是,我需要在不设置xlim的情况下执行此操作。我以为par('usr')可以解决我的问题,但我已经尝试了很长时间,但

我需要在R中做一个条形图,然后使用
par(new=T)
将一个散点图放在它上面,并使用一个新的轴

如果我设置了
xlim
,一切都很好:

xx=c(0,12)
k=barplot(1:10,xlim=xx)
par(new=T)
plot(x=k,y=1:10/2,xlim=xx,xaxt='n',yaxt='n')
axis(1,at=k,labels=1:10)
axis(4)
但是,我需要在不设置
xlim
的情况下执行此操作。我以为
par('usr')
可以解决我的问题,但我已经尝试了很长时间,但没有成功

以下是我正在使用的:

k=barplot(1:10)
xx=par('usr')[1:2]
par(new=T)
plot(x=k,y=1:10/2,xlim=xx,xaxt='n',yaxt='n')
axis(1,at=k,labels=1:10)
axis(4)
在这种情况下,x轴和新点未正确居中。我做错了什么

以防需要或在任何方面有用,以下是
R.Version()
的输出:

编辑:

这个肮脏的黑客(运行两次barplot)解决了这个问题,但它远不是一个合适的解决方案:

k=barplot(1:10)
xx=par('usr')[1:2]
k=barplot(1:10,xlim=xx)
par(new=T)
plot(x=k,y=1:10/2,xlim=xx,xaxt='n',yaxt='n')
axis(1,at=k,labels=1:10)
axis(4)
根据
?条形图

width
optional vector of bar widths. Re-cycled to length the
number of bars drawn. Specifying a single value will have no visible
effect unless xlim is specified.

当指定xlim时,条形图似乎有所不同。可能是这导致了问题?

您尝试使用
add=TRUE
选项,但手动设置轴。这将使任务复杂化,因为每个绘图将创建自己的sacles和绘图设置。创建条形图后,这里更容易使用
叠加点

## set `ylim`  to not "cut" the last point.
k=barplot(1:10,ylim=c(0,11))
## points will use the plot scales already created
points(x=k,y=1:10/2,col='red',pch=20,cex=4)
axis(1,at=k,labels=1:10)
## to set new different axes in the right 
par(new=TRUE)
plot(x=k,y=1:10/2,type='n',xaxt='n',yaxt='n')
axis(4)

不幸的是,这不起作用,因为我需要一个新的(不同的)轴在右边。@dd\u rlwll我的编辑怎么办?这就是你要找的吗?不是。在您的示例中,右侧的新轴没有任何用途,因为条形图和点都是根据左轴缩放的。我需要红点能够使用所有可用空间(因此是新的轴),并且我需要能够以一种不需要硬编码任何参数(包括ylim)的方式对其进行编码,因为我手上的数据集与我用来解释我的问题的这个简单示例不同。谢谢你的帮助。
## set `ylim`  to not "cut" the last point.
k=barplot(1:10,ylim=c(0,11))
## points will use the plot scales already created
points(x=k,y=1:10/2,col='red',pch=20,cex=4)
axis(1,at=k,labels=1:10)
## to set new different axes in the right 
par(new=TRUE)
plot(x=k,y=1:10/2,type='n',xaxt='n',yaxt='n')
axis(4)