我正在尝试在 R-cran 中制作一个箱线图,该箱线图按 x 轴上的两个不同因素分类。我的问题在于为一个具有 +20 级别的因素创建标签,该标签适当地跨越整个图表,同时使用图例来标记只有 2 到 3 个级别的第二个因素。
这是一个大致模仿我的实际数据集的测试脚本:
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"))
谢谢你的帮助