如何在条形图中设置网格的限制?

How to set limits of grid in barplot?

我试图在我的条形图中放置网格,但它们出现在数据前面而不是背景中。我尝试使用

修复此问题
panel.first = grid()

至于我要绘制的数据,第一列由年份数字 (2014-2021) 组成,第二列是相应的值(所有向量 类 都是数字)。尝试使用以下代码绘图时:

par(mfrow=c(1,2))

barplot(mean_trend[,2],names.arg = mean_trend[,1],col="skyblue",ylim = c(0.1,95),cex=0.8,cex.names=0.85,las=2,cex.lab=0.85,lwd=1.5,panel.first = grid())
mtext(side=2,line=2.3, "Average amount in mm", font=2, cex=0.8)
box(lwd=1.5)

barplot(freq_trend[,2],names.arg = freq_trend[,1],col="skyblue",ylim = c(2,4500),cex=0.85,cex.names=0.85,las=2,cex.lab=0.85,lwd=1.5,panel.first = grid())
box(lwd=1.5)
mtext(side=2,line=3.3, "Average flood frequency", font=2, cex=0.8)

我得到如下结果

如您所见,网格现在落后于绘制的数据,但超出了 box/plot 限制。我该如何解决这个问题?

亲切的问候

因为你没有添加数据mean_trend - 我用其他数据举个例子。

关于 add 和其他论点 - 您可以阅读 ?barplot

# One row, two columns
par(mfrow = c(1, 2))

#PLOT1
barplot(table(mtcars$cyl), main = "PLOT 1", col = c("yellow", "green", "red"), ylim = c(0, 15))
grid(nx = NULL, ny = NULL, lwd = 1, lty = 1, col = "gray")
barplot(table(mtcars$cyl), col = c("yellow", "green", "red"), ylim = c(0, 15), add = TRUE)
 
#PLOT2
barplot(table(mtcars$cyl), main = "PLOT 2", col = c("yellow", "green", "red"), ylim = c(0, 15))
grid(nx = NULL, ny = NULL, lwd = 1, lty = 1, col = "gray")
barplot(table(mtcars$cyl), col = c("yellow", "green", "red"), ylim = c(0, 15), add = TRUE)