如何设置轴范围R.

时间:2016-04-01 09:22:24

标签: r axis

我在r中设置轴范围时遇到问题。我的数据在x轴上只有2到9的值,但是我希望它从1到10。有任何快速提示吗?

head(SS)
  Phase Bed Site ACC X.M.SA
1     1   1    1  NG     NO
2     1   1    2  NG     NO
3     1   1    3  SG     NO
4     1   1    4  SG     NO
5     1   1    5  SG     NO
6     1   2    1  SG     NO

XMSA<-factor(SS$X.M.SA)
ACC<-factor(SS$ACC,ordered = TRUE,levels=c("NG","SG","LG","MG","HG"))

boxplot(ACC[XMSA=="MSSA"]~SS$Bed[XMSA=="MSSA"],
xlab="Bed",ylab="Growth",
las=1, yaxt="n",ylim=c(1,5),xlim=c(1,10))
axis(2, at=c(1,2,3,4,5),labels=c("NG","SG","LG","MG","HG"),las=1)

enter image description here

1 个答案:

答案 0 :(得分:5)

没有数据,我试图重现你的情节错误:

plot(x=as.factor(2:8),y=2:8,xlim = c(1,10)

其中给出了以下情节:

enter image description here

将你的情节改为:

boxplot(x= as.numeric(as.character(SS$Bed[XMSA=="MSSA"])),
y= ACC[XMSA=="MSSA"]
xlab="Bed",ylab="Growth",
las=1, yaxt="n",ylim=c(1,5),xlim=c(1,10))
axis(2, at=c(1,2,3,4,5),labels=c("NG","SG","LG","MG","HG"),las=1)

可能会解决您的问题。

修改

似乎公式更改为因子并将其从1到项目数量进行排序,因此我将使用y轴上的技巧来解决此问题。

boxplot(ACC[XMSA=="MSSA"]~SS$Bed[XMSA=="MSSA"],
xlab="Bed",ylab="Growth",
las=1, yaxt="n",ylim=c(1,5),xlim=c(0,9),xaxt="n")
axis(2, at=1:5,labels=c("NG","SG","LG","MG","HG"),las=1)
axis(1, at=0:9,labels=1:10,las=1)