定义R中条形图周围长方体的极限
我已使用以下代码创建了一个x轴(0-23)上有24个条形图和背景着色的条形图:定义R中条形图周围长方体的极限,r,plot,axis,box,R,Plot,Axis,Box,我已使用以下代码创建了一个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=
#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)。我花了很长时间试图找到一种方法来做到这一点,但没有运气。任何帮助都将不胜感激。谢谢 画一条线怎么样?您可以使用
段
功能而不是框
来执行此操作:
segments(24,10, 24,0)
segments(0,10, 24,10)
完整代码:
#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)
segments(24,10, 24,0)
segments(0,10, 24,10)
在第一次调用
barplot
时如何处理xlim=c(0,23)
?谢谢,但这会阻止为条23绘制最后的记号标记,并且不会解决x轴左侧的框问题,即框边距延伸到x=0以下?@ajb。谢谢线段适用于上边框,但出于某种原因不适用于右边框?有什么建议吗?嗯,对我来说很有用,我已经编辑了我的答案,以包含运行时得到的确切代码和图像。这就是你要找的吗?你运行代码时看到相同的图了吗?谢谢。进一步检查后,问题似乎与在quartz()窗口中使用segments()有关。在RStudio中使用默认图形窗口时,它可以正常工作。不知道为什么会这样,但你的解决方案很好。谢谢