R:调整条形图上刻度之间的间距

R: Adjust spacing between ticks on barplot

我需要重新创建一个用 R 制作并随后用 Inkscape 编辑的图表。我想在 R 中做尽可能多的事情。这是图表:

我稍后将使用 Inkscape 添加图表中的一些文本标签。我的问题在于蜱虫。我决定使用没有轴的 barplot() 并将它们与 axis() 分开添加。如您所见,条形之间有一个间距。我只是不知道如何为 x 轴上的刻度设置间距,以便使用条形调整它们。现在蜱虫正在离开中心。这是我的代码:

mwe <- data.frame(month=c("Dec 2007", "Jan 2008", "Feb 2008", "Mar 2008","Apr 2008","May 2008","Jun 2008"),
                 job_difference_in_thousands=c(70, -10, -50, -33, -149, -231, -193),
                 color=c("darkred","darkred","darkred","darkred","darkred","darkred","darkred"))
                 
barplot(height=mwe$job_difference_in_thousands,
        axes=F,
        col=mwe$color,
        border = NA,
        space=0.1,
        main="Grafik X_1 NEW JOBS IN THE UNITED STATES",
        cex.main=0.75,
        ylim=c(-800,600),
)
axis(1,pos=0, lwd.tick=0, labels=F,line=1, outer=TRUE)
axis(1,at=0:6+0.5,labels=FALSE,lwd=0,lwd.tick=1)
axis(1, at=0:6+0.5,
     labels=mwe$month,
     cex.axis=0.65,
     lwd=0)
axis(2, at=seq(-800,600,by=200),las=2,cex.axis=0.65, lwd=0, lwd.tick=1)

reprex package (v0.3.0)

于 2020-12-28 创建

R 在调用 barplot 时返回条形图中点的位置。因此,只需存储 barplot 的 return 并将其用作刻度位置使它们居中。

mwe <- data.frame(month=c("Dec 2007", "Jan 2008", "Feb 2008", "Mar 2008","Apr 2008","May 2008","Jun 2008"),
                 job_difference_in_thousands=c(70, -10, -50, -33, -149, -231, -193),
                 color=c("darkred","darkred","darkred","darkred","darkred","darkred","darkred"))
                 
bar_pos <- barplot(height=mwe$job_difference_in_thousands,
        axes=F,
        col=mwe$color,
        border = NA,
        space=0.1,
        main="Grafik X_1 NEW JOBS IN THE UNITED STATES",
        cex.main=0.75,
        ylim=c(-800,600),
)
axis(1,pos=0, lwd.tick=0, labels=F,line=1, outer=TRUE)
axis(1,at=bar_pos,labels=FALSE,lwd=0,lwd.tick=1)
axis(1, at=bar_pos,
     labels=mwe$month,
     cex.axis=0.65,
     lwd=0)
axis(2, at=seq(-800,600,by=200),las=2,cex.axis=0.65, lwd=0, lwd.tick=1)