使用barplot()绘制较长的x轴和y轴,而不使用box()设置内部长方体

使用barplot()绘制较长的x轴和y轴,而不使用box()设置内部长方体,r,bar-chart,R,Bar Chart,目的: par(mai=c(2, 1, 1, 1), lwd=2) barplot(as.numeric(c(2, 4, 1, 6)), col = c("lightblue"), main="Bar plot", names.arg=c("This is bar 1...1","This is bar 1...2", "This is bar 1...3","This is bar 1...4"), xpd=TRUE, l

目的:

par(mai=c(2, 1, 1, 1), lwd=2)
barplot(as.numeric(c(2, 4, 1, 6)), col = c("lightblue"), main="Bar plot",
        names.arg=c("This is bar 1...1","This is bar 1...2",
                    "This is bar 1...3","This is bar 1...4"),
        xpd=TRUE, las=2, lwd=2, axes=TRUE, axis.lty=1,
        cex.axis=1, cex.names=1, cex.main=1)
axis(4, 0:6)
box(which="outer", lty="solid")
创建与以下草图类似的绘图:

y轴也应具有负值。因此,应增加x轴和y轴,并在末端添加箭头

代码:

par(mai=c(2, 1, 1, 1), lwd=2)
barplot(as.numeric(c(2, 4, 1, 6)), col = c("lightblue"), main="Bar plot",
        names.arg=c("This is bar 1...1","This is bar 1...2",
                    "This is bar 1...3","This is bar 1...4"),
        xpd=TRUE, las=2, lwd=2, axes=TRUE, axis.lty=1,
        cex.axis=1, cex.names=1, cex.main=1)
axis(4, 0:6)
box(which="outer", lty="solid")

如注释中所述,使用xlim和ylim以及箭头函数。 我删除了多余的代码

par(mai=c(2, 1, 1, 1), lwd=2)
LAB <- c("This is bar 1...1","This is bar 1...2",
         "This is bar 1...3","This is bar 1...4")
# save the barplot to extract the x position of the bars
a <- barplot(as.numeric(c(2, 4, 1, 6)), col = c("lightblue"), main="Bar plot", las=2,yaxt="n",xaxt="n",
  ylim=c(-5,7),xlim=c(0,5))
# arrows
arrows(0.1,-2,0.1,6.5,col='red')
arrows(0,0,5,0,col='red')
# ticks and labels
axis(2,1:4,pos=0.1,lwd=2,col="red")
axis(1,at=a,labels = LAB,pos=0,lwd=2,col="red")
par(mai=c(2,1,1,1),lwd=2)

对于负值,在条形图函数中添加一个ylim=c(-2,6)对于箭头,可以使用函数
箭头
哪些参数是yaxt=“n”,xaxt=“n”。我需要在x上用破折号编号,这些参数分别覆盖轴的绘图。我编辑了答案,也打印了记号。