我使用以下代码在 x 轴 (0-23) 和背景底纹上创建了一个带有 24 个条形的条形图:
#Data
Hours = seq(from=0, to=23)
Mean = rnorm(24, mean=5, sd=2)
#Create number seq for tick mark locations
at_tick = seq_len(length(Hours)+1)
#Plot with background rectangle shading
x=barplot(Mean,names.arg=Hours, border="white", ylab="Freq", xlab="Hour",
ylim=c(0,10), axes=FALSE, space=0, col="grey50")
X = c(0,5)
Y = c(0,10)
rect(X[1], Y[1], X[2], Y[2], border = "gray80", col = "gray80")
X2 = c(19,24)
Y2 = c(0,10)
rect(X2[1], Y2[1], X2[2], Y2[2], border = "gray80", col = "gray80")
barplot(Mean,names.arg=Hours, ylim=c(0,10), border="white", ylab="", xlab="", axes=FALSE, space=0, col="gray50", add=TRUE)
axis(2, las=2, pos=0)
axis(1, at = at_tick -1, pos=0, labels = FALSE)
box(which="plot", bty="]") #add a box around the plot
这将创建一个带有环绕框的图,该框在两个方向上都超出 x 轴的限制。相反,我想在绘图周围添加一个与轴限制对齐的框(即 x 轴:0-23,y 轴:0-10)。我花了很长时间试图找到一种方法来做到这一点,但没有运气。任何帮助将非常感激。谢谢!