1

次のコードを使用して、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) に合わせてプロットの周りにボックスを追加したいと思います。私は何年もかけてこれを行う方法を見つけようとしましたが、運がありませんでした。どんな助けでも本当に感謝しています。ありがとう!

4

1 に答える 1

2

個々の線を引くのはどうですか?これを行うsegment代わりに関数を使用できます。box

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)

ここに画像の説明を入力

于 2014-09-04T14:13:01.130 に答える