8

X 軸上の 2 つの異なる要因によって分類されるボックスプロットを R-cran で作成しようと取り組んでいます。私の問題は、凡例を使用して2〜3レベルしかない2番目の因子にラベルを付けながら、グラフ全体に適切に広がる+20レベルの1つの因子のラベルを作成することにあります。

これは、実際のデータセットを大まかに模倣したテスト スクリプトです。

d<-data.frame(x=rnorm(1500),f1=rep(seq(1:20),75),f2=rep(letters[1:3],500))
# first factor has 20+ levels
d$f1<-factor(d$f1)
# second factor a,b,c
d$f2<-factor(d$f2)

boxplot(x~f2*f1,data=d,col=c("red","blue","green"),frame.plot=TRUE,axes=FALSE)

# y axis is numeric and works fine
yts=pretty(d$x,n=5)
axis(2,yts)

# I know this doesn't work; what I'd like is to spread the factors out 
# so the each group of three(a,b,c) is labeled correctly
axis(1,at=seq(1:20))

# Use the legend to handle the f2 factor labels
legend(1, max(d$x), c("a", "b","c"),fill = c("red", "blue","green"))

助けてくれてありがとう

4

2 に答える 2

13

FWIW、ggplot2解決策:

library(ggplot2)
ggplot(data = d, aes(x = f1, y = x)) + 
  geom_boxplot(aes(fill = f2), width = 0.8) + theme_bw()

ここに画像の説明を入力

于 2012-05-04T01:19:23.430 に答える
5

3 つのボックスの各グループの中央にラベルが必要な場合は、次のようにしてみてください。

axis(1,at=seq(2,60,3),labels=1:20,cex.axis=0.7)

ここに画像の説明を入力

一般化すると、これは次のようになります。

groups <- 20
numbox <- 3
total <- groups * numbox
xpoints <- seq(median(1:numbox),total,numbox)
于 2012-05-04T01:05:11.023 に答える